PLLNotificationServlet.java revision 8af80418ba1ec431c8027fa9668e5678658d3611
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: PLLNotificationServlet.java,v 1.6 2008/08/19 19:08:43 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Portions Copyrighted [2011] [ForgeRock AS]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.iplanet.services.comm.client;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.comm.share.NotificationSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.comm.share.PLLBundle;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.ISLocaleContext;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.RequestUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.locale.L10NMessageImpl;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.InputStream;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Vector;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.ServletException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.ServletOutputStream;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.HttpServlet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.HttpServletRequest;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.HttpServletResponse;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The <code>PLLNotificationServlet</code> class is used to receive
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * notifications from servers and forward those notifications to the high level
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * services and applications for processing.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * </p>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The doPost() method gets the XML NotificationSet document from the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * HttpRequest object, then parses the XML documnent and reconstructs a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * NotificationSet object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @see com.iplanet.services.comm.share.Notification
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @see com.iplanet.services.comm.share.NotificationSet
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class PLLNotificationServlet extends HttpServlet {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void init() throws ServletException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Accepts POST requests, reads Inpt Stream, forwards the NotificationSet
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * XML Flushes the ResponseSet XML to OutputStream @param
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * HttpServletNotification Reference to HttpServletNotification object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param HttpServletResponse Reference to HttpServletResponse object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @see javax.servlet.http.HttpServlet
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void doPost(HttpServletRequest request, HttpServletResponse response)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws ServletException, java.io.IOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check content length
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RequestUtils.checkContentLength(request);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (L10NMessageImpl e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ISLocaleContext localeContext = new ISLocaleContext();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster localeContext.setLocale(request);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster java.util.Locale locale = localeContext.getLocale();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServletException(e.getL10NMessage(locale));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int length = request.getContentLength();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (length == -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServletException(PLLBundle.getString("unknownLength"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster byte[] reqData = new byte[length];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster InputStream in = request.getInputStream();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int rlength = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int offset = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (rlength != length) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int r = in.read(reqData, offset, length - offset);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (r == -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServletException(PLLBundle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString("readRequestError"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rlength += r;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster offset += r;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String xml = new String(reqData, 0, length, "UTF-8");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServletOutputStream out = response.getOutputStream();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster handleNotification(xml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.print("OK");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (ServletException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.print("NOT OK");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.flush();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (out != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.close();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void doGet(HttpServletRequest request, HttpServletResponse response)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws ServletException, java.io.IOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServletOutputStream out = response.getOutputStream();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.print("OpenSSO");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.flush();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This methid is used by doPost method. It gets the corresponding
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * notification handler and passes the Notification objects to it for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * processing. @param notificationXML The XML String for the NotificationSet
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void handleNotification(String notificationXML)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws ServletException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NotificationSet set = NotificationSet.parseXML(notificationXML);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Vector nots = set.getNotifications();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!nots.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Each notification in this set shall have the same service id
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceid = set.getServiceID();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the notification handler ...
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NotificationHandler ns = PLLClient
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getNotificationHandler(serviceid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ns == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServletException(PLLBundle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString("noNotificationHandler")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + serviceid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ns.process(nots);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}