Exemple #1
0
  /**
   * POST something to the given URL. The headers are put in as HTTP headers, the content length is
   * calculated and the content is sent as content. Duh.
   *
   * @param cs Client socket items.
   * @param url the url to post to
   * @param headers additional headers to send as HTTP headers
   * @param contentType type of the content
   * @param content the body of the post
   * @exception IOException if post fails
   */
  private static Response post(
      ClientSocket cs, URL url, Hashtable headers, String contentType, String content)
      throws IOException {

    // If the port is not specified in the URL, use the default http port.
    int port = url.getPort();
    if (port == -1) port = 80;

    cs.s = new Socket(url.getHost(), +port);
    cs.out = new PrintWriter(cs.s.getOutputStream());
    cs.in = new BufferedReader(new InputStreamReader(cs.s.getInputStream()));

    // Create the HTTP header
    cs.out.print(HTTP_POST + " " + url.getFile() + " HTTP/" + HTTP_VERSION + "\r\n");
    cs.out.print(HEADER_HOST + ": " + url.getHost() + ':' + port + "\r\n");
    cs.out.print(HEADER_CONTENT_TYPE + ": " + contentType + "\r\n");
    cs.out.print(HEADER_CONTENT_LENGTH + ": " + content.length() + "\r\n");

    for (Enumeration e = headers.keys(); e.hasMoreElements(); ) {
      Object key = e.nextElement();
      cs.out.print(key + ": " + headers.get(key) + "\r\n");
    }

    // According to HTTP1.1 need another CRLF after the header
    cs.out.print("\r\n");

    Debug.log(Debug.MSG_LIFECYCLE, "HTTPUtils: Writing content to the socket... " + content);

    cs.out.println(content);

    cs.out.flush();

    /* read the status line */
    int statusCode = 0;
    String statusString = null;
    StringTokenizer st = new StringTokenizer(cs.in.readLine());
    st.nextToken(); // ignore version part
    statusCode = Integer.parseInt(st.nextToken());

    StringBuffer sb = new StringBuffer();

    while (st.hasMoreTokens()) {
      sb.append(st.nextToken());
      if (st.hasMoreTokens()) sb.append(" ");
    }

    statusString = sb.toString();

    /* get the headers */
    Hashtable respHeaders = new Hashtable();
    int respContentLength = -1;
    String respContentType = null;
    String line = null;

    while ((line = cs.in.readLine()) != null) {

      if (line.length() == 0) break;

      int colonIndex = line.indexOf(':');
      String fieldName = line.substring(0, colonIndex);
      String fieldValue = line.substring(colonIndex + 1).trim();

      if (fieldName.equals(HEADER_CONTENT_LENGTH)) respContentLength = Integer.parseInt(fieldValue);
      else if (fieldName.equals(HEADER_CONTENT_TYPE)) respContentType = fieldValue;
      else respHeaders.put(fieldName, fieldValue);
    }

    Response response =
        new Response(
            statusCode, statusString, respHeaders, respContentLength, respContentType, cs.in);

    return response;
  }
Exemple #2
0
  /**
   * This gets a String to be split in the message object.It splits the string into substrings and
   * then stores them in the message object to be apssed to the next processors individually. This
   * class expects at least a single next processor hence it return a null if there are no next
   * processors.
   *
   * @param msgObj The MesageObject containg the string.
   * @return NVPair[] Name-Value pair array of next processor name and the MessageObject
   * @exception ProcessingException Thrown if processing fails
   */
  public NVPair[] process(MessageProcessorContext context, MessageObject msgObj)
      throws MessageException, ProcessingException {

    if (Debug.isLevelEnabled(Debug.BENCHMARK))
      Debug.log(Debug.BENCHMARK, "Reached Splitter process method..");

    // Debug.log(null, Debug.UNIT_TEST,"MESSAGE OBJECT RECEIVED:--->"+msgObj.describe());

    if (msgObj == null) {
      return null;
    } else if (toProcessorNames == null) {
      Debug.log(Debug.ALL_WARNINGS, "Splitter: No next processors available. Hence exiting.");
    } else if (!(msgObj.get() instanceof String)) {
      if (Debug.isLevelEnabled(Debug.UNIT_TEST))
        Debug.log(Debug.UNIT_TEST, "SPLITTER : UNIT_TEST: " + ((msgObj.get()).getClass()));

      throw new ProcessingException("ERROR: Splitter: " + "The msg Object must be a String.");
    }

    if (Debug.isLevelEnabled(Debug.UNIT_TEST))
      Debug.log(Debug.UNIT_TEST, "MESSAGE OBJECT RECEIVED:--->" + msgObj.describe());

    String batchedRecords = msgObj.getString();

    List singleRecord;
    String subStr = "";
    // to take care empty batch files that are valid files in some cases
    if (!StringUtils.hasValue(batchedRecords)) {
      Debug.log(Debug.ALL_WARNINGS, "MESSAGE CONTENT IS NULL");
      return null;
    }

    if (!StringUtils.hasValue(fileSeparator)) // if fileseparator is false...
    {
      if (batchedRecords.length() < splitLength) {
        throw new MessageException(
            "ERROR: Splitter: " + "Batched String length is less than " + splitLength + "bytes.");
      }

      float tempSize = (float) (batchedRecords.length()) / splitLength;
      int size = (batchedRecords.length()) / splitLength;

      // check for length of string
      if (size != tempSize) {
        throw new MessageException(
            "ERROR: Splitter: " + "Record received is not of correct length");
      }
      singleRecord = new Vector(size);
      int i = 0;
      for (i = 0; i < size; i++) {

        subStr = batchedRecords.substring(i * splitLength, (i + 1) * splitLength);
        singleRecord.add(i, subStr);
      }
    } else // if fileseparator is true...
    {
      singleRecord = new Vector();
      StringTokenizer st = new StringTokenizer(batchedRecords, fileSeparator);
      while (st.hasMoreTokens()) {
        subStr = st.nextToken();
        if (subStr.length() != splitLength) {
          throw new MessageException(
              "ERROR: Splitter: Record "
                  + "received after removing the fileseparator is not of correct length .");
        }
        singleRecord.add(subStr);

        if (Debug.isLevelEnabled(Debug.UNIT_TEST))
          Debug.log(
              Debug.UNIT_TEST,
              "\nDATA\n" + "********************************************" + subStr);
      }
    }

    if (getBoolean(truncHeaderFooter)) // if truncHeaderFooter is true...
    {
      singleRecord.remove((singleRecord.size() - 1));
      singleRecord.remove(0);
    }
    // else continue...
    int batchedSize = singleRecord.size();

    if (Debug.isLevelEnabled(Debug.UNIT_TEST))
      Debug.log(Debug.UNIT_TEST, "BATCHED SIZE--->" + batchedSize);

    int processorsNo = toProcessorNames.length;
    NVPair[] contents = new NVPair[batchedSize * processorsNo];
    int i = 0;

    for (i = 0; i < batchedSize; i++) {
      MessageObject mObj = new MessageObject();
      mObj.set(singleRecord.get(i));

      // Sending the message objects to the next processors.
      for (int j = 0; j < processorsNo; j++) {

        contents[(i * (processorsNo)) + j] = new NVPair(toProcessorNames[j], mObj);

        if (Debug.isLevelEnabled(Debug.UNIT_TEST))
          Debug.log(
              Debug.UNIT_TEST,
              "NEXT PROCESSOR-->"
                  + toProcessorNames[j]
                  + "\n"
                  + "MESSAGE OBJECT CONTENT------------>"
                  + mObj.describe());
      }
    }

    if (Debug.isLevelEnabled(Debug.UNIT_TEST))
      Debug.log(Debug.UNIT_TEST, "SPLITTER : CONTEXT---->" + context.describe());

    return contents;
  }