Map<String, String> parseParams(String query) {
   ParameterParser pp = new ParameterParser();
   List params = pp.parse(query, '&');
   Map<String, String> result = new HashMap<String, String>();
   for (Iterator it = params.iterator(); it.hasNext(); ) {
     NameValuePair pair = (NameValuePair) it.next();
     result.put(pair.getName().toUpperCase(), pair.getValue());
   }
   return result;
 };
Example #2
0
 private String getHttpRequestBody(PostMethod method) {
   StringBuilder strBody = new StringBuilder();
   NameValuePair[] pairs = method.getParameters();
   for (NameValuePair pair : pairs) {
     String name = pair.getName();
     String value = pair.getValue();
     strBody.append(name + "=" + value + ";");
   }
   return strBody.toString();
 }
Example #3
0
  /**
   * parameter와 queryString 를 가져온다.
   *
   * @param method
   * @return
   */
  private String getHttpInfoDumy(HttpMethod method) {
    NameValuePair[] params = null;
    String methodType = "GET";
    String reqBody = null;
    if (method instanceof PostMethod) {
      params = ((PostMethod) method).getParameters();
      methodType = "POST";
      StringRequestEntity sre = (StringRequestEntity) ((PostMethod) method).getRequestEntity();
      reqBody = sre.getContent();
    }

    StringBuffer sb = new StringBuffer();

    try {
      sb.append("#### getHttpInfoDumy ####");
      sb.append("\n## " + methodType + " [" + method.getURI() + "], hscd[" + this.hashCode() + "]");
    } catch (URIException e) {
      sb.append("\n## getParamsQueryStr- URIException " + e.getMessage() + "]");
      return sb.toString();
    }

    if (method.getQueryString() != null && method.getQueryString().length() > 0)
      sb.append("\n" + "## queryString[" + method.getQueryString() + "]");

    if (params != null) {
      for (int i = 0; i < params.length; i++) {
        NameValuePair param = params[i];
        sb.append(
            "\n"
                + "## POST body param["
                + i
                + "], name["
                + param.getName()
                + "], value["
                + param.getValue()
                + "]");
      }
    }

    if (reqBody != null) {
      sb.append("\n" + "## POST body String [" + reqBody + "]");
    }

    sb.append("\n##########");

    return sb.toString();
  }
Example #4
0
  /**
   * setting the headers
   *
   * @param header -- the name value pair of the header to be set.
   */
  private void setHeaders() {

    if (headers != null) {
      headers.clear();
    }
    if (headerNameValuePair != null && headerNameValuePair.size() > 0) {

      Iterator<NameValuePair> mIterator = headerNameValuePair.iterator();

      while (mIterator.hasNext()) {

        NameValuePair nameValuePair = mIterator.next();
        headers.add(new Header(nameValuePair.getName(), nameValuePair.getValue()));
        nameValuePair = null;
      }
      mIterator = null;
    }
  }
  /**
   * 将NameValuePairs数组转变为字符串
   *
   * @param nameValues
   * @return
   */
  protected String toString(NameValuePair[] nameValues) {
    if (nameValues == null || nameValues.length == 0) {
      return "null";
    }

    StringBuffer buffer = new StringBuffer();

    for (int i = 0; i < nameValues.length; i++) {
      NameValuePair nameValue = nameValues[i];

      if (i == 0) {
        buffer.append(nameValue.getName() + "=" + nameValue.getValue());
      } else {
        buffer.append("&" + nameValue.getName() + "=" + nameValue.getValue());
      }
    }

    return buffer.toString();
  }
Example #6
0
  public HttpMessageAdapter(Object message) {
    if (message instanceof Object[]) {
      // This case comes from the HttpMessageReceiver...
      Object[] messageParts = (Object[]) message;

      this.message = messageParts[0];

      Map<Object, Object> headers = new HashMap<Object, Object>();
      if (messageParts.length > 1) {
        Object second = messageParts[1];
        if (second instanceof Map) {
          setupHeadersFromMap(headers, (Map) second);
        } else if (second instanceof Header[]) {
          setupHeadersFromHeaderArray(headers, (Header[]) second);
        }

        determineHttpVersion(headers);
        rewriteConnectionAndKeepAliveHeaders(headers);

        addInboundProperties(headers);
      }
    } else if (message instanceof HttpResponse) {
      this.message = message;
      return;
    } else {
      this.message = message;
    }

    String contentType = getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, null);
    if (contentType != null) {
      // set the encoding
      Header contentTypeHeader = new Header(HttpConstants.HEADER_CONTENT_TYPE, contentType);
      HeaderElement values[] = contentTypeHeader.getElements();
      if (values.length == 1) {
        NameValuePair param = values[0].getParameterByName("charset");
        if (param != null) {
          setEncoding(param.getValue());
        }
      }
    }
  }
  /**
   * Parse RFC 2965 specific cookie attribute and update the corresponsing {@link
   * org.apache.commons.httpclient.Cookie} properties.
   *
   * @param attribute {@link org.apache.commons.httpclient.NameValuePair} cookie attribute from the
   *     <tt>Set-Cookie2</tt> header.
   * @param cookie {@link org.apache.commons.httpclient.Cookie} to be updated
   * @throws MalformedCookieException if an exception occurs during parsing
   */
  public void parseAttribute(final NameValuePair attribute, final Cookie cookie)
      throws MalformedCookieException {
    if (attribute == null) {
      throw new IllegalArgumentException("Attribute may not be null.");
    }
    if (attribute.getName() == null) {
      throw new IllegalArgumentException("Attribute Name may not be null.");
    }
    if (cookie == null) {
      throw new IllegalArgumentException("Cookie may not be null.");
    }
    final String paramName = attribute.getName().toLowerCase();
    final String paramValue = attribute.getValue();

    CookieAttributeHandler handler = findAttribHandler(paramName);
    if (handler == null) {
      // ignore unknown attribute-value pairs
      if (LOG.isDebugEnabled()) LOG.debug("Unrecognized cookie attribute: " + attribute.toString());
    } else {
      handler.parse(cookie, paramValue);
    }
  }
 // TODO Jean
 public static URL createQuery(final int port, final CidsBean bean) {
   if ((port < 0) || (port > 65535)) {
     log.warn("Crossover: verdisCrossoverPort ist ungültig: " + port);
   } else {
     try {
       // ToDo ugly because is static
       PARAMETER_KASSENZEICHEN.setValue(
           String.valueOf(bean.getProperty("kassenzeichennummer8"))); // kz.getId().toString());
       final GetMethod tmp = new GetMethod(server + port + request);
       tmp.setQueryString(new NameValuePair[] {PARAMETER_KASSENZEICHEN});
       if (log.isDebugEnabled()) {
         log.debug("Crossover: verdisCrossOverQuery: " + tmp.getURI().toString());
       }
       return new URL(tmp.getURI().toString());
     } catch (Exception ex) {
       log.error("Crossover: Fehler beim fernsteuern von VerdIS.", ex);
     }
   }
   return null;
 }
Example #9
0
  /**
   * comparing the two name value pairs. ( header / param name value pairs)
   *
   * @return boolean - true if their contents are same.
   */
  private boolean compareNameValuePairs(List<NameValuePair> pair1, List<NameValuePair> pair2) {

    if (pair1 != null && pair2 != null) {

      if (pair1 == null || pair2 == null) {
        return false;
      }

      if (pair1.size() != pair2.size()) {
        return false;
      }

      Iterator pair1_itr = pair1.iterator();
      Iterator pair2_itr = pair2.iterator();

      while (pair1_itr.hasNext()) {

        NameValuePair pair1_namevaluepair = (NameValuePair) pair1_itr.next();
        NameValuePair pair2_namevaluepair = (NameValuePair) pair2_itr.next();

        if (pair1_namevaluepair.getName().equals(pair2_namevaluepair.getName())
            && pair1_namevaluepair.getValue().equals(pair2_namevaluepair.getValue())) {

        } else {
          pair1_itr = null;
          pair2_itr = null;
          return false;
        }
      }
      pair1_itr = null;
      pair2_itr = null;
      return true;

    } else {
      return true;
    }
  }
Example #10
0
  private String send(HttpMethod method, boolean sslExceptionIgnore) throws HttpNetAgentException {
    String responseBody = null;
    HttpClient hc = null;

    try {
      this.hashCode();
      String reqLog =
          ">> " + (method instanceof PostMethod ? "POST" : "GET") + " [" + method.getURI() + "]";

      if (method instanceof PostMethod) {
        NameValuePair[] params = ((PostMethod) method).getParameters();
        for (int i = 0; i < params.length; i++) {
          NameValuePair param = params[i];
          reqLog += "\n" + (param.getName() + "=" + param.getValue());
        }
      }
      reqLog +=
          ", hscd["
              + this.hashCode()
              + "], sslExcIgnore["
              + sslExceptionIgnore
              + "], hs["
              + this.hashCode()
              + "]";

      logger.info(reqLog);
      logger.info(getHttpInfoDumy(method));

      HttpConnectionManager httpConnMgr = new SimpleHttpConnectionManager();
      HttpConnectionManagerParams httpConnMgrParams = new HttpConnectionManagerParams();

      // Connection Timeout 설정
      httpConnMgrParams.setConnectionTimeout(netTimeoutConn);

      // Socket Timeout 설정
      httpConnMgrParams.setSoTimeout(netTimeoutSock);
      httpConnMgr.setParams(httpConnMgrParams);

      hc = new HttpClient(httpConnMgr);
      /*
      if (sslExceptionIgnore){
      	try {
      		Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
      	} catch (GeneralSecurityException e) {
      		e.printStackTrace();
      	}
      }
      */

      int status = hc.executeMethod(method);

      if (200 == status) {
        responseBody = method.getResponseBodyAsString();
        logger.info("<< responseBody [" + responseBody + "], hs[" + this.hashCode() + "]");
        if (responseBody == null) responseBody = "";

      } else {
        logger.error(
            "<< http status Not Success ["
                + status
                + "], hs["
                + this.hashCode()
                + "],"
                + getHttpInfoDumy(method));
        throw new HttpNetAgentException(
            HttpNetAgentException.HTTP_HAEDER_NOT_SUCCESS, "Http Status[" + status + "]");
      }

    } catch (HttpException e) {
      logger.error(
          "<< HttpException msg ["
              + e.getMessage()
              + "], hs["
              + this.hashCode()
              + "],"
              + getHttpInfoDumy(method),
          e);
      throw new HttpNetAgentException(HttpNetAgentException.HTTP_NET_ERROR, e.getMessage());
    } catch (IOException e) {
      logger.error(
          "<< IOException msg ["
              + e.getMessage()
              + "], hs["
              + this.hashCode()
              + "],"
              + getHttpInfoDumy(method),
          e);
      throw new HttpNetAgentException(HttpNetAgentException.HTTP_NET_ERROR, e.getMessage());
    }

    return responseBody;
  }
  /** @see #parse(String, int, String, boolean, org.apache.commons.httpclient.Header) */
  public Cookie[] parse(String host, int port, String path, boolean secure, final String header)
      throws MalformedCookieException {
    LOG.trace("enter RFC2965Spec.parse(" + "String, int, String, boolean, String)");

    // before we do anything, lets check validity of arguments
    if (host == null) {
      throw new IllegalArgumentException("Host of origin may not be null");
    }
    if (host.trim().equals("")) {
      throw new IllegalArgumentException("Host of origin may not be blank");
    }
    if (port < 0) {
      throw new IllegalArgumentException("Invalid port: " + port);
    }
    if (path == null) {
      throw new IllegalArgumentException("Path of origin may not be null.");
    }
    if (header == null) {
      throw new IllegalArgumentException("Header may not be null.");
    }

    if (path.trim().equals("")) {
      path = PATH_DELIM;
    }
    host = getEffectiveHost(host);

    HeaderElement[] headerElements = HeaderElement.parseElements(header.toCharArray());

    List cookies = new LinkedList();
    for (int i = 0; i < headerElements.length; i++) {
      HeaderElement headerelement = headerElements[i];
      Cookie2 cookie = null;
      try {
        cookie =
            new Cookie2(
                host,
                headerelement.getName(),
                headerelement.getValue(),
                path,
                null,
                false,
                new int[] {port});
      } catch (IllegalArgumentException ex) {
        throw new MalformedCookieException(ex.getMessage());
      }
      NameValuePair[] parameters = headerelement.getParameters();
      // could be null. In case only a header element and no parameters.
      if (parameters != null) {
        // Eliminate duplicate attribues. The first occurence takes precedence
        Map attribmap = new HashMap(parameters.length);
        for (int j = parameters.length - 1; j >= 0; j--) {
          NameValuePair param = parameters[j];
          attribmap.put(param.getName().toLowerCase(), param);
        }
        for (Iterator it = attribmap.entrySet().iterator(); it.hasNext(); ) {
          Map.Entry entry = (Map.Entry) it.next();
          parseAttribute((NameValuePair) entry.getValue(), cookie);
        }
      }
      cookies.add(cookie);
      // cycle through the parameters
    }
    return (Cookie[]) cookies.toArray(new Cookie[cookies.size()]);
  }