001    /*
002     * Copyright 2011-2012 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     */
017    package com.unboundid.scim.sdk.examples;
018    
019    import com.unboundid.scim.data.AttributeValueResolver;
020    import com.unboundid.scim.data.BaseResource;
021    import com.unboundid.scim.data.Entry;
022    import com.unboundid.scim.data.Manager;
023    import com.unboundid.scim.data.Name;
024    import com.unboundid.scim.data.ResourceFactory;
025    import com.unboundid.scim.data.UserResource;
026    import com.unboundid.scim.schema.ResourceDescriptor;
027    import com.unboundid.scim.sdk.SCIMEndpoint;
028    import com.unboundid.scim.sdk.SCIMObject;
029    import com.unboundid.scim.sdk.SCIMService;
030    
031    import javax.ws.rs.core.MediaType;
032    import java.net.URI;
033    import java.util.Collection;
034    
035    /**
036     * A simple client example.
037     */
038    public class ClientExample {
039    
040      /**
041       * A device resource extension.
042       */
043      public static class DeviceResource extends BaseResource
044      {
045        /**
046         * Create a new empty device resource.
047         *
048         * @param resourceDescriptor The resource descriptor of this resource.
049         */
050        public DeviceResource(final ResourceDescriptor resourceDescriptor) {
051          super(resourceDescriptor);
052        }
053    
054        /**
055         * Create a device resource based on the provided SCIMObject.
056         *
057         * @param resourceDescriptor The resource descriptor of this resource.
058         * @param scimObject The SCIMObject containing all the attributes and
059         * values.
060         */
061        public DeviceResource(final ResourceDescriptor resourceDescriptor,
062                              final SCIMObject scimObject) {
063          super(resourceDescriptor, scimObject);
064        }
065    
066        /**
067         * Retrieves the vendor name of this device.
068         *
069         * @return The vendor name of this device.
070         */
071        public String getVendorName()
072        {
073          return getSingularAttributeValue("urn:com:example:device:1.0",
074              "vendorName", AttributeValueResolver.STRING_RESOLVER);
075        }
076      }
077    
078      /**
079       * The resource factory that can be used to create device resource instances.
080       */
081      public static final ResourceFactory<DeviceResource> DEVICE_RESOURCE_FACTORY =
082          new ResourceFactory<DeviceResource>() {
083            /**
084             * {@inheritDoc}
085             */
086            public DeviceResource createResource(
087                final ResourceDescriptor resourceDescriptor,
088                final SCIMObject scimObject) {
089              return new DeviceResource(resourceDescriptor, scimObject);
090            }
091          };
092    
093      /**
094       * The main method.
095       *
096       * @param args Parameters for the application.
097       * @throws Exception If an error occurs.
098       */
099      public static void main(final String[] args) throws Exception
100      {
101        URI uri = URI.create("http://localhost:52959");
102        SCIMService scimService = new SCIMService(uri,
103            "uid=bjensen,dc=example,dc=com", "password");
104        scimService.setAcceptType(MediaType.APPLICATION_JSON_TYPE);
105    
106        // Core user resource CRUD operation example
107        SCIMEndpoint<UserResource> endpoint = scimService.getUserEndpoint();
108    
109        UserResource user = endpoint.get("uid=bjensen,dc=example,dc=com");
110    
111        Name name = user.getName();
112    
113        Collection<Entry<String>> phoneNumbers = user.getPhoneNumbers();
114        if(phoneNumbers != null)
115        {
116          for(Entry<String> phoneNumber : phoneNumbers)
117          {
118            System.out.println(phoneNumber);
119          }
120        }
121    
122        // Attribute extension example
123        Manager manager = user.getSingularAttributeValue(
124            "urn:scim:schemas:extension:enterprise:1.0",  "manager",
125            Manager.MANAGER_RESOLVER);
126        if(manager == null)
127        {
128          manager = new Manager("uid=jsmith,dc=example,dc=com", "Manager John");
129        }
130        manager.setDisplayName("Best in the world");
131        user.setSingularAttributeValue("urn:scim:schemas:extension:enterprise:1.0",
132            "manager", Manager.MANAGER_RESOLVER, manager);
133    
134        String employeeNumber =
135            user.getSingularAttributeValue(
136                "urn:scim:schemas:extension:enterprise:1.0",  "employeeNumber",
137                AttributeValueResolver.STRING_RESOLVER);
138    
139        user.setSingularAttributeValue("urn:scim:schemas:extension:enterprise:1.0",
140            "department",  AttributeValueResolver.STRING_RESOLVER, "sales");
141    
142        user.setNickName("brad");
143    
144        endpoint.update(user);
145    
146        // Resource type extension example
147        ResourceDescriptor deviceDescriptor =
148            scimService.getResourceDescriptor("Device", null);
149        SCIMEndpoint<DeviceResource> deviceEndpoint =
150            scimService.getEndpoint(deviceDescriptor, DEVICE_RESOURCE_FACTORY);
151      }
152    }