JSP Sample ApplicationThis is a featured page

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.


boe_wetpaint
boe_wetpaint
Latest page update: made by boe_wetpaint , Sep 2 2008, 6:01 PM EDT (about this update About This Update boe_wetpaint Modified to include getBootstrap GET key param - boe_wetpaint

5 words added
1 word deleted

view changes

- complete history)
More Info: links to this page
There are no threads for this page.  Be the first to start a new thread.

Related Content

  (what's this?Related ContentThanks to keyword tags, links to related pages and threads are added to the bottom of your pages. Up to 15 links are shown, determined by matching tags and by how recently the content was updated; keeping the most current at the top. Share your feedback on Wetpaint Central.)