If you're using Java/JSP your best bet may be to create some
Custom Tag Libs.
For our example we're creating two, one to insert the
client application and another to request the
user generated content. The
login call will be contained in a helper class used from a servlet/controller layer.
This is what user generated content tag looks like:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection;
import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport;
public class UserGenContentTag extends TagSupport {
private String developerKey;
private String contentNamespace;
private String cellId;
public UserGenContentTag(){}
public int doStartTag() throws JspException {
return super.doStartTag();
}
public int doEndTag() throws JspException {
try {
super.pageContext.getOut().write( getContent() );
} catch (IOException e) {
//log exception
}
return super.doEndTag();
}
private String getContent() {
StringBuilder htmlBuilder = new StringBuilder();
try {
// Construct url
String urlString = "http://wapi.wetpaint.com/CellService/" +
"getCell.do?key=" + developerKey + "&ns=" + contentNamespace +
"&cell.cellId=" + cellId;
// Send data
URL url = new URL( urlString );
URLConnection conn = url.openConnection();
conn.setDoOutput( false );
// Get the response
BufferedReader rd = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
String line;
while ( ( line = rd.readLine() ) != null) {
htmlBuilder.append( line );
}
rd.close();
} catch ( Exception e ) {
//log exception
}
return htmlBuilder.toString();
}
public String getDeveloperKey() {
return developerKey;
}
//Insert getters and setters for properties
} |
As you can see, there's not much to it. All it does is make a request of the wetpaint api server for the cell of a particular id within a particular namespace and renders it onto a page. We will also need a tag to generate the javascript for the client application:
package com.wetpaint.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport;
public class ClientApplicationTag extends TagSupport {
private String developerKey; private String contentNamespace; private String ticket; private String wetpaintApiHost; public ClientApplicationTag(){} public int doStartTag() throws JspException {
return super.doStartTag();
}
public int doEndTag() throws JspException {
try {
super.pageContext.getOut().write( renderScript() );
} catch (IOException e) {
//log exception
}
return super.doEndTag();
} private String renderScript() {
String script = "\t<script src=\"" + wetpaintApiHost + "/JavaScriptService/getBootstrap.do?" + developerKey + "\" type=\"text/javascript\"></script>\r" +
"\t<script type=\'text/javascript\'>\r" +
"\t\tif( window.WPCAPI ) {\r" +
"\t\t\tWPCAPI.setLoginTicket('" + ticket + "');\r" +
"\t\t\tWPCAPI.setDeveloperKey('" + developerKey + "');\r" +
"\t\t\tWPCAPI.setNamespace('" + contentNamespace + "');\r" +
"\t\t}\r" +
"\t</script>\r";
return script;
}
//Insert getters and setters for properties
}
|
|
Here's an example of the logic needed to call login written in Java:
private static String getTicket( String serverUrl, PartnerCommand command ){
String ticket = null; StringBuilder htmlBuilder = new StringBuilder(); try {
long ts = Calendar.getInstance().getTime().getTime() / 1000L;
String sigString = command.getKey() + command.getUserId() + ts;
SecretKeySpec signingKey = new SecretKeySpec( "secret".getBytes( "UTF-8" ), "HmacSHA1" );
String sigHashString = null;
try {
Mac mac = Mac.getInstance( "HmacSHA1" );
mac.init( signingKey );
byte[] hash = mac.doFinal( sigString.getBytes( "UTF-8" ) );
sigHashString = new String( Hex.encodeHex( hash ) );
} catch ( NoSuchAlgorithmException e ) {
//throw custom exception
} catch ( InvalidKeyException e ) {
//throw custom exception
}
StringBuilder sbUrl = new StringBuilder();
sbUrl.append( serverUrl ) .append( "/UserService/login.do" );
StringBuilder sbData = new StringBuilder();
sbData.append("key=" )
.append( command.getKey() ) .append( "&ns=" ) .append( command.getNs() ) .append( "&cred.ts=" ) .append( ts ) .append( "&cred.sig=" ) .append( sigHashString ) .append( "&user.email=" ) .append( command.getUserId() ) .append( "@wetpaint.com") .append( "&user.userId=" ) .append( command.getUserId() ) .append( "&user.emailOptIn=false" ) .append( "&user.displayName=" ) .append( command.getDisplayName() ) .append( "&output=api" );
URL url = new URL( sbUrl.toString() ); URLConnection conn = url.openConnection(); conn.setDoOutput( true );
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); String data = sbData.toString(); wr.write( data ); wr.flush();
BufferedReader rd = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); String line; while ( ( line = rd.readLine() ) != null) { htmlBuilder.append( line );
} rd.close(); ticket = htmlBuilder.toString().split( "<ticket>" )[1].split( "</ticket>" )[0]; } catch ( Exception e ) {
// noop
} return ticket;
} |
Data for testing login is available
here.