/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egiz.pdfas.web.servlets; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import at.gv.egiz.pdfas.api.commons.SignatureInformation; import at.gv.egiz.pdfas.api.io.DataSource; import at.gv.egiz.pdfas.api.verify.VerifyResult; import at.gv.egiz.pdfas.exceptions.web.SessionExpiredException; import at.gv.egiz.pdfas.web.helper.SessionHelper; import at.gv.egiz.pdfas.web.session.SignSessionInformation; import at.gv.egiz.pdfas.web.session.VerifySessionInformation; /** * Retrieves the Signature Data from the session and returns it. * * @author wprinz */ public class RetrieveSignatureDataServlet extends HttpServlet { /** * SVUID. */ private static final long serialVersionUID = -5387006236836240538L; /** * The log. */ private static Log log = LogFactory.getLog(RetrieveSignatureDataServlet.class); protected void dispatch(HttpServletRequest request, HttpServletResponse response, String resource) throws ServletException, IOException { dispatch(request, response, resource, getServletContext()); } protected static void dispatch(HttpServletRequest request, HttpServletResponse response, String resource, ServletContext context) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); RequestDispatcher disp = context.getRequestDispatcher(resource); disp.forward(request, response); } /** * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (log.isDebugEnabled()) { log.debug("Request for receiving signature data."); //$NON-NLS-1$ log.debug("Requested session id is = " + request.getRequestedSessionId()); } try { Object sessionObject = SessionHelper.getSession(request); if (sessionObject instanceof SignSessionInformation) { SignSessionInformation si = (SignSessionInformation) sessionObject; processSign(request, response, si); } else { VerifySessionInformation si = (VerifySessionInformation) sessionObject; processVerify(request, response, si); } } catch (SessionExpiredException e) { log.error(e.getMessage(), e); SignServlet.prepareDispatchToErrorPage(e, request); dispatch(request, response, "/jsp/error.jsp"); } } protected void processSign(HttpServletRequest request, HttpServletResponse response, SignSessionInformation si) throws ServletException, IOException { writeSignatureData(si.sdi.getSignatureData(), response); } protected void processVerify(HttpServletRequest request, HttpServletResponse response, VerifySessionInformation si) throws ServletException, IOException { DataSource data = null; if (si.currentLocalOperation != null) { data = ((SignatureInformation) si.currentLocalOperation.signaturesToBeverified.get(si.currentLocalOperation.current_operation)).getSignedData(); } else { data = ((VerifyResult)si.verifyResults.getResults().get(0)).getSignedData(); } writeSignatureData(data, response); } protected void writeSignatureData(DataSource ds, HttpServletResponse response) throws IOException { log.trace("Writing " + ds.getMimeType() + " data:"); //$NON-NLS-1$ //$NON-NLS-2$ response.setContentType(ds.getMimeType()); response.setCharacterEncoding(ds.getCharacterEncoding()); // [tknall] content length must be set, otherwise ITS BKU for Mac rejects the request. response.setContentLength(ds.getLength()); IOUtils.copy(ds.createInputStream(), response.getOutputStream()); log.trace("Writing SignatureData finished."); //$NON-NLS-1$ } }