Esempio n. 1
0
  /* process the request */
  private Response processRequest(
      Request req, HttpServletRequest servletReq, HttpServletResponse servletRes) {

    // this call is to create a http session so that the JSESSIONID cookie
    // is created. The appserver(8.1) load balancer plugin relies on the
    // JSESSIONID cookie to set its JROUTE sticky cookie.
    debug.message("=======================Entering processRequest");
    servletReq.getSession(true);

    String content = req.getContent();
    AuthXMLResponse authResponse = null;

    // Check for mis-routed requests
    String cookieURL = null;
    int index = content.indexOf(AuthXMLTags.AUTH_ID_HANDLE);
    if (index != -1) {
      // Check for mis-routed requests, get server URL for
      // AuthIdentifier
      int beginIndex = content.indexOf('"', index);
      int endIndex = content.indexOf('"', beginIndex + 1);
      String authIdentifier = content.substring(beginIndex + 1, endIndex);
      if (debug.messageEnabled()) {
        debug.message(
            "authIdentifier = "
                + authIdentifier
                + "beginIndex = "
                + beginIndex
                + "endIndex ="
                + endIndex);
      }
      if (!authIdentifier.equals("0")) {
        try {
          SessionID sessionID = new SessionID(authIdentifier);
          URL sessionServerURL = SESSION_SERVICE_URL_SERVICE.getSessionServiceURL(sessionID);
          StringBuilder srtBuff = new StringBuilder(100);
          srtBuff
              .append(sessionServerURL.getProtocol())
              .append("://")
              .append(sessionServerURL.getHost())
              .append(":")
              .append(Integer.toString(sessionServerURL.getPort()))
              .append(serviceURI);
          cookieURL = srtBuff.toString();
        } catch (Exception exp) {
          debug.error("Error in getting URL from session", exp);
          cookieURL = null;
        }
      }
    }

    if ((cookieURL != null)
        && (cookieURL.trim().length() != 0)
        && !(AuthUtils.isLocalServer(cookieURL, serviceURI))) {
      // Routing to the correct server, the looks like a mis-routed
      // requested.
      HashMap cookieTable = new HashMap();
      Map headers = new HashMap();
      Enumeration headerNames = servletReq.getHeaderNames();
      while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        List headerValues = new ArrayList();
        Enumeration enum1 = servletReq.getHeaders(headerName);
        while (enum1.hasMoreElements()) {
          headerValues.add(enum1.nextElement());
        }
        headers.put(headerName, headerValues);
      }
      if (debug.messageEnabled()) {
        debug.message("Headers: " + headers);
      }
      PLLClient.parseCookies(headers, cookieTable);
      if (debug.messageEnabled()) {
        debug.message("Cookies: " + cookieTable);
      }
      RequestSet set = new RequestSet(AuthXMLTags.AUTH_SERVICE);
      set.addRequest(req);
      try {
        Vector responses = PLLClient.send(new URL(cookieURL), set, cookieTable);
        if (!responses.isEmpty()) {
          debug.message("=====================Returning redirected");
          return ((Response) responses.elementAt(0));
        }
      } catch (Exception e) {
        debug.error("Error in misrouted ", e);
        // Attempt to contact server failed
        authResponse = new AuthXMLResponse(AuthXMLRequest.NewAuthContext);
        setErrorCode(authResponse, e);
        return new Response(authResponse.toXMLString());
      }
    }

    // Either local request or new request, handle it locally
    try {
      AuthXMLRequest sreq = AuthXMLRequest.parseXML(content, servletReq);
      sreq.setHttpServletRequest(servletReq);
      authResponse = processAuthXMLRequest(content, sreq, servletReq, servletRes);
    } catch (AuthException e) {
      debug.error("Got Auth Exception", e);
      authResponse = new AuthXMLResponse(AuthXMLRequest.NewAuthContext);
      authResponse.setErrorCode(e.getErrorCode());
    } catch (Exception ex) {
      debug.error("Error while processing xml request", ex);
      authResponse = new AuthXMLResponse(AuthXMLRequest.NewAuthContext);
      setErrorCode(authResponse, ex);
    }
    debug.message("=======================Returning");
    return new Response(authResponse.toXMLString());
  }