JAVA

/*******************************************************************************
 * Copyright 2020 Precisely Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 *  See the License for the specific language governing permissions and limitations under the License.
 *******************************************************************************/
package com.precisely.apis.samples;

import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class IdentityProfilesSample {

	  //FIXME Assign your Client Id here
    private static String API_KEY = "";

    //FIXME Assign your Secret here
    private static String SECRET = "";

	private static final String ACCESS_TOKEN = "access_token";

	private static final String BEARER = "Bearer ";

	private static final String BASIC = "Basic ";

	private static final String CLIENT_CREDENTIALS = "client_credentials";

	private static final String GRANT_TYPE = "grant_type";

	private static final String AUTH_HEADER = "Authorization";

	private static final String COLON = ":";

	private static String accessToken;

	private static final String API_FRAGMENT = "identityprofiles/v1/identity/";

	private static String OAUTH2_TOKEN_URL = "https://api.precisely.com/oauth/token";

	private static String PRECISELY_APIS_URL = "https://api.precisely.com/"
			+ API_FRAGMENT;

	public static void main(String[] args) {
		// Acquires OAuth2 token
		acquireAuthToken();

		getIdentityByAddress(true, "1 Global View, troy, NY", null, null,null,null);

		getIdentityByAddress(false, "1 Global View, troy, NY", null, null,null,null);

		getIdentityByEmailAddress(true, "ken@fullcontact.com", "low",null,null);
		
		getIdentityByEmailAddress(false, "ken@fullcontact.com", "high",null,null);
		
		getIdentityByTwitter(true, "nelrahcaz", "high",null,null);
		
		getIdentityByTwitter(false, "nelrahcaz", "high",null,null);
	}


	private static void getIdentityByTwitter(boolean responseTypeIsXml, String twitter, String confidence,String theme,String filter) {
		String apiUrl = "bytwitter?twitter=" + twitter;
		
		if(confidence != null || confidence != ""){
			apiUrl += "&confidence="+ confidence;
		}
		
		processRequest(responseTypeIsXml, apiUrl,theme,filter);
		
	}


	private static void getIdentityByEmailAddress(boolean responseTypeIsXml, String emailAddress, String confidence,String theme,String filter) {
		String apiUrl = "byemail?email=" + emailAddress;
		apiUrl = parameterizeGeoIdentity(confidence,  null,
				apiUrl);

		processRequest(responseTypeIsXml, apiUrl,theme,filter);
	}

	private static void getIdentityByAddress(boolean responseTypeIsXml, String address, String confidence, String maxCandidates,String theme,String filter) {
		String apiUrl = null;
		try {
			apiUrl = "byaddress?address=" + URLEncoder.encode(address, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		apiUrl = parameterizeGeoIdentity(confidence, maxCandidates,
				apiUrl);

		processRequest(responseTypeIsXml, apiUrl,theme,filter);
	}

	private static void processRequest(boolean responseTypeIsXml, String apiUrl,String theme,String filter) {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(PRECISELY_APIS_URL + apiUrl).queryParam("theme",theme).queryParam("filter",filter);
		Builder builder;
		if (responseTypeIsXml) {
			builder = target.request(MediaType.APPLICATION_XML).header(AUTH_HEADER, accessToken);
		} else {
			builder = target.request(MediaType.APPLICATION_JSON).header(AUTH_HEADER, accessToken);
		}
		System.out.println(builder.get(String.class));

	}

	private static String parameterizeGeoIdentity(String confidence,
			String maxCandidates, String apiUrl) {
		if (confidence != null) {
			apiUrl += "&confidence=" + confidence;
		}
		if (maxCandidates != null) {
			apiUrl += "&maxCandidates=" + maxCandidates;
		}

		return apiUrl;
	}

	/**
	 * Acquires OAuth2 token for accessing Precisely APIs
	 */
	private static void acquireAuthToken() {
		String authHeader = BASIC + Base64.getEncoder().encodeToString((API_KEY + COLON + SECRET).getBytes(StandardCharsets.UTF_8));

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(OAUTH2_TOKEN_URL);

		Builder builder = target.request().header(AUTH_HEADER, authHeader);
		Form form = new Form();
		form.param(GRANT_TYPE, CLIENT_CREDENTIALS);
		Response response = builder.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED));
		String jsonResponse = response.readEntity(String.class);

		JsonReader jsonReader = Json.createReader(new StringReader(jsonResponse));
		JsonObject jsonObject = jsonReader.readObject();
		jsonReader.close();
		accessToken = jsonObject.getString(ACCESS_TOKEN);
		accessToken = BEARER + accessToken;
	}

}