Example #1
0
 /**
  * * sendResponse checks for the mode of the server and sends a Response object to the client with
  * the appropriate string stored and the client's Cookie.
  *
  * @param clientCookie - Cookie object received by client
  * @param responseToClient - Response object that will be sent to client
  * @param outputObject - Used to write serialized version of object back to client
  * @throws IOException - Thrown if outputObject is interrupted while processing or fails to
  *     process
  */
 static void sendResponse(
     Cookie clientCookie, Response responseToClient, ObjectOutputStream outputObject)
     throws IOException {
   if (mode.getMode() == ServerMode.JOKE) { // If the mode of the server is set to JOKE, send joke
     responseToClient.addResponse(
         joke.say(
             clientCookie
                 .getJokeKey())); // gets joke from Database and stores string in responseToClient
     clientCookie.nextJoke(); // clientCookie increments the index of the joke to be accessed later
     responseToClient.setCookie(clientCookie); // stores clientCookie in responseToClient
     System.out.println("Sending joke response..."); // notify server joke is being sent
     outputObject.writeObject(
         responseToClient); // send a serialized version of Response object to client
   } else if (mode.getMode()
       == ServerMode.PROVERB) { // If the mode of the server is set to PROVERB, send proverb
     responseToClient.addResponse(proverb.say(clientCookie.getProverbKey()));
     clientCookie.nextProverb();
     responseToClient.setCookie(clientCookie);
     System.out.println("Sending proverb response...");
     outputObject.writeObject(
         responseToClient); // send Response object with proverb and client's Cookie to the client
   } else if (mode.getMode()
       == ServerMode
           .MAINTENANCE) { // If the mode of the server is set to MAINTENANCE, notify clients
                           // server is down for maintenance
     responseToClient.addResponse("Joke server temporarily down for maintenance.\n");
     responseToClient.setCookie(clientCookie);
     System.out.println("Sending maintenance response...");
     outputObject.writeObject(
         responseToClient); // send Response object with maintenance message and client's Cookie to
                            // the client
   }
 }
Example #2
0
 // getBrowserInfiniteCookie
 public static String getBrowserInfiniteCookie(HttpServletRequest request) {
   Cookie[] cookieJar = request.getCookies();
   if (cookieJar != null) {
     for (Cookie cookie : cookieJar) {
       if (cookie.getName().equals("infinitecookie")) {
         return cookie.getValue() + ";";
       }
     }
   }
   return null;
 } // TESTED
Example #3
0
  private void checkHeaders(HTTPMethod method) {
    if (debugHeaders) {
      System.out.println("\nOpenConnection Headers for " + method.getPath());
      System.out.println("Status Line: " + method.getStatusLine());
    }

    Header[] responseHeaders = method.getResponseHeaders();
    for (int i1 = 0; i1 < responseHeaders.length; i1++) {
      Header responseHeader = responseHeaders[i1];
      if (debugHeaders) System.out.print("  " + responseHeader);
      String key = responseHeader.getName();
      String value = responseHeader.getValue();

      if (key.equals("Last-Modified")) {
        lastModified = value;
        if (debugHeaders) System.out.println(" **found lastModified = " + lastModified);

      } else if (key.equals("X-Last-Extended")) {
        lastExtended = value;
        if (debugHeaders) System.out.println(" **found lastExtended = " + lastExtended);

      } else if (key.equals("X-Last-Modified-Invalid")) {
        lastModifiedInvalid = value;
        if (debugHeaders)
          System.out.println(" **found lastModifiedInvalid = " + lastModifiedInvalid);
      }
    }

    if (debugHeaders) System.out.println("OpenConnection Headers for " + method.getPath());

    Cookie[] cookies = _session.getCookies();

    if (cookies.length > 0) {
      if (debugHeaders) System.out.println("Cookies= ");

      for (int i = 0; i < cookies.length; i++) {
        Cookie cooky = cookies[i];
        if (debugHeaders) System.out.println("  " + cooky);
        if (cooky.getName().equalsIgnoreCase("jsessionid")) hasSession = true;
      }
    }
  }
Example #4
0
  /**
   * Binds the annotated variables.
   *
   * @param http http context
   * @param arg argument array
   * @throws QueryException query exception
   * @throws IOException I/O exception
   */
  void bind(final HTTPContext http, final Expr[] arg) throws QueryException, IOException {
    // bind variables from segments
    for (int s = 0; s < path.size; s++) {
      final Matcher m = TEMPLATE.matcher(path.segment[s]);
      if (!m.find()) continue;
      final QNm qnm = new QNm(token(m.group(1)), context);
      bind(qnm, arg, new Atm(http.segment(s)));
    }

    // cache request body
    final String ct = http.contentType();
    IOContent body = null;

    if (requestBody != null) {
      body = cache(http, null);
      try {
        // bind request body in the correct format
        body.name(http.method + IO.XMLSUFFIX);
        bind(requestBody, arg, Parser.item(body, context.context.prop, ct));
      } catch (final IOException ex) {
        error(INPUT_CONV, ex);
      }
    }

    // bind query parameters
    final Map<String, String[]> params = http.params();
    for (final RestXqParam rxp : queryParams) bind(rxp, arg, params.get(rxp.key));

    // bind form parameters
    if (!formParams.isEmpty()) {
      if (MimeTypes.APP_FORM.equals(ct)) {
        // convert parameters encoded in a form
        body = cache(http, body);
        addParams(body.toString(), params);
      }
      for (final RestXqParam rxp : formParams) bind(rxp, arg, params.get(rxp.key));
    }

    // bind header parameters
    for (final RestXqParam rxp : headerParams) {
      final StringList sl = new StringList();
      final Enumeration<?> en = http.req.getHeaders(rxp.key);
      while (en.hasMoreElements()) {
        for (final String s : en.nextElement().toString().split(", *")) sl.add(s);
      }
      bind(rxp, arg, sl.toArray());
    }

    // bind cookie parameters
    final Cookie[] ck = http.req.getCookies();
    for (final RestXqParam rxp : cookieParams) {
      String v = null;
      if (ck != null) {
        for (final Cookie c : ck) {
          if (rxp.key.equals(c.getName())) v = c.getValue();
        }
      }
      if (v == null) bind(rxp, arg);
      else bind(rxp, arg, v);
    }
  }