added appropriate package into to header
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / hs / HandleServlet.java
1 package edu.internet2.middleware.shibboleth.hs
2
3 import java.io.*;
4 import java.text.*;
5 import java.util.*;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8 import edu.internet2.middleware.shibboleth.*;
9 import edu.internet2.middleware.shibboleth.common.*;
10 import org.opensaml.*;
11
12 public class HandleServlet extends HttpServlet {
13
14     private HandleRepositoryFactory hrf;
15     private long ticketExp; 
16     private HandleServiceSAML hsSAML;
17
18     public void init(ServletConfig conf)
19         throws ServletException
20     {
21         super.init(conf);
22         getInitParams();
23
24         try {
25             hsSAML = new HandleServiceSAML( getInitParameter("domain"), 
26                                             getInitParameter("AAurl"),
27                                             getInitParameter("issuer") );
28             hrf = HandleRepositoryFactory.getInstance
29                 ( Constants.POLICY_CLUBSHIB, this );
30         }
31         catch (SAMLException ex) {
32             throw new ServletException( "Error initializing SAML libraries: " + ex );
33         }
34         catch (HandleException ex) {
35             throw new ServletException( "Error initializing Handle Service: " +ex );
36         }
37         if (hsSAML == null) {
38             throw new ServletException( "Error initializing SAML libraries: No Profile created." );
39         }  
40     }
41     
42     private void getInitParams() throws ServletException {
43
44         String ticket = getInitParameter("ticket");
45         if (ticket == null) {
46             ticket = "1400000";
47         }
48         ticketExp = Long.parseLong(ticket);
49         if ( getInitParameter("domain") == null || 
50              getInitParameter("domain").equals("")) {
51             throw new ServletException("Cannot find host domain in init parameters");
52         }
53         if ( getInitParameter("AAurl") == null || 
54              getInitParameter("AAurl").equals("")) {
55             throw new ServletException("Cannot find host Attribute Authority location in init parameters");
56         }
57         
58
59     }
60
61     public void doGet(HttpServletRequest req, 
62                       HttpServletResponse res)
63         throws ServletException, IOException
64     {
65
66
67
68         HandleEntry he = null;
69
70         try {
71             checkRequestParams(req);
72
73             req.setAttribute("shire", req.getParameter("shire"));
74             req.setAttribute("target", req.getParameter("target"));
75
76             he = new HandleEntry( req.getRemoteUser(), req.getAuthType(), 
77                                   ticketExp );
78             hrf.insertHandleEntry( he );
79             
80             byte[] buf = hsSAML.prepare
81                 ( he.getHandle(), req.getParameter("shire"), 
82                   req.getRemoteAddr(), he.getAuthType(), 
83                   new Date(he.getAuthInstant()));
84
85             createForm( req, res, buf );
86         }
87         catch (HandleException ex) {
88             handleError( req, res, ex );
89         }
90
91     }
92     
93     private void createForm( HttpServletRequest req, 
94                              HttpServletResponse res,
95                              byte[] buf )  
96         throws HandleException {
97         try {
98
99             res.setContentType("text/html");
100             PrintWriter out = res.getWriter();
101             out.println("<HTML><HEAD><TITLE>Handle Service</TITLE></HEAD>");
102             out.println("<BODY onLoad=\"document.forms[0].submit()\">");
103             out.println("<p><form name=\"shib\" " + "action=\"" +
104                         req.getParameter("shire")+"\" method=\"POST\">");
105             out.println("<input type=\"hidden\" name=\"TARGET\"" +
106                         " value=\"" + req.getParameter("target") + "\">");
107             out.println("<input type=\"hidden\" name=\"SAMLAssertion\""+
108                         "value=\"" + buf + "\">");
109             out.println("<input type=\"submit\" value=\"Transmit\">");
110             out.println("</form>");
111             
112             /**
113              * soon to implement forwarding to hs.jsp for submission
114              * 
115             //Hardcoded to ASCII to ensure Base64 encoding compatibility
116             req.setAttribute("assertion", new String(buf, "ASCII"));
117             RequestDispatcher rd = req.getRequestDispatcher("/hs.jsp");
118             rd.forward(req, res);
119             */
120         } catch (IOException ex) {
121             throw new HandleException
122                 ("IO interruption while displaying Handle Service UI." + ex);
123         } 
124         /*
125           catch (ServletException ex) {
126             throw new HandleException
127                 ("Problem displaying Handle Service UI." + ex);
128         }
129         */
130     }
131
132     private void handleError( HttpServletRequest req, 
133                              HttpServletResponse res,
134                              Exception e )  
135         throws ServletException, IOException {
136
137         req.setAttribute("errorText", e.toString());
138         //      req.setAttribute("requestURL", req.getRequestURL().toString());
139         RequestDispatcher rd = req.getRequestDispatcher("/hserror.jsp");
140         
141         rd.forward(req, res);
142         
143     }
144
145                      
146     private void checkRequestParams( HttpServletRequest req )
147         throws HandleException {
148
149         if ( req.getParameter("target") == null 
150              || req.getParameter("target").equals("")) {
151             throw new HandleException("Invalid data from SHIRE: no target URL received.");
152         }
153         if ((req.getParameter("shire") == null)
154             || (req.getParameter("shire").equals(""))) {
155             throw new HandleException("Invalid data from SHIRE: No acceptance URL received.");
156         }
157         if ((req.getRemoteUser() == null)
158             || (req.getRemoteUser().equals(""))) {
159             throw new HandleException("Unable to authenticate remote user");
160         }
161         if ((req.getAuthType() == null) || (req.getAuthType().equals(""))) {
162             throw new HandleException("Unable to obtain authentication type of user.");
163         }
164         if ((req.getRemoteAddr() == null)
165             || (req.getRemoteAddr().equals(""))) {
166             throw new HandleException("Unable to obtain client address.");
167         }    
168     }
169 }
170     
171