001: package org.apache.jetspeed.portlet;
002:
003: import java.io.IOException;
004: import java.io.InputStreamReader;
005: import java.io.Reader;
006: import java.util.Map;
007:
008: import javax.portlet.PortletException;
009: import javax.portlet.RenderRequest;
010: import javax.portlet.RenderResponse;
011:
012: import org.apache.commons.httpclient.Header;
013: import org.apache.commons.httpclient.HttpClient;
014: import org.apache.commons.httpclient.HttpMethodBase;
015: import org.apache.commons.httpclient.methods.PostMethod;
016: import org.apache.jetspeed.rewriter.ParserAdaptor;
017: import org.apache.jetspeed.rewriter.RewriterException;
018: import org.apache.jetspeed.rewriter.TicketParamRewriter;
019: import org.apache.jetspeed.rewriter.html.SwingParserAdaptor;
020:
021: public class SSOTicketPortlet extends SSOWebContentPortlet {
022: public final static String SSO_PREF_TICKET_NAME = "ticket.name";
023: // sso.type
024: protected Class adaptorHtmlClass = SwingParserAdaptor.class;
025:
026: //form Constants
027: public static final String FORM_POST_METHOD = "post";
028: public static final String FORM_GET_METHOD = "get";
029: public static final String FORM_MULTIPART_METHOD = "multipart";
030:
031: protected HttpMethodBase getHttpMethod(HttpClient client,
032: String uri, Map params, String formMethod,
033: RenderRequest request) throws IOException {
034: String postURI = (String) request.getPreferences().getValue(
035: SSO_TYPE_FORM_ACTION_URL, "");
036: String ticketName = (String) request.getPreferences().getValue(
037: SSO_PREF_TICKET_NAME, "");
038: if (uri.startsWith(postURI)) {
039: if (!params.containsKey(ticketName)) {
040: params.put(ticketName, new String[] { requestTicket(
041: uri, request, null) });
042: }
043: }
044: return super .getHttpMethod(client, uri, params, formMethod,
045: request);
046: }
047:
048: private String requestTicket(String url, RenderRequest request,
049: RenderResponse response) {
050: // ...set up URL and HttpClient stuff
051: String ticket = "";
052: HttpClient client = new HttpClient();
053: HttpMethodBase httpMethod = null;
054: httpMethod = new PostMethod();
055: //String useragentProperty = request.getProperty("User-Agent");
056: httpMethod.addRequestHeader("User-Agent", "Firefox");
057: httpMethod.setPath(url);
058: try {
059: client.executeMethod(httpMethod);
060: int responseCode = httpMethod.getStatusCode();
061: if (responseCode >= 300 && responseCode <= 399) {
062: // redirection that could not be handled automatically!!! (probably from a POST)
063: Header locationHeader = httpMethod
064: .getResponseHeader("location");
065: String redirectLocation = locationHeader != null ? locationHeader
066: .getValue()
067: : null;
068: if (redirectLocation != null) {
069: // System.out.println("WebContentPortlet.doHttpWebContent() >>>handling redirect to: "+redirectLocation+"<<<");
070: // one more time (assume most params are already encoded & new URL is using GET protocol!)
071: return requestTicket(redirectLocation, null, null);
072: } else {
073: // The response is a redirect, but did not provide the new location for the resource.
074: throw new PortletException("Redirection code: "
075: + responseCode
076: + ", but with no redirectionLocation set.");
077: }
078: } else if (responseCode == 200) {
079: // String body = httpMethod.getResponseBodyAsString();
080: // Header [] head = httpMethod.getResponseHeaders();
081: TicketParamRewriter ticketWriter = new TicketParamRewriter();
082: String ticketName = (String) request.getPreferences()
083: .getValue(SSO_PREF_TICKET_NAME, null);
084: if (ticketName != null) {
085: ticketWriter.setTicketName(ticketName);
086: Reader reader = new InputStreamReader(httpMethod
087: .getResponseBodyAsStream());
088: createParserAdaptor().parse(ticketWriter, reader);
089: ticket = ticketWriter.getTicket();
090: }
091: }
092: } catch (Exception e) {
093: e.printStackTrace();
094: }
095: return ticket;
096: }
097:
098: public ParserAdaptor createParserAdaptor() throws RewriterException {
099: try {
100: return (ParserAdaptor) adaptorHtmlClass.newInstance();
101:
102: } catch (Exception e) {
103: log.error("Error creating rewriter class", e);
104: }
105: return null;
106: }
107: }
|