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 |
0 |
public class SSOTicketPortlet extends SSOWebContentPortlet |
22 |
|
{ |
23 |
|
public final static String SSO_PREF_TICKET_NAME = "ticket.name"; |
24 |
|
|
25 |
0 |
protected Class adaptorHtmlClass = SwingParserAdaptor.class; |
26 |
|
|
27 |
|
|
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 |
0 |
String postURI = (String)request.getPreferences().getValue(SSO_TYPE_FORM_ACTION_URL, ""); |
35 |
0 |
String ticketName = (String)request.getPreferences().getValue(SSO_PREF_TICKET_NAME, ""); |
36 |
0 |
if(uri.startsWith(postURI)) |
37 |
|
{ |
38 |
0 |
if(!params.containsKey(ticketName)) |
39 |
|
{ |
40 |
0 |
params.put(ticketName, new String[] |
41 |
|
{ requestTicket(uri, request, null) }); |
42 |
|
} |
43 |
|
} |
44 |
0 |
return super.getHttpMethod(client, uri, params, formMethod, request); |
45 |
|
} |
46 |
|
|
47 |
|
private String requestTicket(String url, RenderRequest request, RenderResponse response) |
48 |
|
{ |
49 |
|
|
50 |
0 |
String ticket = ""; |
51 |
0 |
HttpClient client = new HttpClient(); |
52 |
0 |
HttpMethodBase httpMethod = null; |
53 |
0 |
httpMethod = new PostMethod(); |
54 |
|
|
55 |
0 |
httpMethod.addRequestHeader( "User-Agent", "Firefox" ); |
56 |
0 |
httpMethod.setPath(url); |
57 |
|
try |
58 |
|
{ |
59 |
0 |
client.executeMethod(httpMethod); |
60 |
0 |
int responseCode = httpMethod.getStatusCode(); |
61 |
0 |
if (responseCode >= 300 && responseCode <= 399) |
62 |
|
{ |
63 |
|
|
64 |
0 |
Header locationHeader = httpMethod.getResponseHeader("location"); |
65 |
0 |
String redirectLocation = locationHeader != null ? locationHeader.getValue() : class="keyword">null ; |
66 |
0 |
if (redirectLocation != null) |
67 |
|
{ |
68 |
|
|
69 |
|
|
70 |
0 |
return requestTicket( redirectLocation,null,class="keyword">null) ; |
71 |
|
} |
72 |
|
else |
73 |
|
{ |
74 |
|
|
75 |
0 |
throw new PortletException("Redirection code: "+responseCode+", but with no redirectionLocation set."); |
76 |
|
} |
77 |
|
} |
78 |
0 |
else if (responseCode == 200) |
79 |
|
{ |
80 |
|
|
81 |
|
|
82 |
0 |
TicketParamRewriter ticketWriter = new TicketParamRewriter(); |
83 |
0 |
String ticketName = (String)request.getPreferences().getValue(SSO_PREF_TICKET_NAME, null); |
84 |
0 |
if (ticketName != null) |
85 |
|
{ |
86 |
0 |
ticketWriter.setTicketName(ticketName); |
87 |
0 |
Reader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream()); |
88 |
0 |
createParserAdaptor().parse(ticketWriter, reader); |
89 |
0 |
ticket = ticketWriter.getTicket(); |
90 |
|
} |
91 |
|
} |
92 |
|
} |
93 |
0 |
catch (Exception e) |
94 |
|
{ |
95 |
0 |
e.printStackTrace(); |
96 |
0 |
} |
97 |
0 |
return ticket; |
98 |
|
} |
99 |
|
|
100 |
|
public ParserAdaptor createParserAdaptor() throws RewriterException |
101 |
|
{ |
102 |
|
try |
103 |
|
{ |
104 |
0 |
return (ParserAdaptor) adaptorHtmlClass.newInstance(); |
105 |
|
|
106 |
|
} |
107 |
0 |
catch (Exception e) |
108 |
|
{ |
109 |
0 |
log.error("Error creating rewriter class", e); |
110 |
|
} |
111 |
0 |
return null; |
112 |
|
} |
113 |
|
} |