001/*
002 * Copyright 2011-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017package com.unboundid.scim.sdk.examples;
018
019import com.unboundid.scim.data.AttributeValueResolver;
020import com.unboundid.scim.data.BaseResource;
021import com.unboundid.scim.data.Entry;
022import com.unboundid.scim.data.Manager;
023import com.unboundid.scim.data.Name;
024import com.unboundid.scim.data.ResourceFactory;
025import com.unboundid.scim.data.UserResource;
026import com.unboundid.scim.schema.ResourceDescriptor;
027import com.unboundid.scim.sdk.Resources;
028import com.unboundid.scim.sdk.SCIMEndpoint;
029import com.unboundid.scim.sdk.SCIMObject;
030import com.unboundid.scim.sdk.SCIMService;
031
032import javax.ws.rs.core.MediaType;
033import java.net.URI;
034import java.util.Collection;
035
036/**
037 * A simple client example.
038 */
039public class ClientExample {
040
041  /**
042   * A device resource extension.
043   */
044  public static class DeviceResource extends BaseResource
045  {
046    /**
047     * Create a new empty device resource.
048     *
049     * @param resourceDescriptor The resource descriptor of this resource.
050     */
051    public DeviceResource(final ResourceDescriptor resourceDescriptor) {
052      super(resourceDescriptor);
053    }
054
055    /**
056     * Create a device resource based on the provided SCIMObject.
057     *
058     * @param resourceDescriptor The resource descriptor of this resource.
059     * @param scimObject The SCIMObject containing all the attributes and
060     * values.
061     */
062    public DeviceResource(final ResourceDescriptor resourceDescriptor,
063                          final SCIMObject scimObject) {
064      super(resourceDescriptor, scimObject);
065    }
066
067    /**
068     * Retrieves the vendor name of this device.
069     *
070     * @return The vendor name of this device.
071     */
072    public String getVendorName()
073    {
074      return getSingularAttributeValue("urn:com:example:device:1.0",
075          "vendorName", AttributeValueResolver.STRING_RESOLVER);
076    }
077  }
078
079  /**
080   * The resource factory that can be used to create device resource instances.
081   */
082  public static final ResourceFactory<DeviceResource> DEVICE_RESOURCE_FACTORY =
083      new ResourceFactory<DeviceResource>() {
084        /**
085         * {@inheritDoc}
086         */
087        public DeviceResource createResource(
088            final ResourceDescriptor resourceDescriptor,
089            final SCIMObject scimObject) {
090          return new DeviceResource(resourceDescriptor, scimObject);
091        }
092      };
093
094  /**
095   * The main method.
096   *
097   * @param args Parameters for the application.
098   * @throws Exception If an error occurs.
099   */
100  public static void main(final String[] args) throws Exception {
101    final URI uri = URI.create("https://localhost:8443");
102    final SCIMService scimService = new SCIMService(uri, "bjensen", "password");
103    scimService.setAcceptType(MediaType.APPLICATION_JSON_TYPE);
104
105    // Core user resource CRUD operation example
106    final SCIMEndpoint<UserResource> endpoint = scimService.getUserEndpoint();
107
108    // Query for a specified user
109    Resources<UserResource> resources =
110        endpoint.query("userName eq \"bjensen\"");
111    if (resources.getItemsPerPage() == 0) {
112      System.out.println("User bjensen not found");
113      return;
114    }
115    UserResource user = resources.iterator().next();
116
117    Name name = user.getName();
118    if (name != null) {
119      System.out.println(name);
120    }
121
122    Collection<Entry<String>> phoneNumbers = user.getPhoneNumbers();
123    if(phoneNumbers != null) {
124      for(Entry<String> phoneNumber : phoneNumbers) {
125        System.out.println(phoneNumber);
126      }
127    }
128
129    // Attribute extension example
130    Manager manager = user.getSingularAttributeValue(
131        "urn:scim:schemas:extension:enterprise:1.0",  "manager",
132        Manager.MANAGER_RESOLVER);
133    if(manager == null) {
134      resources = endpoint.query("userName eq \"jsmith\"");
135      if (resources.getItemsPerPage() > 0) {
136        UserResource boss = resources.iterator().next();
137        manager = new Manager(boss.getId(), null);
138      } else {
139        System.out.println("User jsmith not found");
140      }
141    }
142
143    user.setSingularAttributeValue("urn:scim:schemas:extension:enterprise:1.0",
144        "manager", Manager.MANAGER_RESOLVER, manager);
145
146    String employeeNumber =
147        user.getSingularAttributeValue(
148            "urn:scim:schemas:extension:enterprise:1.0",  "employeeNumber",
149            AttributeValueResolver.STRING_RESOLVER);
150    if (employeeNumber != null) {
151      System.out.println("employeeNumber: " + employeeNumber);
152    }
153
154    user.setSingularAttributeValue("urn:scim:schemas:extension:enterprise:1.0",
155        "department",  AttributeValueResolver.STRING_RESOLVER, "sales");
156
157    user.setTitle("Vice President");
158
159    // Update the user
160    endpoint.update(user);
161
162    // Demonstrate retrieval by SCIM ID
163    user = endpoint.get(user.getId());
164
165    // Resource type extension example
166    ResourceDescriptor deviceDescriptor =
167        scimService.getResourceDescriptor("Device", null);
168    SCIMEndpoint<DeviceResource> deviceEndpoint =
169        scimService.getEndpoint(deviceDescriptor, DEVICE_RESOURCE_FACTORY);
170  }
171}