void copyProxyReponse(HttpMethod proxyResponse, HttpServletResponse response) throws IOException {
   copyProxyHeaders(proxyResponse.getResponseHeaders(), response);
   response.setContentLength(getResponseContentLength(proxyResponse));
   copy(proxyResponse.getResponseBodyAsStream(), response.getOutputStream());
   if (proxyResponse.getStatusLine() != null) {
     int statCode = proxyResponse.getStatusCode();
     response.setStatus(statCode);
   }
 }
Example #2
0
  /**
   * Gets the result of the execution of a get request. the attempt will be repeated until obtain
   * the exepected result
   *
   * @param path the path on which the request should be executed
   * @param expression the result that should be returned by the GET request, which allows to know
   *     if that request is completely processed or not
   * @throws Exception if something's going wrong...
   */
  public void retryGetRequestUntilObtainExpectedResponse(
      @NonNull String path, @NonNull String expression) throws Exception {
    String fullpath = path;
    if (path.startsWith("/")) fullpath = baseUrl + path;
    log.debug("Sending GET request to " + fullpath + " with several attemps");

    final int maxAttempts = Integer.parseInt(ExecutionContext.get(RestConstants.MAX_ATTEMPTS_KEY));
    final int timeToWait = Integer.parseInt(ExecutionContext.get(RestConstants.TIME_TO_WAIT_KEY));

    final HttpMethod method = new GetMethod(fullpath);
    try {
      for (final Map.Entry<String, String> header : requestHeaders.entrySet())
        method.setRequestHeader(header.getKey(), header.getValue());

      String responseAsString = null;
      String toCheck = null;
      String expected = expression;
      String prop = null;
      final int idx = expression.indexOf("=");
      if (idx > 0) {
        prop = expression.substring(0, idx);
        expected = expression.substring(idx + 1);
      }
      int statusCode;
      int attempts = 0;
      boolean success = false;
      do {
        // waiting timeToWait seconds
        Thread.sleep(timeToWait * 1000);
        statusCode = httpClient.executeMethod(method);
        attempts++;
        if (statusCode == HttpStatus.SC_OK) {
          responseAsString = method.getResponseBodyAsString();
          toCheck = responseAsString;
          if (prop != null) toCheck = JSONHelper.getPropertyValue(responseAsString, prop);
          if (toCheck.contains(expected)) {
            success = true;
            log.debug("The result is available! ");
          } else
            log.warn("The result is not yet available! | Waiting " + timeToWait + " seconds ...");
        } else
          log.warn(
              "unsuccessful GET request : "
                  + method.getStatusLine()
                  + " | Waiting "
                  + timeToWait
                  + " seconds ...");
      } while (!success && maxAttempts > attempts);
      response = new ResponseWrapper(responseAsString, statusCode);
    } catch (final Exception e) {
      log.error(e.getMessage(), e);
      throw e;
    } finally {
      method.releaseConnection();
    }
  }
  protected Exception populateHttpOperationFailedException(
      Exchange exchange, HttpMethod method, int responseCode)
      throws IOException, ClassNotFoundException {
    Exception answer;

    String uri = method.getURI().toString();
    String statusText =
        method.getStatusLine() != null ? method.getStatusLine().getReasonPhrase() : null;
    Map<String, String> headers = extractResponseHeaders(method.getResponseHeaders());

    Object responseBody = extractResponseBody(method, exchange);
    if (transferException && responseBody != null && responseBody instanceof Exception) {
      // if the response was a serialized exception then use that
      return (Exception) responseBody;
    }

    // make a defensive copy of the response body in the exception so its detached from the cache
    String copy = null;
    if (responseBody != null) {
      copy =
          exchange.getContext().getTypeConverter().convertTo(String.class, exchange, responseBody);
    }

    if (responseCode >= 300 && responseCode < 400) {
      String redirectLocation;
      Header locationHeader = method.getResponseHeader("location");
      if (locationHeader != null) {
        redirectLocation = locationHeader.getValue();
        answer =
            new HttpOperationFailedException(
                uri, responseCode, statusText, redirectLocation, headers, copy);
      } else {
        // no redirect location
        answer =
            new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
      }
    } else {
      // internal server error (error code 500)
      answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
    }

    return answer;
  }
 private HTTPClientResponseResolver createResponseResolver(
     final HttpMethod httpMethod, final Status status, final Header[] headers) {
   try {
     when(httpMethod.getStatusLine())
         .thenReturn(
             new org.apache.commons.httpclient.StatusLine(
                 String.format("HTTP/1.1 %s %s\r\n", status.getCode(), status.getName())));
   } catch (HttpException e) {
     throw new RuntimeException(e);
   }
   when(httpMethod.getStatusCode()).thenReturn(status.getCode());
   when(httpMethod.getResponseHeaders()).thenReturn(headers);
   return new TestableHTTPClientResponseResolver(httpMethod);
 }
  public JSONObject get(String path) throws Exception {
    JSONObject result;
    HttpMethod method = new GetMethod(repoHostname + ":" + repoHostPort + "/api/data/" + path);
    method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    method.setRequestHeader("Cookie", "jsonhub-store-key=" + getStoreKey());

    // client.getState().setCredentials(new AuthScope(null, repoHostPort, "authentication"), new
    // UsernamePasswordCredentials(memberId, memberPassword));

    // method.setDoAuthentication(true);

    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
      System.err.println("Method failed: " + method.getStatusLine());
      throw new GeneralException(method.getStatusLine().toString());
    } else {
      Reader reader = new InputStreamReader(method.getResponseBodyAsStream());
      CharArrayWriter cdata = new CharArrayWriter();
      char buf[] = new char[BUFFER_SIZE];
      int ret;
      while ((ret = reader.read(buf, 0, BUFFER_SIZE)) != -1) cdata.write(buf, 0, ret);

      reader.close();

      System.out.println("get response: " + cdata.toString());

      result = JSONObject.fromObject(cdata.toString());
    }

    method.releaseConnection();

    if (result.has("error")) throw new GeneralException(result.getString("error"));

    return result;
  }
Example #6
0
 public static Msg<List<LatLng>> call(String staffid) {
   Msg<List<LatLng>> rmsg = new Msg<List<LatLng>>();
   List<LatLng> lls = new ArrayList();
   AroundLocation mrj = new AroundLocation();
   try {
     HttpClient hc = new HttpClient();
     //			String urlPath=Constants.HF_url;
     String urlPath = "http://yuntuapi.amap.com/datamanage/data/list?";
     urlPath += "tableid=558d1470e4b0b297165d1168";
     urlPath += "&filter=user_id:" + staffid;
     urlPath += "&sortrule=sequence:1";
     urlPath += "&key=" + Constants.EWP_SERVER_AK;
     //			urlPath +="&key=25284b478f1da94d318d0f7d8bd8af87";
     System.out.println(urlPath);
     HttpMethod method = new GetMethod(urlPath);
     hc.executeMethod(method);
     // 打印服务器返回的状态
     System.out.println(method.getStatusLine());
     // 打印返回的信息
     System.out.println(method.getResponseBodyAsString().replaceAll("_", "map"));
     // 释放连接
     method.releaseConnection();
     // 解析JSON
     mrj =
         JSON.parseObject(
             method.getResponseBodyAsString().replaceAll("_", "map"), AroundLocation.class);
     List<Datas> pjs = mrj.getDatas();
     if (pjs != null) {
       for (int i = 0; i < pjs.size(); i++) {
         String[] location = pjs.get(i).getMaplocation().split(",");
         lls.add(new LatLng(Double.valueOf(location[1]), Double.valueOf(location[0])));
       }
       rmsg.setResult(true);
       rmsg.setValue(lls);
     } else {
       rmsg.setResult(false);
     }
   } catch (Exception e) {
     e.printStackTrace();
     rmsg.setResult(false);
     return rmsg;
   }
   return rmsg;
 }
Example #7
0
  /**
   * Gets the result of the execution of a get request. the attempt will be repeated several times
   * in case of failures
   *
   * @param path the path on which the request should be sent
   * @throws Exception if something's going wrong...
   */
  public void retryGetRequestUntilSucceed(@NonNull String path) throws Exception {
    String fullpath = path;
    if (path.startsWith("/")) fullpath = baseUrl + path;

    log.debug("Sending GET request to " + fullpath + " with several attemps");

    final int maxAttempts = Integer.parseInt(ExecutionContext.get(RestConstants.MAX_ATTEMPTS_KEY));
    final int timeToWait = Integer.parseInt(ExecutionContext.get(RestConstants.TIME_TO_WAIT_KEY));

    final HttpMethod method = new GetMethod(fullpath);
    try {
      for (final Map.Entry<String, String> header : requestHeaders.entrySet())
        method.setRequestHeader(header.getKey(), header.getValue());

      String responseAsString = null;
      int statusCode;
      int attempts = 0;
      boolean success = false;
      do {
        // waiting timeToWait seconds
        Thread.sleep(timeToWait * 1000);
        statusCode = httpClient.executeMethod(method);
        attempts++;
        // check for status code 200
        if (statusCode == HttpStatus.SC_OK) {
          responseAsString = method.getResponseBodyAsString();
          success = true;
          log.info("The result is available! ");
        } else
          log.warn(
              "unsuccessful GET request : "
                  + method.getStatusLine()
                  + " | Waiting "
                  + timeToWait
                  + " seconds ...");
      } while (!success && maxAttempts > attempts);
      response = new ResponseWrapper(responseAsString, statusCode);
    } catch (final Exception e) {
      log.error(e.getMessage(), e);
      throw e;
    } finally {
      method.releaseConnection();
    }
  }
Example #8
0
 public static Msg<List<Datas>> callAround(String lat, String lng) {
   Msg<List<Datas>> rmsg = new Msg<List<Datas>>();
   List<LatLng> lls = new ArrayList();
   AroundLocation mrj = new AroundLocation();
   try {
     HttpClient hc = new HttpClient();
     //			String urlPath=Constants.HF_url;
     String urlPath = "http://yuntuapi.amap.com/datasearch/around?";
     urlPath += "tableid=558d1470e4b0b297165d1168";
     urlPath += "&sortrule=user_id:1";
     urlPath += "&center=" + lng + "," + lat;
     urlPath += "&radius=" + Constants.EWP_AROUND_RADIUS;
     //			urlPath +="&radius=5000";
     urlPath += "&key=" + Constants.EWP_SERVER_AK;
     //			urlPath +="&key=25284b478f1da94d318d0f7d8bd8af87";
     System.out.println(urlPath);
     HttpMethod method = new GetMethod(urlPath);
     hc.executeMethod(method);
     // 打印服务器返回的状态
     System.out.println(method.getStatusLine());
     // 打印返回的信息
     System.out.println(method.getResponseBodyAsString().replaceAll("_", "map"));
     // 释放连接
     method.releaseConnection();
     // 解析JSON
     mrj =
         JSON.parseObject(
             method.getResponseBodyAsString().replaceAll("_", "map"), AroundLocation.class);
     List<Datas> pjs = mrj.getDatas();
     if (pjs != null && pjs.size() > 0) {
       rmsg.setResult(true);
       rmsg.setValue(pjs);
     } else {
       rmsg.setResult(false);
     }
   } catch (Exception e) {
     e.printStackTrace();
     rmsg.setResult(false);
     return rmsg;
   }
   return rmsg;
 }
  private String post(final HttpMethod method) throws IOException, ForbiddenException {
    method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    method.setRequestHeader("Accept-Encoding", "gzip");
    InputStream is = null;
    try {
      this.m_clientManager.executeMethod(method);
      final int statusCode = method.getStatusCode();
      final StatusLine statusLine = method.getStatusLine();
      final Header encoding = method.getResponseHeader("Content-Encoding");
      if (encoding != null && encoding.getValue().equals("gzip")) {
        m_log.debug("Unzipping body...");
        is = new GZIPInputStream(method.getResponseBodyAsStream());
      } else {
        is = method.getResponseBodyAsStream();
      }
      final String body = IOUtils.toString(is);
      if (StringUtils.isBlank(body)) {
        // Could easily be a post request, which would not have a body.
        m_log.debug("No response body.  Post request?");
      }
      if (statusCode == HttpStatus.SC_FORBIDDEN) {
        final String msg = "NO 200 OK: " + method.getURI() + "\n" + statusLine + "\n" + body;
        m_log.warn(msg);
        throw new ForbiddenException(msg);
      }

      if (statusCode != HttpStatus.SC_OK) {

        final String msg = "NO 200 OK: " + method.getURI() + "\n" + statusLine + "\n" + body;
        m_log.warn(msg);
        throw new IOException(msg);
      } else {
        m_log.debug("Got 200 response...");
      }

      return body;
    } finally {
      IOUtils.closeQuietly(is);
      method.releaseConnection();
    }
  }
  /**
   * Report to deliver engines that a portlet has been uploaded
   *
   * @param contentId contentId of portlet
   */
  private void updateDeliverEngines(Integer digitalAssetId) {
    List allUrls = CmsPropertyHandler.getInternalDeliveryUrls();
    allUrls.addAll(CmsPropertyHandler.getPublicDeliveryUrls());

    Iterator urlIterator = allUrls.iterator();
    while (urlIterator.hasNext()) {
      String url = (String) urlIterator.next() + "/DeployPortlet.action";

      try {
        HttpClient client = new HttpClient();

        // establish a connection within 5 seconds
        client.setConnectionTimeout(5000);

        // set the default credentials
        HttpMethod method = new GetMethod(url);
        method.setQueryString("digitalAssetId=" + digitalAssetId);
        method.setFollowRedirects(true);

        // execute the method
        client.executeMethod(method);
        StatusLine status = method.getStatusLine();
        if (status != null && status.getStatusCode() == 200) {
          log.info("Successfully deployed portlet at " + url);
        } else {
          log.warn("Failed to deploy portlet at " + url + ": " + status);
        }

        // clean up the connection resources
        method.releaseConnection();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    /*
    Properties props = CmsPropertyHandler.getProperties();
    for (Enumeration keys = props.keys(); keys.hasMoreElements();) {
        String key = (String) keys.nextElement();
        if (key.startsWith(PORTLET_DEPLOY_PREFIX)) {
            String url = props.getProperty(key);
            try {
                HttpClient client = new HttpClient();

                //establish a connection within 5 seconds
                client.setConnectionTimeout(5000);

                //set the default credentials
                HttpMethod method = new GetMethod(url);
                method.setQueryString("digitalAssetId=" + digitalAssetId);
                method.setFollowRedirects(true);

                //execute the method
                client.executeMethod(method);
                StatusLine status = method.getStatusLine();
                if (status != null && status.getStatusCode() == 200) {
                    log.info("Successfully deployed portlet at " + url);
                } else {
                    log.warn("Failed to deploy portlet at " + url + ": " + status);
                }

                //clean up the connection resources
                method.releaseConnection();
            } catch (Throwable e) {
                log.error(e.getMessage(), e);
            }
        }
    }
    */

  }
  public NamedList<Object> request(final SolrRequest request, ResponseParser processor)
      throws SolrServerException, IOException {
    HttpMethod method = null;
    InputStream is = null;
    SolrParams params = request.getParams();
    Collection<ContentStream> streams = requestWriter.getContentStreams(request);
    String path = requestWriter.getPath(request);
    if (path == null || !path.startsWith("/")) {
      path = "/select";
    }

    ResponseParser parser = request.getResponseParser();
    if (parser == null) {
      parser = _parser;
    }

    // The parser 'wt=' and 'version=' params are used instead of the original params
    ModifiableSolrParams wparams = new ModifiableSolrParams();
    wparams.set(CommonParams.WT, parser.getWriterType());
    wparams.set(CommonParams.VERSION, parser.getVersion());
    if (params == null) {
      params = wparams;
    } else {
      params = new DefaultSolrParams(wparams, params);
    }

    if (_invariantParams != null) {
      params = new DefaultSolrParams(_invariantParams, params);
    }

    int tries = _maxRetries + 1;
    try {
      while (tries-- > 0) {
        // Note: since we aren't do intermittent time keeping
        // ourselves, the potential non-timeout latency could be as
        // much as tries-times (plus scheduling effects) the given
        // timeAllowed.
        try {
          if (SolrRequest.METHOD.GET == request.getMethod()) {
            if (streams != null) {
              throw new SolrException(
                  SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
            }
            method = new GetMethod(_baseURL + path + ClientUtils.toQueryString(params, false));
          } else if (SolrRequest.METHOD.POST == request.getMethod()) {

            String url = _baseURL + path;
            boolean isMultipart = (streams != null && streams.size() > 1);

            if (streams == null || isMultipart) {
              PostMethod post = new PostMethod(url);
              post.getParams().setContentCharset("UTF-8");
              if (!this.useMultiPartPost && !isMultipart) {
                post.addRequestHeader(
                    "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
              }

              List<Part> parts = new LinkedList<Part>();
              Iterator<String> iter = params.getParameterNamesIterator();
              while (iter.hasNext()) {
                String p = iter.next();
                String[] vals = params.getParams(p);
                if (vals != null) {
                  for (String v : vals) {
                    if (this.useMultiPartPost || isMultipart) {
                      parts.add(new StringPart(p, v, "UTF-8"));
                    } else {
                      post.addParameter(p, v);
                    }
                  }
                }
              }

              if (isMultipart) {
                int i = 0;
                for (ContentStream content : streams) {
                  final ContentStream c = content;

                  String charSet = null;
                  String transferEncoding = null;
                  parts.add(
                      new PartBase(c.getName(), c.getContentType(), charSet, transferEncoding) {
                        @Override
                        protected long lengthOfData() throws IOException {
                          return c.getSize();
                        }

                        @Override
                        protected void sendData(OutputStream out) throws IOException {
                          InputStream in = c.getStream();
                          try {
                            IOUtils.copy(in, out);
                          } finally {
                            in.close();
                          }
                        }
                      });
                }
              }
              if (parts.size() > 0) {
                post.setRequestEntity(
                    new MultipartRequestEntity(
                        parts.toArray(new Part[parts.size()]), post.getParams()));
              }

              method = post;
            }
            // It is has one stream, it is the post body, put the params in the URL
            else {
              String pstr = ClientUtils.toQueryString(params, false);
              PostMethod post = new PostMethod(url + pstr);

              // Single stream as body
              // Using a loop just to get the first one
              final ContentStream[] contentStream = new ContentStream[1];
              for (ContentStream content : streams) {
                contentStream[0] = content;
                break;
              }
              if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
                post.setRequestEntity(
                    new RequestEntity() {
                      public long getContentLength() {
                        return -1;
                      }

                      public String getContentType() {
                        return contentStream[0].getContentType();
                      }

                      public boolean isRepeatable() {
                        return false;
                      }

                      public void writeRequest(OutputStream outputStream) throws IOException {
                        ((RequestWriter.LazyContentStream) contentStream[0]).writeTo(outputStream);
                      }
                    });

              } else {
                is = contentStream[0].getStream();
                post.setRequestEntity(
                    new InputStreamRequestEntity(is, contentStream[0].getContentType()));
              }
              method = post;
            }
          } else {
            throw new SolrServerException("Unsupported method: " + request.getMethod());
          }
        } catch (NoHttpResponseException r) {
          // This is generally safe to retry on
          method.releaseConnection();
          method = null;
          if (is != null) {
            is.close();
          }
          // If out of tries then just rethrow (as normal error).
          if ((tries < 1)) {
            throw r;
          }
          // log.warn( "Caught: " + r + ". Retrying..." );
        }
      }
    } catch (IOException ex) {
      throw new SolrServerException("error reading streams", ex);
    }

    method.setFollowRedirects(_followRedirects);
    method.addRequestHeader("User-Agent", AGENT);
    if (_allowCompression) {
      method.setRequestHeader(new Header("Accept-Encoding", "gzip,deflate"));
    }

    try {
      // Execute the method.
      // System.out.println( "EXECUTE:"+method.getURI() );

      int statusCode = _httpClient.executeMethod(method);
      if (statusCode != HttpStatus.SC_OK) {
        StringBuilder msg = new StringBuilder();
        msg.append(method.getStatusLine().getReasonPhrase());
        msg.append("\n\n");
        msg.append(method.getStatusText());
        msg.append("\n\n");
        msg.append("request: " + method.getURI());
        throw new SolrException(statusCode, java.net.URLDecoder.decode(msg.toString(), "UTF-8"));
      }

      // Read the contents
      String charset = "UTF-8";
      if (method instanceof HttpMethodBase) {
        charset = ((HttpMethodBase) method).getResponseCharSet();
      }
      InputStream respBody = method.getResponseBodyAsStream();
      // Jakarta Commons HTTPClient doesn't handle any
      // compression natively.  Handle gzip or deflate
      // here if applicable.
      if (_allowCompression) {
        Header contentEncodingHeader = method.getResponseHeader("Content-Encoding");
        if (contentEncodingHeader != null) {
          String contentEncoding = contentEncodingHeader.getValue();
          if (contentEncoding.contains("gzip")) {
            // log.debug( "wrapping response in GZIPInputStream" );
            respBody = new GZIPInputStream(respBody);
          } else if (contentEncoding.contains("deflate")) {
            // log.debug( "wrapping response in InflaterInputStream" );
            respBody = new InflaterInputStream(respBody);
          }
        } else {
          Header contentTypeHeader = method.getResponseHeader("Content-Type");
          if (contentTypeHeader != null) {
            String contentType = contentTypeHeader.getValue();
            if (contentType != null) {
              if (contentType.startsWith("application/x-gzip-compressed")) {
                // log.debug( "wrapping response in GZIPInputStream" );
                respBody = new GZIPInputStream(respBody);
              } else if (contentType.startsWith("application/x-deflate")) {
                // log.debug( "wrapping response in InflaterInputStream" );
                respBody = new InflaterInputStream(respBody);
              }
            }
          }
        }
      }
      return processor.processResponse(respBody, charset);
    } catch (HttpException e) {
      throw new SolrServerException(e);
    } catch (IOException e) {
      throw new SolrServerException(e);
    } finally {
      method.releaseConnection();
      if (is != null) {
        is.close();
      }
    }
  }
 /** Get the "reason phrase" associated with the status code. */
 public String getStatusText() {
   return m_method.getStatusLine().getReasonPhrase();
 }