001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package edu.yale.its.tp.cas.servlet;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020:
021: import javax.servlet.ServletConfig;
022: import javax.servlet.ServletContext;
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServlet;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import edu.yale.its.tp.cas.ticket.GrantorCache;
029: import edu.yale.its.tp.cas.ticket.ProxyGrantingTicket;
030: import edu.yale.its.tp.cas.ticket.ProxyTicket;
031: import edu.yale.its.tp.cas.ticket.ServiceTicketCache;
032:
033: /**
034: * Handles PT acquisition.
035: */
036: public class Proxy extends HttpServlet {
037:
038: // *********************************************************************
039: // KFSConstants
040:
041: private static final String INVALID_REQUEST = "INVALID_REQUEST";
042: private static final String BAD_PGT = "BAD_PGT";
043: private static final String INTERNAL_ERROR = "INTERNAL_ERROR";
044:
045: // *********************************************************************
046: // Private state
047:
048: private GrantorCache pgtCache;
049: private ServiceTicketCache ptCache;
050:
051: // *********************************************************************
052: // Initialization
053:
054: public void init(ServletConfig config) throws ServletException {
055: // retrieve the context and the caches
056: ServletContext app = config.getServletContext();
057: pgtCache = (GrantorCache) app.getAttribute("pgtCache");
058: ptCache = (ServiceTicketCache) app.getAttribute("ptCache");
059: }
060:
061: // *********************************************************************
062: // Request handling
063:
064: public void doGet(HttpServletRequest request,
065: HttpServletResponse response) throws ServletException,
066: IOException {
067:
068: PrintWriter out = null;
069: try {
070: out = response.getWriter();
071: if (request.getParameter("pgt") == null
072: || request.getParameter("targetService") == null) {
073: proxyFailure(out, INVALID_REQUEST,
074: "'pgt' and 'targetService' parameters are both required");
075: } else {
076: String pgtId = request.getParameter("pgt");
077: String targetService = request
078: .getParameter("targetService");
079: ProxyGrantingTicket pgt = (ProxyGrantingTicket) pgtCache
080: .getTicket(pgtId);
081: if (pgt == null)
082: proxyFailure(out, BAD_PGT, "unrecognized pgt: '"
083: + pgtId + "'");
084: else {
085: ProxyTicket pt = new ProxyTicket(pgt, targetService);
086: String token = ptCache.addTicket(pt);
087: proxySuccess(out, token);
088: }
089: }
090: } catch (Exception ex) {
091: try {
092: if (out != null)
093: proxyFailure(out, INTERNAL_ERROR,
094: "Unexpected exception");
095: } catch (IOException ignoredEx) {
096: // ignore
097: }
098: }
099: }
100:
101: /** Sends a proxy failure message to the given PrintWriter. */
102: protected void proxyFailure(PrintWriter out, String code,
103: String errorMessage) throws IOException {
104: out
105: .println("<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>");
106: out.println(" <cas:proxyFailure code='" + code + "'>");
107: out.println(" " + errorMessage);
108: out.println(" </cas:proxyFailure>");
109: out.println("</cas:serviceResponse>");
110: }
111:
112: /** Sends a validation success message to the given PrintWriter. */
113: protected void proxySuccess(PrintWriter out, String proxyToken) {
114: out
115: .println("<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>");
116: out.println(" <cas:proxySuccess>");
117: out.println(" <cas:proxyTicket>" + proxyToken
118: + "</cas:proxyTicket>");
119: out.println(" </cas:proxySuccess>");
120: out.println("</cas:serviceResponse>");
121: }
122:
123: }
|