Skip to content

Instantly share code, notes, and snippets.

@edm00se
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edm00se/e1f0d31663b42a612fc5 to your computer and use it in GitHub Desktop.
Save edm00se/e1f0d31663b42a612fc5 to your computer and use it in GitHub Desktop.
Extending the CustRestConsumer class, the get method is public for invocation but the build URL and credentialing methods are both private, so as to prevent exposing that information. This is covered in detail in my blog post: http://edm00se.github.io/DevBlog/xpages/server-rest-with-authentication/
package com.eric.restful;
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import org.apache.commons.codec.binary.Base64;
import com.google.gson.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.MalformedURLException;
/**
* Class with a single, public, static method to provide a REST consumer
* which returns data as a JsonObject, with authentication.
*
* @author Eric McCormick, @edm00se
*
*/
public class MyDataConsumer {
/**
* Modified copy of co.3edesign.eric.restful CustRestConsumer GetMyData
* method. This one requires provides basic authentication with Base64
* encoding.
*
* @param myUrlStr URL of the REST endpoint
* @return JsonObject of the REST data
*/
public JsonObject GetMyAuthenticatedRestData( String myUrlStr ) {
JsonObject myRestData = new JsonObject();
try{
URL myUrl = new URL(myUrlStr);
URLConnection urlCon = myUrl.openConnection();
urlCon.setRequestProperty("Method", "GET");
urlCon.setRequestProperty("Accept", "application/json");
urlCon.setConnectTimeout(5000);
//set the basic auth of the hashed value of the user to connect
urlCon.addRequestProperty("Authorization", GetMyCredentials() );
InputStream is = urlCon.getInputStream();
InputStreamReader isR = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isR);
StringBuffer buffer = new StringBuffer();
String line = "";
while( (line = reader.readLine()) != null ){
buffer.append(line);
}
reader.close();
JsonParser parser = new JsonParser();
myRestData = (JsonObject) parser.parse(buffer.toString());
return myRestData;
}catch( MalformedURLException e ){
e.printStackTrace();
myRestData.addProperty("error", e.toString());
return myRestData;
}catch( IOException e ){
e.printStackTrace();
myRestData.addProperty("error", e.toString());
return myRestData;
}
}
/**
* Uses the Apache Commons codec binary Base64 package for encoding
* of credentials, so none transmit 'in the open'.
*
* @return String of credentials for use with authenticated REST source
*/
private String GetMyCredentials () {
String rawUser = "SomeUsername";
String rawPass = "SomePassword12345";
String rawCred = rawUser+":"+rawPass;
String myCred = Base64.encodeBase64String(rawCred.getBytes());
return "Basic "+myCred;
}
/**
* @param some parameters from which you build your URL source
* @return String of the properly built URL of the source REST data
*/
private String BuildMyURL( String param ) {
//the string this returns is
String base = "https://";
String srv = "my.company.com";
//String port = ":443";
String pth = "/api/data/collections/name/ViewName";
return base+srv+pth+"?"+param;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment