Skip to content

Instantly share code, notes, and snippets.

@bertold
Created January 24, 2017 23:53
Show Gist options
  • Save bertold/009a0f8555c11720756711e6b3a6a7a2 to your computer and use it in GitHub Desktop.
Save bertold/009a0f8555c11720756711e6b3a6a7a2 to your computer and use it in GitHub Desktop.
Sample created by Doug Bulkley to demonstrate the UnboundID SCIM SDK interfacing with PingFed
// Test program that uses the SCIM V1.1 SDK to make a connection
// and perform some action against a server with a SCIM V1.1 interface.
// SSL configuration set to blindly trust. NOT valid for a production env.
import java.net.URI;
import java.util.Collection;
import javax.ws.rs.core.MediaType;
import com.unboundid.scim.data.Entry;
import com.unboundid.scim.data.Name;
import com.unboundid.scim.data.UserResource;
import com.unboundid.scim.sdk.Resources;
import com.unboundid.scim.sdk.SCIMEndpoint;
import com.unboundid.scim.sdk.SCIMService;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
public class Client {
public static void main (String[] args) throws Exception {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory trustSelfSigned =
new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());
final Registry<ConnectionSocketFactory> schemeRegistry =
RegistryBuilder.<ConnectionSocketFactory> create()
.register("https", trustSelfSigned)
.register("http", new PlainConnectionSocketFactory())
.build();
PoolingHttpClientConnectionManager connectionManager =
new PoolingHttpClientConnectionManager(schemeRegistry);
Credentials credentials =
new UsernamePasswordCredentials("Administrator", “password”);
CredentialsProvider credentialsProvider =
new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(“pingfed.local”, 9031), credentials);
ClientConfig jerseyConfig = new ClientConfig();
jerseyConfig.property(ApacheClientProperties.CONNECTION_MANAGER,
connectionManager);
jerseyConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER,
credentialsProvider);
jerseyConfig.property(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION,
true);
ApacheConnectorProvider connectorProvider =
new ApacheConnectorProvider();
jerseyConfig.connectorProvider(connectorProvider);
final URI secureUri = URI.create("https://pingfed.local:9031/pf-scim/v1");
final SCIMService scimService = new SCIMService(secureUri, jerseyConfig);
scimService.setAcceptType(MediaType.APPLICATION_JSON_TYPE);
final SCIMEndpoint<UserResource> endpoint = scimService.getUserEndpoint();
String userName = "user.0";
Resources<UserResource> resources = endpoint.query("userName eq \"" + userName + "\"");
if (resources.getItemsPerPage() == 0) {
System.out.println("User " + userName + " not found");
return;
}
UserResource user = resources.iterator().next();
Name name = user.getName();
if (name != null) {
System.out.println(name);
}
Collection<Entry<String>> phoneNumbers = user.getPhoneNumbers();
if (phoneNumbers != null) {
for (Entry<String> phoneNumber : phoneNumbers) {
System.out.println(phoneNumber);
}
}
}
}
@anotherrohit
Copy link

Which scim jars are needed for the import of com.unboundid.scim.data.* and com.unboundid.scim.sdk.* ?Also is this scim2 compatible?

@bertold
Copy link
Author

bertold commented Jun 20, 2020

Which scim jars are needed for the import of com.unboundid.scim.data.* and com.unboundid.scim.sdk.* ?Also is this scim2 compatible?

There is only one dependency that you need scim-sdk-.jar. Look for the SDK on search.maven.org. The comments says that it was written for SCIM-1.1. SCIM-2.0 is very different from 1.x. I would be surprised if it worked as is.

@anotherrohit
Copy link

Do you know why there can be a build path error for below, I have already added the jersey-apache-connector-3.0.0 jar in the build path.
ClientConfig jerseyConfig = new ClientConfig();
jerseyConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);

@bertold
Copy link
Author

bertold commented Jun 23, 2020

It may be easier to resolve this offline. You can reach me at bertold at gmail dot com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment