Created
March 5, 2013 17:08
-
-
Save tarraschk/5091919 to your computer and use it in GitHub Desktop.
Java - Play Framework - Leetchi API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import java.io.*; | |
import java.net.*; | |
import java.security.*; | |
import java.security.spec.*; | |
import sun.misc.BASE64Encoder; | |
import play.*; | |
import play.mvc.*; | |
public class Leetchi extends Controller{ | |
// Attributes for Leetchi | |
protected static String partnerId = "partnerid"; | |
// RSA KeyImport | |
public static PrivateKey getPrivateKey(String filename) throws Exception { | |
File f = new File(filename); | |
FileInputStream fis = new FileInputStream(f); | |
DataInputStream dis = new DataInputStream(fis); | |
byte[] keyBytes = new byte[(int)f.length()]; | |
dis.readFully(keyBytes); | |
dis.close(); | |
PKCS8EncodedKeySpec spec = | |
new PKCS8EncodedKeySpec(keyBytes); | |
KeyFactory kf = KeyFactory.getInstance("RSA"); | |
return kf.generatePrivate(spec); | |
} | |
public static PublicKey getPublicKey(String filename) throws Exception { | |
File f = new File(filename); | |
FileInputStream fis = new FileInputStream(f); | |
DataInputStream dis = new DataInputStream(fis); | |
byte[] keyBytes = new byte[(int)f.length()]; | |
dis.readFully(keyBytes); | |
dis.close(); | |
X509EncodedKeySpec spec = | |
new X509EncodedKeySpec(keyBytes); | |
KeyFactory kf = KeyFactory.getInstance("RSA"); | |
return kf.generatePublic(spec); | |
} | |
// Signature calculus | |
public static String computeSignature(String request) throws Exception { | |
Signature sig = Signature.getInstance("SHA1withRSA"); | |
sig.initSign(getPrivateKey("privatekey.pem")); | |
sig.update(request.getBytes("UTF8")); | |
byte[] signatureBytes = sig.sign(); | |
String signature = new BASE64Encoder().encode(signatureBytes); | |
return signature; | |
} | |
// Request transfer to external API | |
public static String sendRequest(String method, String urlPath, String body) throws Exception { | |
String baseUrl = "http://api-preprod.leetchi.com/v1/partner/"; | |
String url = baseUrl + partnerId + urlPath + "?ts=" + System.currentTimeMillis(); | |
String toBeEncrypted = method.toUpperCase() + "|" + url + "|"; | |
if (!method.equals("GET")) { | |
toBeEncrypted = toBeEncrypted + body+"|"; | |
} | |
String signature = computeSignature(toBeEncrypted); | |
URL targetUrl; | |
HttpURLConnection connection = null; | |
try { | |
//Create connection | |
targetUrl = new URL(url); | |
connection = (HttpURLConnection)targetUrl.openConnection(); | |
connection.setRequestMethod(method.toUpperCase()); | |
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
connection.setRequestProperty("X-Leetchi-Signature", signature); | |
connection.setRequestProperty("Content-Length", "" + Integer.toString(body.getBytes().length)); | |
connection.setUseCaches (false); | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
//Send request | |
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ()); | |
wr.writeBytes (body); | |
wr.flush (); | |
wr.close (); | |
//Get Response | |
InputStream is = connection.getInputStream(); | |
BufferedReader rd = new BufferedReader(new InputStreamReader(is)); | |
String line; | |
StringBuffer response = new StringBuffer(); | |
while((line = rd.readLine()) != null) { | |
response.append(line); | |
response.append('\r'); | |
} | |
rd.close(); | |
return response.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} finally { | |
if(connection != null) { | |
connection.disconnect(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment