コード例 #1
0
  private HttpResponse getHTTPResponse() throws IOException, NullPointerException {
    if (fileURI == null) throw new NullPointerException("No file URI specified");

    HttpClient client = new DefaultHttpClient();
    BasicHttpContext localContext = new BasicHttpContext();

    // Clear down the local cookie store every time to make sure we don't have any left over cookies
    // influencing the test
    localContext.setAttribute(ClientContext.COOKIE_STORE, null);

    LOG.info("Mimic WebDriver cookie state: " + mimicWebDriverCookieState);
    if (mimicWebDriverCookieState) {
      localContext.setAttribute(
          ClientContext.COOKIE_STORE, mimicCookieState(driver.manage().getCookies()));
    }

    HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod();
    requestMethod.setURI(fileURI);
    HttpParams httpRequestParameters = requestMethod.getParams();
    httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
    requestMethod.setParams(httpRequestParameters);
    // TODO if post send map of variables, also need to add a post map setter

    LOG.info("Sending " + httpRequestMethod.toString() + " request for: " + fileURI);
    return client.execute(requestMethod, localContext);
  }
コード例 #2
0
  /**
   * Perform an HTTP Status check and return the response code
   *
   * @return
   * @throws IOException
   */
  public int getHTTPStatusCode() throws IOException {

    HttpClient client = new DefaultHttpClient();
    BasicHttpContext localContext = new BasicHttpContext();
    System.out.println("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState);
    if (this.mimicWebDriverCookieState) {
      localContext.setAttribute(
          ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies()));
    }
    HttpRequestBase requestMethod = this.httpRequestMethod.getRequestMethod();
    requestMethod.setURI(this.linkToCheck);
    HttpParams httpRequestParameters = requestMethod.getParams();
    httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects);
    requestMethod.setParams(httpRequestParameters);

    System.out.println(
        "Sending " + requestMethod.getMethod() + " request for: " + requestMethod.getURI());
    HttpResponse response = client.execute(requestMethod, localContext);
    System.out.println(
        "HTTP "
            + requestMethod.getMethod()
            + " request status: "
            + response.getStatusLine().getStatusCode());

    return response.getStatusLine().getStatusCode();
  }
コード例 #3
0
  public void loadHttpMethod(final ClientRequest request, HttpRequestBase httpMethod)
      throws Exception {
    if (httpMethod instanceof HttpGet && request.followRedirects()) {
      HttpClientParams.setRedirecting(httpMethod.getParams(), true);
    } else {
      HttpClientParams.setRedirecting(httpMethod.getParams(), false);
    }

    if (request.getBody() != null && !request.getFormParameters().isEmpty())
      throw new RuntimeException(Messages.MESSAGES.cannotSendFormParametersAndEntity());

    if (!request.getFormParameters().isEmpty()) {
      commitHeaders(request, httpMethod);
      HttpPost post = (HttpPost) httpMethod;

      List<NameValuePair> formparams = new ArrayList<NameValuePair>();

      for (Map.Entry<String, List<String>> formParam : request.getFormParameters().entrySet()) {
        List<String> values = formParam.getValue();
        for (String value : values) {
          formparams.add(new BasicNameValuePair(formParam.getKey(), value));
        }
      }

      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
      post.setEntity(entity);
    } else if (request.getBody() != null) {
      if (httpMethod instanceof HttpGet)
        throw new RuntimeException(Messages.MESSAGES.getRequestCannotHaveBody());

      try {
        HttpEntity entity = buildEntity(request);
        HttpPost post = (HttpPost) httpMethod;
        commitHeaders(request, httpMethod);
        post.setEntity(entity);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    } else // no body
    {
      commitHeaders(request, httpMethod);
    }
  }
コード例 #4
0
  public Response request(String url, String data, String method) {
    WorkerThread th = (WorkerThread) Thread.currentThread();

    HttpRequestBase req;
    if (method == "POST") {
      req = new HttpPost(url);
      if (data != null) {
        StringEntity se = null;
        try {
          se = new StringEntity(data);
        } catch (UnsupportedEncodingException e) {
          return new Response(-1, e.toString(), null);
        }
        se.setContentType("application/x-www-form-urlencoded");
        ((HttpPost) req).setEntity(se);
      }
    } else {
      req = new HttpGet(url);
    }
    req.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

    HttpResponse response = null;
    try {
      response = th.client.execute(req);
    } catch (Exception e) {
      return new Response(-1, e.toString(), null);
    }

    StatusLine statusLine = response.getStatusLine();
    String responseData = null;
    try {
      InputStream is = response.getEntity().getContent();
      try {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
          sb.append(line);
        }
        responseData = sb.toString();
      } finally {
        is.close();
      }
    } catch (Exception e) {
      return new Response(-1, e.toString(), null);
    }

    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
      return new Response(200, null, responseData);
    }
    return new Response(statusLine.getStatusCode(), statusLine.getReasonPhrase(), null);
  }
コード例 #5
0
ファイル: HttpSolrServer.java プロジェクト: neilzhangsl/solr
  public NamedList<Object> request(final SolrRequest request, final ResponseParser processor)
      throws SolrServerException, IOException {
    HttpRequestBase 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 = DEFAULT_PATH;
    }

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

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

    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 HttpGet(baseUrl + path + ClientUtils.toQueryString(wparams, false));
          } else if (SolrRequest.METHOD.POST == request.getMethod()) {

            String url = baseUrl + path;
            boolean hasNullStreamName = false;
            if (streams != null) {
              for (ContentStream cs : streams) {
                if (cs.getName() == null) {
                  hasNullStreamName = true;
                  break;
                }
              }
            }
            boolean isMultipart =
                (this.useMultiPartPost || (streams != null && streams.size() > 1))
                    && !hasNullStreamName;

            // only send this list of params as query string params
            ModifiableSolrParams queryParams = new ModifiableSolrParams();
            for (String param : this.queryParams) {
              String[] value = wparams.getParams(param);
              if (value != null) {
                for (String v : value) {
                  queryParams.add(param, v);
                }
                wparams.remove(param);
              }
            }

            LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>();
            if (streams == null || isMultipart) {
              HttpPost post = new HttpPost(url + ClientUtils.toQueryString(queryParams, false));
              post.setHeader("Content-Charset", "UTF-8");
              if (!isMultipart) {
                post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
              }

              List<FormBodyPart> parts = new LinkedList<FormBodyPart>();
              Iterator<String> iter = wparams.getParameterNamesIterator();
              while (iter.hasNext()) {
                String p = iter.next();
                String[] vals = wparams.getParams(p);
                if (vals != null) {
                  for (String v : vals) {
                    if (isMultipart) {
                      parts.add(new FormBodyPart(p, new StringBody(v, Charset.forName("UTF-8"))));
                    } else {
                      postParams.add(new BasicNameValuePair(p, v));
                    }
                  }
                }
              }

              if (isMultipart && streams != null) {
                for (ContentStream content : streams) {
                  String contentType = content.getContentType();
                  if (contentType == null) {
                    contentType = BinaryResponseParser.BINARY_CONTENT_TYPE; // default
                  }
                  String name = content.getName();
                  if (name == null) {
                    name = "";
                  }
                  parts.add(
                      new FormBodyPart(
                          name,
                          new InputStreamBody(
                              content.getStream(), contentType, content.getName())));
                }
              }

              if (parts.size() > 0) {
                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
                for (FormBodyPart p : parts) {
                  entity.addPart(p);
                }
                post.setEntity(entity);
              } else {
                // not using multipart
                post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));
              }

              method = post;
            }
            // It is has one stream, it is the post body, put the params in the URL
            else {
              String pstr = ClientUtils.toQueryString(wparams, false);
              HttpPost post = new HttpPost(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.setEntity(
                    new InputStreamEntity(contentStream[0].getStream(), -1) {
                      @Override
                      public Header getContentType() {
                        return new BasicHeader("Content-Type", contentStream[0].getContentType());
                      }

                      @Override
                      public boolean isRepeatable() {
                        return false;
                      }
                    });
              } else {
                post.setEntity(
                    new InputStreamEntity(contentStream[0].getStream(), -1) {
                      @Override
                      public Header getContentType() {
                        return new BasicHeader("Content-Type", contentStream[0].getContentType());
                      }

                      @Override
                      public boolean isRepeatable() {
                        return false;
                      }
                    });
              }
              method = post;
            }
          } else {
            throw new SolrServerException("Unsupported method: " + request.getMethod());
          }
        } catch (NoHttpResponseException r) {
          method = null;
          if (is != null) {
            is.close();
          }
          // If out of tries then just rethrow (as normal error).
          if (tries < 1) {
            throw r;
          }
        }
      }
    } catch (IOException ex) {
      throw new SolrServerException("error reading streams", ex);
    }

    // XXX client already has this set, is this needed?
    method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
    method.addHeader("User-Agent", AGENT);

    InputStream respBody = null;
    boolean shouldClose = true;
    boolean success = false;
    try {
      // Execute the method.
      final HttpResponse response = httpClient.execute(method);
      int httpStatus = response.getStatusLine().getStatusCode();

      // Read the contents
      respBody = response.getEntity().getContent();
      Header ctHeader = response.getLastHeader("content-type");
      String contentType;
      if (ctHeader != null) {
        contentType = ctHeader.getValue();
      } else {
        contentType = "";
      }

      // handle some http level checks before trying to parse the response
      switch (httpStatus) {
        case HttpStatus.SC_OK:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_CONFLICT: // 409
          break;
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
          if (!followRedirects) {
            throw new SolrServerException(
                "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ").");
          }
          break;
        default:
          if (processor == null) {
            throw new RemoteSolrException(
                httpStatus,
                "Server at "
                    + getBaseURL()
                    + " returned non ok status:"
                    + httpStatus
                    + ", message:"
                    + response.getStatusLine().getReasonPhrase(),
                null);
          }
      }
      if (processor == null) {

        // no processor specified, return raw stream
        NamedList<Object> rsp = new NamedList<Object>();
        rsp.add("stream", respBody);
        // Only case where stream should not be closed
        shouldClose = false;
        success = true;
        return rsp;
      }

      String procCt = processor.getContentType();
      if (procCt != null) {
        if (!contentType.equals(procCt)) {
          // unexpected content type
          String msg = "Expected content type " + procCt + " but got " + contentType + ".";
          Header encodingHeader = response.getEntity().getContentEncoding();
          String encoding;
          if (encodingHeader != null) {
            encoding = encodingHeader.getValue();
          } else {
            encoding = "UTF-8"; // try UTF-8
          }
          try {
            msg = msg + " " + IOUtils.toString(respBody, encoding);
          } catch (IOException e) {
            throw new RemoteSolrException(
                httpStatus, "Could not parse response with encoding " + encoding, e);
          }
          RemoteSolrException e = new RemoteSolrException(httpStatus, msg, null);
          throw e;
        }
      }

      //      if(true) {
      //        ByteArrayOutputStream copy = new ByteArrayOutputStream();
      //        IOUtils.copy(respBody, copy);
      //        String val = new String(copy.toByteArray());
      //        System.out.println(">RESPONSE>"+val+"<"+val.length());
      //        respBody = new ByteArrayInputStream(copy.toByteArray());
      //      }

      NamedList<Object> rsp = null;
      String charset = EntityUtils.getContentCharSet(response.getEntity());
      try {
        rsp = processor.processResponse(respBody, charset);
      } catch (Exception e) {
        throw new RemoteSolrException(httpStatus, e.getMessage(), e);
      }
      if (httpStatus != HttpStatus.SC_OK) {
        String reason = null;
        try {
          NamedList err = (NamedList) rsp.get("error");
          if (err != null) {
            reason = (String) err.get("msg");
            // TODO? get the trace?
          }
        } catch (Exception ex) {
        }
        if (reason == null) {
          StringBuilder msg = new StringBuilder();
          msg.append(response.getStatusLine().getReasonPhrase());
          msg.append("\n\n");
          msg.append("request: " + method.getURI());
          reason = java.net.URLDecoder.decode(msg.toString(), UTF_8);
        }
        throw new RemoteSolrException(httpStatus, reason, null);
      }
      success = true;
      return rsp;
    } catch (ConnectException e) {
      throw new SolrServerException("Server refused connection at: " + getBaseURL(), e);
    } catch (SocketTimeoutException e) {
      throw new SolrServerException(
          "Timeout occured while waiting response from server at: " + getBaseURL(), e);
    } catch (IOException e) {
      throw new SolrServerException(
          "IOException occured when talking to server at: " + getBaseURL(), e);
    } finally {
      if (respBody != null && shouldClose) {
        try {
          respBody.close();
        } catch (Throwable t) {
        } // ignore
        if (!success) {
          method.abort();
        }
      }
    }
  }