View Javadoc

1   package org.apache.jetspeed.portlet;
2   
3   import java.io.IOException;
4   import java.io.InputStreamReader;
5   import java.io.Reader;
6   import java.util.Map;
7   
8   import javax.portlet.PortletException;
9   import javax.portlet.RenderRequest;
10  import javax.portlet.RenderResponse;
11  
12  import org.apache.commons.httpclient.Header;
13  import org.apache.commons.httpclient.HttpClient;
14  import org.apache.commons.httpclient.HttpMethodBase;
15  import org.apache.commons.httpclient.methods.PostMethod;
16  import org.apache.jetspeed.rewriter.ParserAdaptor;
17  import org.apache.jetspeed.rewriter.RewriterException;
18  import org.apache.jetspeed.rewriter.TicketParamRewriter;
19  import org.apache.jetspeed.rewriter.html.SwingParserAdaptor;
20  
21  public class SSOTicketPortlet extends SSOWebContentPortlet 
22  {
23      public final static String SSO_PREF_TICKET_NAME = "ticket.name";
24      // sso.type
25  	protected Class adaptorHtmlClass = SwingParserAdaptor.class;
26          
27      //form Constants
28      public static final String FORM_POST_METHOD = "post";
29      public static final String FORM_GET_METHOD = "get";
30      public static final String FORM_MULTIPART_METHOD = "multipart";
31      
32      protected HttpMethodBase getHttpMethod(HttpClient client, String uri, Map params, String formMethod, RenderRequest request) throws IOException
33      {
34          String postURI = (String)request.getPreferences().getValue(SSO_TYPE_FORM_ACTION_URL, "");
35          String ticketName = (String)request.getPreferences().getValue(SSO_PREF_TICKET_NAME, "");        
36      	if(uri.startsWith(postURI))
37          {
38              if(!params.containsKey(ticketName))
39              {
40              	params.put(ticketName, new String[]
41                                          { requestTicket(uri, request, null) });
42              }
43          }        
44          return super.getHttpMethod(client, uri, params, formMethod, request);
45      }
46  
47      private String requestTicket(String url, RenderRequest request, RenderResponse response)
48      {
49          // ...set up URL and HttpClient stuff
50      	String ticket = "";
51      	HttpClient client = new HttpClient();
52          HttpMethodBase httpMethod = null;
53          httpMethod = new PostMethod();
54          //String useragentProperty = request.getProperty("User-Agent");
55          httpMethod.addRequestHeader( "User-Agent", "Firefox" );
56          httpMethod.setPath(url);
57          try
58          {
59              client.executeMethod(httpMethod);
60              int responseCode  = httpMethod.getStatusCode();
61              if (responseCode >= 300 && responseCode <= 399)
62              {
63                  // redirection that could not be handled automatically!!! (probably from a POST)
64                  Header locationHeader = httpMethod.getResponseHeader("location");
65                  String redirectLocation = locationHeader != null ? locationHeader.getValue() : null ;
66                  if (redirectLocation != null)
67                  {
68                      // System.out.println("WebContentPortlet.doHttpWebContent() >>>handling redirect to: "+redirectLocation+"<<<");                    
69                      // one more time (assume most params are already encoded & new URL is using GET protocol!)
70                      return requestTicket( redirectLocation,null,null) ;
71                  }
72                  else
73                  {
74                      // The response is a redirect, but did not provide the new location for the resource.
75                      throw new PortletException("Redirection code: "+responseCode+", but with no redirectionLocation set.");
76                  }
77              }
78              else if (responseCode == 200)
79              {        	
80      //        	String body = httpMethod.getResponseBodyAsString();
81      //        	Header [] head =  httpMethod.getResponseHeaders();
82              	TicketParamRewriter ticketWriter =  new TicketParamRewriter();
83                  String ticketName = (String)request.getPreferences().getValue(SSO_PREF_TICKET_NAME, null);
84                  if (ticketName != null)
85                  {
86                      ticketWriter.setTicketName(ticketName);
87                      Reader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream());
88                      createParserAdaptor().parse(ticketWriter, reader);
89                      ticket = ticketWriter.getTicket();
90                  }
91              }
92          }
93          catch (Exception e) 
94          {
95  			e.printStackTrace();
96  		}	
97      	return ticket;
98      }
99  
100     public ParserAdaptor createParserAdaptor() throws RewriterException
101     {
102         try
103         {
104                 return (ParserAdaptor) adaptorHtmlClass.newInstance();
105          
106         }
107         catch (Exception e)
108         {
109             log.error("Error creating rewriter class", e);
110         }
111         return null;
112     }    
113 }