Example #1
0
  private void writeLinks(HttpRequestBase httpMethod, String basePath) {
    StringBuilder linkHeader = new StringBuilder();

    for (RiakLink link : this.links) {
      if (linkHeader.length() > 0) {
        linkHeader.append(", ");
      }
      linkHeader.append("<");
      linkHeader.append(basePath);
      linkHeader.append("/");
      linkHeader.append(link.getBucket());
      linkHeader.append("/");
      linkHeader.append(link.getKey());
      linkHeader.append(">; ");
      linkHeader.append(Constants.LINK_TAG);
      linkHeader.append("=\"");
      linkHeader.append(link.getTag());
      linkHeader.append("\"");

      // To avoid (MochiWeb) problems with too long headers, flush if
      // it grows too big:
      if (linkHeader.length() > 2000) {
        httpMethod.addHeader(Constants.HDR_LINK, linkHeader.toString());
        linkHeader = new StringBuilder();
      }
    }
    if (linkHeader.length() > 0) {
      httpMethod.addHeader(Constants.HDR_LINK, linkHeader.toString());
    }
  }
Example #2
0
 private static ArrayList<SearchResults> apiRequest(
     String endpoint, String methodName, Map<String, String> params, File file)
     throws IOException, JSONException {
   HttpClient client = new DefaultHttpClient();
   HttpRequestBase request = null;
   String url = null;
   if (endpoint.startsWith("http")) {
     url = endpoint;
   } else {
     url = new StringBuffer(VIMEO_SERVER).append(endpoint).toString();
   }
   if (methodName.equals(HttpGet.METHOD_NAME)) {
     request = new HttpGet(url);
   } else if (methodName.equals(HttpPost.METHOD_NAME)) {
     request = new HttpPost(url);
   }
   request.addHeader("Accept", "application/vnd.vimeo.*+json; version=3.2");
   request.addHeader(
       "Authorization", new StringBuffer(tokenType).append(" ").append(token).toString());
   HttpResponse response = client.execute(request);
   String responseAsString = null;
   int statusCode = response.getStatusLine().getStatusCode();
   if (statusCode != 204) {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     response.getEntity().writeTo(out);
     responseAsString = out.toString("UTF-8");
     out.close();
   }
   JSONObject json = null;
   try {
     json = new JSONObject(responseAsString);
   } catch (Exception e) {
     json = new JSONObject();
   }
   ArrayList<SearchResults> tempSearch = new ArrayList<SearchResults>();
   for (int i = 0; i < json.getJSONArray("data").length(); i++) {
     JSONObject object = (JSONObject) json.getJSONArray("data").get(i);
     Map<String, String> out = new HashMap<String, String>();
     parse(object, out);
     SearchResults temp = new SearchResults();
     temp.setTitle(out.get("name"));
     //			temp.setOwner("By: " + out.get("owner.screenname"));
     temp.setId(out.get("uri").substring(out.get("uri").lastIndexOf('/') + 1));
     JSONArray tJson = new JSONArray(out.get("sizes"));
     for (int j = 0; j < tJson.length(); j++) {
       parse((JSONObject) tJson.get(j), out);
       if (out.get("width").equals("200")) {
         temp.setThumbnailUrl(out.get("link"));
         break;
       }
     }
     temp.setProvider("Vimeo");
     tempSearch.add(temp);
   }
   return tempSearch;
 }
 public void addRequestHeaders(NativeObject headersNO) {
   if (headersNO != null) {
     for (Object o : headersNO.getAllIds()) {
       Object val = headersNO.get(o.toString(), null);
       if (val instanceof NativeArray) {
         NativeArray arr = (NativeArray) val;
         for (int i = 0; i < arr.getLength(); i++) {
           method.addHeader(o.toString(), arr.get(i, null).toString());
         }
       } else {
         method.addHeader(o.toString(), val.toString());
       }
     }
   }
 }
Example #4
0
  private Response doRequest(final Request<?> request, final HttpRequestBase fetcher) {
    if (fetcher == null) {
      return null;
    }

    List<Parameter> headers = request.getHeaders();
    if (checkListOfParams(headers)) {
      for (Parameter parameter : headers) {
        Header header = new BasicHeader(parameter.getName(), parameter.getValue());
        fetcher.addHeader(header);
      }
    }

    final DefaultHttpClient httpClient = getHttpClient();
    List<Parameter> cookies = request.getCookies();
    if (checkListOfParams(cookies)) {
      CookieStore cookieStore = httpClient.getCookieStore();
      for (Parameter cookie : cookies) {
        cookieStore.addCookie(new BasicClientCookie(cookie.getName(), cookie.getValue()));
      }
      httpClient.setCookieStore(cookieStore);
    }

    return doRequest(fetcher, httpClient);
  }
Example #5
0
  @Override
  public void onCheckRequestHeaders(String requestUrl, HttpRequestBase request) {
    if (request == null) {
      throw new IllegalArgumentException("Http Request is null");
    }

    if (SUPPORT_RANGED) {
      // 目前只有大文件下载才会做此接口回调,在此回调中可以增加断点续传
      if (request instanceof HttpGet) {
        String saveFile =
            mDownloadFilenameCreateListener != null
                ? mDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(requestUrl)
                : mDefaultDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(
                    requestUrl);
        File bigCacheFile = new File(INPUT_STREAM_CACHE_PATH);
        if (!bigCacheFile.exists() || !bigCacheFile.isDirectory()) {
          bigCacheFile.delete();
          bigCacheFile.mkdirs();
        }

        File tempFile = new File(INPUT_STREAM_CACHE_PATH + saveFile);
        long fileSize = 0;
        if (tempFile.exists()) {
          fileSize = tempFile.length();
        } else {
          fileSize = 0;
        }

        request.addHeader("RANGE", "bytes=" + fileSize + "-");
      }
    }
  }
  private void addHeaders(HttpClientRequest request, HttpRequestBase baseRequest) {
    ScopedAttributesResolutionStrategy attributes =
        request.getAttributes().getScopeAttributeContext(ContextScope.REQUEST_HEADERS);

    for (Attribute attribute : attributes) {
      baseRequest.addHeader(attribute.getName(), attribute.getValue(String.class));
    }
  }
Example #7
0
  public Response makeRequest(
      Methods method, URI uri, HashMap<String, String> requestHeaders, String data) {

    HttpRequestBase request = null;

    try {
      if (method == Methods.GET) {
        request = new HttpGet();
      } else if (method == Methods.POST) {
        request = new HttpPost();
        if (data != null) {
          ((HttpPost) request).setEntity(new StringEntity(data, "UTF-8"));
        }
      } else if (method == Methods.PUT) {
        request = new HttpPut();
        if (data != null) {
          ((HttpPut) request).setEntity(new StringEntity(data, "UTF-8"));
        }
      } else if (method == Methods.DELETE) {
        request = new HttpDelete();
      }
    } catch (UnsupportedEncodingException e) {
      logger.log(e);
    }
    request.setURI(uri);
    AndroidHttpClient client = AndroidHttpClient.newInstance(userAgent);
    if (requestHeaders != null) {
      for (Map.Entry<String, String> entry : requestHeaders.entrySet()) {
        request.addHeader(new BasicHeader(entry.getKey(), entry.getValue()));
      }
    }

    HttpResponse response = null;
    Response retVal = new Response();
    retVal.headers = new HashMap<String, String>();
    try {
      response = client.execute(request);
    } catch (SocketTimeoutException e) {
      // forget it--we don't care that the user couldn't connect
      // TODO hand this back as an error to JS
    } catch (IOException e) {
      if (!caughtIOException) {
        caughtIOException = true;
        logger.log(e);
      }
    } catch (Exception e) {
      logger.log(e);
    }
    if (response != null) {
      retVal.status = response.getStatusLine().getStatusCode();
      retVal.body = readContent(response);
      for (Header header : response.getAllHeaders()) {
        retVal.headers.put(header.getName(), header.getValue());
      }
    }
    client.close();
    return retVal;
  }
Example #8
0
 /**
  * Write HTTP header for each secondary index
  *
  * @param httpMethod
  */
 @SuppressWarnings("rawtypes")
 private void writeIndexes(HttpRequestBase httpMethod) {
   // write 2i headers
   synchronized (indexLock) {
     for (RiakIndex i : indexes) {
       String index = i.getName();
       httpMethod.addHeader(Constants.HDR_SEC_INDEX_REQ_PREFIX + index, i.getValue().toString());
     }
   }
 }
 public void commitHeaders(ClientRequest request, HttpRequestBase httpMethod) {
   MultivaluedMap<String, String> headers = request.getHeaders();
   for (Map.Entry<String, List<String>> header : headers.entrySet()) {
     List<String> values = header.getValue();
     for (String value : values) {
       //               System.out.println(String.format("setting %s = %s", header.getKey(),
       // value));
       httpMethod.addHeader(header.getKey(), value);
     }
   }
 }
Example #10
0
 public HttpOperation(HttpOperationClient httpoperationclient, HttpRequestBase httprequestbase, HttpOperationReader httpoperationreader, VineAPI vineapi)
 {
     httprequestbase.addHeader("Accept-Encoding", "gzip");
     httprequestbase.addHeader("User-Agent", USER_AGENT_STRING);
     if (vineapi != null)
     {
         vineapi.addClientHeaders(httprequestbase);
     }
     mHttpClient = httpoperationclient.getHttpClient();
     mHttpRequest = httprequestbase;
     if (httpoperationreader == null)
     {
         mReader = DEFAULT_READER;
         return;
     } else
     {
         mReader = httpoperationreader;
         return;
     }
 }
Example #11
0
  /**
   * Serializes this object to an existing {@link HttpRequestBase} which can be sent as an HTTP
   * request. Specifically, sends the object's link, user-defined metadata and vclock as HTTP
   * headers and the value as the body. Used by {@link RiakClient} to create PUT requests.
   *
   * <p>if the this RiakObject's value is a stream, and no length is set, the stream is first
   * buffered into a byte array before being written
   */
  public void writeToHttpMethod(HttpRequestBase httpMethod) {
    // Serialize headers
    String basePath = getBasePathFromHttpMethod(httpMethod);
    writeLinks(httpMethod, basePath);
    for (String name : userMetaData.keySet()) {
      httpMethod.addHeader(Constants.HDR_USERMETA_REQ_PREFIX + name, userMetaData.get(name));
    }

    writeIndexes(httpMethod);

    if (vclock != null) {
      httpMethod.addHeader(Constants.HDR_VCLOCK, vclock);
    }

    // Serialize body
    if (httpMethod instanceof HttpEntityEnclosingRequestBase) {
      HttpEntityEnclosingRequestBase entityEnclosingMethod =
          (HttpEntityEnclosingRequestBase) httpMethod;
      AbstractHttpEntity entity = null;
      // Any value set using setValueAsStream() has precedent over value
      // set using setValue()
      if (valueStream != null) {
        if (valueStreamLength != null && valueStreamLength >= 0) {
          entity = new InputStreamEntity(valueStream, valueStreamLength);
        } else {
          // since apache http client 4.1 no longer supports buffering stream entities, but we can't
          // change API
          // behaviour, here we have to buffer the whole content
          entity = new ByteArrayEntity(ClientUtils.bufferStream(valueStream));
        }
      } else if (value != null) {
        entity = new ByteArrayEntity(value);
      } else {
        entity = new ByteArrayEntity(EMPTY);
      }
      entity.setContentType(contentType);
      entityEnclosingMethod.setEntity(entity);
    }
  }
  private JSONObject fastRequestHelper(HttpRequestBase request)
      throws IOException, org.json.simple.parser.ParseException {

    request.addHeader("authToken", authToken);
    request.addHeader("requestToken", requestToken);
    request.addHeader("appKey", penguinAppId);

    HttpResponse response = httpclient.execute(request, localContext);
    HttpEntity entity = response.getEntity();

    InputStreamReader in =
        zip
            ? new InputStreamReader(new GZIPInputStream(entity.getContent()))
            : new InputStreamReader(entity.getContent());

    Object result;
    synchronized (jsonParser) { // Parser is not thread safe!
      result = jsonParser.parse(in);
    }
    System.out.println("fastRequest" + (JSONObject) result);
    return (JSONObject) result;
  }
Example #13
0
 protected void addHeaders(HttpHeaders headers, HttpRequestBase uriRequest) {
   if (headers == null) {
     headers = new HttpHeaders();
   }
   // Add default accept headers
   if (!headers.containsKey(HttpHeaders.ACCEPT)) {
     headers.set(HttpHeaders.ACCEPT, "*/*");
   }
   // Add default user agent
   if (!headers.containsKey(HttpHeaders.USER_AGENT)) {
     headers.set(HttpHeaders.USER_AGENT, "apache/httpclient4");
   }
   for (Iterator<Entry<String, List<String>>> headerIterator = headers.entrySet().iterator();
       headerIterator.hasNext(); ) {
     Entry<String, List<String>> header = headerIterator.next();
     if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
       uriRequest.addHeader(header.getKey(), StringUtil.join(header.getValue(), ';'));
     } else {
       for (String headerValue : header.getValue()) {
         uriRequest.addHeader(header.getKey(), headerValue != null ? headerValue : "");
       }
     }
   }
 }
  /**
   * Copy header values from source request to the http method.
   *
   * @param method
   */
  private void setHeaders(HttpRequestBase method) {
    final Enumeration<String> headerNames = sourceRequest.getHeaderNames();

    while (headerNames.hasMoreElements()) {
      final String headerName = headerNames.nextElement();

      if (excludeHeader(headerName)) {
        continue;
      }

      final Enumeration<String> headerValues = sourceRequest.getHeaders(headerName);

      while (headerValues.hasMoreElements()) {
        String headerValue = headerValues.nextElement();
        method.addHeader(headerName, processHeaderValue(headerName, headerValue));
      }
    }
  }
  /**
   * @param request
   * @return
   */
  private HttpClientResponse execute(HttpRequestBase request) throws HttpProcessException {

    try {

      // add user agent

      request.addHeader("User-Agent", createUserAgentHeaderValue());

      final HttpResponse apacheResponse = apacheClient.execute(request);

      return new HttpClientResponseAdapter(apacheResponse);

    } catch (ClientProtocolException e) {
      throw new HttpProtocolException(e);
    } catch (IOException e) {
      throw new HttpProtocolException(ManagedIOException.manage(e));
    }
  }
  // create http request
  private HttpRequestBase getHttpRequest() {
    HttpRequestBase httpRequest = null;

    if (method == null) {
      method = RestMethod.GET;
    }

    // set method
    switch (method) {
      case GET:
        httpRequest = new HttpGet(getFinalURL().toString());
        break;
      case DELETE:
        httpRequest = new HttpDelete(getFinalURL().toString());
        break;
      case POST:
        HttpPost postRequest = new HttpPost(getFinalURL().toString());
        if (multipartEntity != null) {
          postRequest.setEntity(multipartEntity);
        } else {
          postRequest.setEntity(getBody());
        }
        httpRequest = postRequest;
        break;
      case PUT:
        HttpPut putRequest = new HttpPut(getFinalURL().toString());
        putRequest.setEntity(getBody());
        httpRequest = putRequest;
        break;
      default:
        break;
    }

    // set headers
    if (headers != null) {
      for (String key : headers.keySet()) {
        httpRequest.addHeader(key, headers.get(key));
      }
    }

    return httpRequest;
  }
Example #17
0
 @Override
 public Response execute(Request request) {
   if (!(request instanceof HttpRemoteRequest)) {
     throw new IllegalArgumentException("必须是HttpRequest才能执行!,参数是:" + request);
   }
   try {
     // 添加用户来源标记
     request.addParameter("userRefer", 1);
     HttpRemoteRequest httpRequest = (HttpRemoteRequest) request;
     HttpRequestBase httpRequestBase = asHttpRequest(httpRequest);
     String sessionId = request.getSessionId();
     if (!StringUtil.isEmpty(sessionId)) {
       httpRequestBase.addHeader("Cookie", JSESSION_STR + sessionId);
     }
     return handleHttpRequest(httpRequestBase);
   } catch (IOException e) {
     Log.e("HttpRemoteManager", "execute http", e);
     return new Response(MessageCodes.CONNECT_FAILED, "连接服务器失败");
   }
 }
 protected void configureRequest(final HttpRequestBase request) {
   request.addHeader(HttpHeaders.ACCEPT, CONTENT_TYPE_JSON);
   request.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
 }
Example #19
0
  /**
   * The handler method that invokes the actual HTTP service when the component is used as a HTTP
   * consumer.
   *
   * @param exchange the Exchange
   * @throws HandlerException handler exception
   */
  @Override
  public void handleMessage(final Exchange exchange) throws HandlerException {
    HttpClient httpclient = new DefaultHttpClient();
    try {
      HttpMessage httpRequest = _messageComposer.decompose(exchange, new HttpRequestMessage());
      HttpRequestBase request = null;
      if (_httpMethod.equals(HTTP_GET)) {
        request = new HttpGet(_baseAddress);
      } else if (_httpMethod.equals(HTTP_POST)) {
        request = new HttpPost(_baseAddress);
        ((HttpPost) request).setEntity(new ByteArrayEntity(httpRequest.getBodyBytes()));
      } else if (_httpMethod.equals(HTTP_DELETE)) {
        request = new HttpDelete(_baseAddress);
      } else if (_httpMethod.equals(HTTP_HEAD)) {
        request = new HttpHead(_baseAddress);
      } else if (_httpMethod.equals(HTTP_PUT)) {
        request = new HttpPut(_baseAddress);
        ((HttpPut) request).setEntity(new ByteArrayEntity(httpRequest.getBodyBytes()));
      } else if (_httpMethod.equals(HTTP_OPTIONS)) {
        request = new HttpOptions(_baseAddress);
      }
      Iterator<Map.Entry<String, List<String>>> entries =
          httpRequest.getHeaders().entrySet().iterator();
      while (entries.hasNext()) {
        Map.Entry<String, List<String>> entry = entries.next();
        String name = entry.getKey();
        List<String> values = entry.getValue();
        for (String value : values) {
          request.addHeader(name, value);
        }
      }
      if (_contentType != null) {
        request.addHeader("Content-Type", _contentType);
      }

      HttpResponse response = httpclient.execute(request);
      int status = response.getStatusLine().getStatusCode();
      if (status == HttpServletResponse.SC_OK) {
        HttpEntity entity = response.getEntity();
        HttpResponseMessage httpResponse = new HttpResponseMessage();
        Header[] headers = response.getAllHeaders();
        for (Header header : headers) {
          httpResponse.addHeader(header.getName(), header.getValue());
        }
        if (entity != null) {
          if (entity.getContentType() != null) {
            httpResponse.setContentType(new ContentType(entity.getContentType().getValue()));
          } else {
            httpResponse.setContentType(new ContentType());
          }
          httpResponse.setBodyFromStream(entity.getContent());
        }
        httpResponse.setStatus(status);
        Message out = _messageComposer.compose(httpResponse, exchange, true);
        exchange.send(out);
      }
    } catch (Exception e) {
      throw new HandlerException("Unexpected exception handling HTTP Message", e);
    } finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      httpclient.getConnectionManager().shutdown();
    }
  }
Example #20
0
  private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException {
    HttpRequestBase method = null;
    HttpEntity httpEntity = null;
    try {
      String urlstr = coreUrl + queryParams.toQueryString();

      boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod());
      if ("GET".equals(req.getMethod())) {
        method = new HttpGet(urlstr);
      } else if ("HEAD".equals(req.getMethod())) {
        method = new HttpHead(urlstr);
      } else if (isPostOrPutRequest) {
        HttpEntityEnclosingRequestBase entityRequest =
            "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr);
        InputStream in =
            new CloseShieldInputStream(req.getInputStream()); // Prevent close of container streams
        HttpEntity entity = new InputStreamEntity(in, req.getContentLength());
        entityRequest.setEntity(entity);
        method = entityRequest;
      } else if ("DELETE".equals(req.getMethod())) {
        method = new HttpDelete(urlstr);
      } else {
        throw new SolrException(
            SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod());
      }

      for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
        String headerName = e.nextElement();
        if (!"host".equalsIgnoreCase(headerName)
            && !"authorization".equalsIgnoreCase(headerName)
            && !"accept".equalsIgnoreCase(headerName)) {
          method.addHeader(headerName, req.getHeader(headerName));
        }
      }
      // These headers not supported for HttpEntityEnclosingRequests
      if (method instanceof HttpEntityEnclosingRequest) {
        method.removeHeaders(TRANSFER_ENCODING_HEADER);
        method.removeHeaders(CONTENT_LENGTH_HEADER);
      }

      final HttpResponse response =
          solrDispatchFilter.httpClient.execute(
              method, HttpClientUtil.createNewHttpClientRequestContext());
      int httpStatus = response.getStatusLine().getStatusCode();
      httpEntity = response.getEntity();

      resp.setStatus(httpStatus);
      for (HeaderIterator responseHeaders = response.headerIterator();
          responseHeaders.hasNext(); ) {
        Header header = responseHeaders.nextHeader();

        // We pull out these two headers below because they can cause chunked
        // encoding issues with Tomcat
        if (header != null
            && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER)
            && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) {
          resp.addHeader(header.getName(), header.getValue());
        }
      }

      if (httpEntity != null) {
        if (httpEntity.getContentEncoding() != null)
          resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue());
        if (httpEntity.getContentType() != null)
          resp.setContentType(httpEntity.getContentType().getValue());

        InputStream is = httpEntity.getContent();
        OutputStream os = resp.getOutputStream();

        IOUtils.copyLarge(is, os);
      }

    } catch (IOException e) {
      sendError(
          new SolrException(
              SolrException.ErrorCode.SERVER_ERROR,
              "Error trying to proxy request for url: " + coreUrl,
              e));
    } finally {
      Utils.consumeFully(httpEntity);
    }
  }
  S3Response invoke(
      @Nullable String bucket, @Nullable String object, @Nullable String temporaryEndpoint)
      throws S3Exception, CloudException, InternalException {
    if (wire.isDebugEnabled()) {
      wire.debug("");
      wire.debug(
          "----------------------------------------------------------------------------------");
    }
    try {
      StringBuilder url = new StringBuilder();
      boolean leaveOpen = false;
      HttpRequestBase method;
      HttpClient client;
      int status;

      if (provider.getEC2Provider().isAWS()) {
        url.append("https://");
        if (temporaryEndpoint == null) {
          boolean validDomainName = isValidDomainName(bucket);
          if (bucket != null && validDomainName) {
            url.append(bucket);
            url.append(".");
          }
          url.append("s3.amazonaws.com/");
          if (bucket != null && !validDomainName) {
            url.append(bucket);
            url.append("/");
          }
        } else {
          url.append(temporaryEndpoint);
          url.append("/");
        }
      } else if (provider.getEC2Provider().isStorage()
          && "google".equalsIgnoreCase(provider.getProviderName())) {
        url.append("https://");
        if (temporaryEndpoint == null) {
          if (bucket != null) {
            url.append(bucket);
            url.append(".");
          }
          url.append("commondatastorage.googleapis.com/");
        } else {
          url.append(temporaryEndpoint);
          url.append("/");
        }
      } else {
        int idx = 0;

        if (!provider.getContext().getEndpoint().startsWith("http")) {
          url.append("https://");
        } else {
          idx = provider.getContext().getEndpoint().indexOf("https://");
          if (idx == -1) {
            idx = "http://".length();
            url.append("http://");
          } else {
            idx = "https://".length();
            url.append("https://");
          }
        }
        String service = "";
        if (provider.getEC2Provider().isEucalyptus()) {
          service = "Walrus/";
        }

        if (temporaryEndpoint == null) {
          url.append(provider.getContext().getEndpoint().substring(idx));
          if (!provider.getContext().getEndpoint().endsWith("/")) {
            url.append("/").append(service);
          } else {
            url.append(service);
          }
        } else {
          url.append(temporaryEndpoint);
          url.append("/");
          url.append(service);
        }
        if (bucket != null) {
          url.append(bucket);
          url.append("/");
        }
      }
      if (object != null) {
        url.append(object);
      } else if (parameters != null) {
        boolean first = true;

        if (object != null && object.indexOf('?') != -1) {
          first = false;
        }
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
          String key = entry.getKey();
          String val = entry.getValue();

          if (first) {
            url.append("?");
            first = false;
          } else {
            url.append("&");
          }
          if (val != null) {
            url.append(AWSCloud.encode(key, false));
            url.append("=");
            url.append(AWSCloud.encode(val, false));
          } else {
            url.append(AWSCloud.encode(key, false));
          }
        }
      }

      if (provider.getEC2Provider().isStorage()
          && provider.getProviderName().equalsIgnoreCase("Google")) {
        headers.put(AWSCloud.P_GOOG_DATE, getDate());
      } else {
        headers.put(AWSCloud.P_AWS_DATE, getDate());
      }
      method = action.getMethod(url.toString());
      if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
          method.addHeader(entry.getKey(), entry.getValue());
        }
      }
      if (contentType == null && body != null) {
        contentType = "application/xml";
        method.addHeader("Content-Type", contentType);
      }
      try {
        String hash = null;
        String signature;

        signature =
            provider.signS3(
                new String(provider.getContext().getAccessPublic(), "utf-8"),
                provider.getContext().getAccessPrivate(),
                method.getMethod(),
                hash,
                contentType,
                headers,
                bucket,
                object);
        method.addHeader(AWSCloud.P_CFAUTH, signature);
      } catch (UnsupportedEncodingException e) {
        logger.error(e);
        e.printStackTrace();
        throw new InternalException(e);
      }
      if (body != null) {
        try {
          ((HttpEntityEnclosingRequestBase) method)
              .setEntity(new StringEntity(body, "application/xml", "utf-8"));
        } catch (UnsupportedEncodingException e) {
          throw new InternalException(e);
        }
      } else if (uploadFile != null) {
        ((HttpEntityEnclosingRequestBase) method)
            .setEntity(new FileEntity(uploadFile, contentType));
      }
      attempts++;
      client = getClient(url.toString(), body == null && uploadFile == null);

      if (wire.isDebugEnabled()) {
        wire.debug("[" + url.toString() + "]");
        wire.debug(method.getRequestLine().toString());
        for (Header header : method.getAllHeaders()) {
          wire.debug(header.getName() + ": " + header.getValue());
        }
        wire.debug("");
        if (body != null) {
          try {
            wire.debug(EntityUtils.toString(((HttpEntityEnclosingRequestBase) method).getEntity()));
          } catch (IOException ignore) {
          }

          wire.debug("");
        } else if (uploadFile != null) {
          wire.debug("-- file upload --");
          wire.debug("");
        }
      }
      S3Response response = new S3Response();
      HttpResponse httpResponse;

      try {
        httpResponse = client.execute(method);
        if (wire.isDebugEnabled()) {
          wire.debug(httpResponse.getStatusLine().toString());
          for (Header header : httpResponse.getAllHeaders()) {
            wire.debug(header.getName() + ": " + header.getValue());
          }
          wire.debug("");
        }
        status = httpResponse.getStatusLine().getStatusCode();
      } catch (IOException e) {
        logger.error(url + ": " + e.getMessage());
        e.printStackTrace();
        throw new InternalException(e);
      }
      response.headers = httpResponse.getAllHeaders();

      HttpEntity entity = httpResponse.getEntity();
      InputStream input = null;

      if (entity != null) {
        try {
          input = entity.getContent();
        } catch (IOException e) {
          throw new CloudException(e);
        }
      }
      try {
        if (status == HttpServletResponse.SC_OK
            || status == HttpServletResponse.SC_CREATED
            || status == HttpServletResponse.SC_ACCEPTED) {
          Header clen = httpResponse.getFirstHeader("Content-Length");
          long len = -1L;

          if (clen != null) {
            len = Long.parseLong(clen.getValue());
          }
          if (len != 0L) {
            try {
              Header ct = httpResponse.getFirstHeader("Content-Type");

              if (ct != null
                  && (ct.getValue().startsWith("application/xml")
                      || ct.getValue().startsWith("text/xml"))) {
                try {
                  response.document = parseResponse(input);
                  return response;
                } finally {
                  input.close();
                }
              } else if (ct != null
                  && ct.getValue().startsWith("application/octet-stream")
                  && len < 1) {
                return null;
              } else {
                response.contentLength = len;
                if (ct != null) {
                  response.contentType = ct.getValue();
                }
                response.input = input;
                response.method = method;
                leaveOpen = true;
                return response;
              }
            } catch (IOException e) {
              logger.error(e);
              e.printStackTrace();
              throw new CloudException(e);
            }
          } else {
            return response;
          }
        } else if (status == HttpServletResponse.SC_NO_CONTENT) {
          return response;
        } else if (status == HttpServletResponse.SC_NOT_FOUND) {
          throw new S3Exception(status, null, null, "Object not found.");
        } else {
          if (status == HttpServletResponse.SC_SERVICE_UNAVAILABLE
              || status == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
            if (attempts >= 5) {
              String msg;

              if (status == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {
                msg = "Cloud service is currently unavailable.";
              } else {
                msg = "The cloud service encountered a server error while processing your request.";
              }
              logger.error(msg);
              throw new CloudException(msg);
            } else {
              leaveOpen = true;
              if (input != null) {
                try {
                  input.close();
                } catch (IOException ignore) {
                }
              }
              try {
                Thread.sleep(5000L);
              } catch (InterruptedException ignore) {
              }
              return invoke(bucket, object);
            }
          }
          try {
            Document doc;

            try {
              logger.warn("Received error code: " + status);
              doc = parseResponse(input);
            } finally {
              input.close();
            }
            if (doc != null) {
              String endpoint = null, code = null, message = null, requestId = null;
              NodeList blocks = doc.getElementsByTagName("Error");

              if (blocks.getLength() > 0) {
                Node error = blocks.item(0);
                NodeList attrs;

                attrs = error.getChildNodes();
                for (int i = 0; i < attrs.getLength(); i++) {
                  Node attr = attrs.item(i);

                  if (attr.getNodeName().equals("Code") && attr.hasChildNodes()) {
                    code = attr.getFirstChild().getNodeValue().trim();
                  } else if (attr.getNodeName().equals("Message") && attr.hasChildNodes()) {
                    message = attr.getFirstChild().getNodeValue().trim();
                  } else if (attr.getNodeName().equals("RequestId") && attr.hasChildNodes()) {
                    requestId = attr.getFirstChild().getNodeValue().trim();
                  } else if (attr.getNodeName().equals("Endpoint") && attr.hasChildNodes()) {
                    endpoint = attr.getFirstChild().getNodeValue().trim();
                  }
                }
              }
              if (endpoint != null && code.equals("TemporaryRedirect")) {
                if (temporaryEndpoint != null) {
                  throw new CloudException("Too deep redirect to " + endpoint);
                } else {
                  return invoke(bucket, object, endpoint);
                }
              } else {
                if (message == null) {
                  throw new CloudException(
                      "Unable to identify error condition: "
                          + status
                          + "/"
                          + requestId
                          + "/"
                          + code);
                }
                throw new S3Exception(status, requestId, code, message);
              }
            } else {
              throw new CloudException("Unable to parse error.");
            }
          } catch (IOException e) {
            if (status == HttpServletResponse.SC_FORBIDDEN) {
              throw new S3Exception(
                  status, "", "AccessForbidden", "Access was denied without explanation.");
            }
            throw new CloudException(e);
          } catch (RuntimeException e) {
            throw new CloudException(e);
          } catch (Error e) {
            throw new CloudException(e);
          }
        }
      } finally {
        if (!leaveOpen) {
          if (input != null) {
            try {
              input.close();
            } catch (IOException ignore) {
            }
          }
        }
      }
    } finally {
      if (wire.isDebugEnabled()) {
        wire.debug(
            "----------------------------------------------------------------------------------");
        wire.debug("");
      }
    }
  }
 public void addRequestHeader(String key, String value) {
   method.addHeader(key, value);
 }
Example #23
0
  /**
   * Generate and make the necessary HTTP request.
   *
   * @return
   * @throws IOException
   * @throws ClientProtocolException
   * @throws URISyntaxException
   * @throws Exception
   */
  public HttpResponse generateRequest()
      throws ClientProtocolException, IOException, URISyntaxException {

    String URL = this.getRequestURL();
    // List<NameValuePair> queryParamsNVPair = null;
    HttpRequestBase request = null;

    switch (method) {
      case "GET":
        request = new HttpGet(URL);
        for (String headerKey : this.headers.keySet()) {
          request.addHeader(headerKey, headers.get(headerKey));
        }
        break;
      case "POST":
        HttpPost postRequest = new HttpPost(URL);
        for (String headerKey : this.headers.keySet()) {
          postRequest.addHeader(headerKey, headers.get(headerKey));
        }
        if (bodyParams != null) {
          StringEntity requestBodySE = new StringEntity(bodyParams.toString());
          requestBodySE.setContentType("application/json");
          postRequest.setEntity(requestBodySE);
        }
        request = postRequest;
        break;
      case "PUT":
        HttpPut putRequest = new HttpPut(URL);
        for (String headerKey : this.headers.keySet()) {
          putRequest.addHeader(headerKey, headers.get(headerKey));
        }
        if (bodyParams != null) {
          StringEntity requestBodySE = new StringEntity(bodyParams.toString());
          System.out.println(requestBodySE.toString());
          requestBodySE.setContentEncoding("application/json");
          putRequest.setEntity(requestBodySE);
        }
        request = putRequest;
        System.out.println(request.getURI().toString());
        break;
      case "DELETE":
        request = new HttpDelete(URL);
        for (String headerKey : this.headers.keySet()) {
          request.addHeader(headerKey, headers.get(headerKey));
        }
        break;
      default:
        break;
    }

    if (queryParams != null) {
      URIBuilder getUriBuilder = new URIBuilder(request.getURI());
      Iterator<?> paramKeys = queryParams.keys();
      while (paramKeys.hasNext()) {
        String param = (String) paramKeys.next();
        try {
          getUriBuilder.addParameter(param, queryParams.getString(param));
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
      URI getUri = getUriBuilder.build();
      ((HttpRequestBase) request).setURI(getUri);
    }

    HttpResponse response = sbgClient.execute(request);

    return response;
  }
 /* (non-Javadoc)
  * @see java.net.URLConnection#getInputStream()
  */
 @Override
 public InputStream getInputStream() throws IOException {
   try {
     if (m_client == null) {
       connect();
     }
     // Build URL
     int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort();
     URIBuilder ub = new URIBuilder();
     ub.setPort(port);
     ub.setScheme(m_url.getProtocol());
     ub.setHost(m_url.getHost());
     ub.setPath(m_url.getPath());
     ub.setQuery(m_url.getQuery());
     // Build Request
     HttpRequestBase request = null;
     if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) {
       final Content cnt = m_request.getContent();
       HttpPost post = new HttpPost(ub.build());
       ContentType contentType = ContentType.create(cnt.getType());
       LOG.info("Processing POST request for %s", contentType);
       if (contentType
           .getMimeType()
           .equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
         FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData());
         post.setEntity(fields.getEntity());
       } else {
         StringEntity entity = new StringEntity(cnt.getData(), contentType);
         post.setEntity(entity);
       }
       request = post;
     } else {
       request = new HttpGet(ub.build());
     }
     if (m_request != null) {
       // Add Custom Headers
       for (Header header : m_request.getHeaders()) {
         request.addHeader(header.getName(), header.getValue());
       }
     }
     // Add User Authentication
     String[] userInfo = m_url.getUserInfo() == null ? null : m_url.getUserInfo().split(":");
     if (userInfo != null) {
       UsernamePasswordCredentials credentials =
           new UsernamePasswordCredentials(userInfo[0], userInfo[1]);
       request.addHeader(BasicScheme.authenticate(credentials, "UTF-8", false));
     }
     // Get Response
     HttpResponse response = m_client.execute(request);
     return response.getEntity().getContent();
   } catch (Exception e) {
     throw new IOException(
         "Can't retrieve "
             + m_url.getPath()
             + " from "
             + m_url.getHost()
             + " because "
             + e.getMessage(),
         e);
   }
 }
Example #25
0
  @SuppressWarnings("deprecation")
  @Override
  public void filterHttpRequest(SubmitContext context, HttpRequestInterface<?> request) {
    HttpRequestBase httpMethod =
        (HttpRequestBase) context.getProperty(BaseHttpRequestTransport.HTTP_METHOD);

    String path = PropertyExpander.expandProperties(context, request.getPath());
    StringBuffer query = new StringBuffer();
    String encoding =
        System.getProperty("soapui.request.encoding", StringUtils.unquote(request.getEncoding()));

    StringToStringMap responseProperties =
        (StringToStringMap) context.getProperty(BaseHttpRequestTransport.RESPONSE_PROPERTIES);

    MimeMultipart formMp =
        "multipart/form-data".equals(request.getMediaType())
                && httpMethod instanceof HttpEntityEnclosingRequestBase
            ? new MimeMultipart()
            : null;

    RestParamsPropertyHolder params = request.getParams();

    for (int c = 0; c < params.getPropertyCount(); c++) {
      RestParamProperty param = params.getPropertyAt(c);

      String value = PropertyExpander.expandProperties(context, param.getValue());
      responseProperties.put(param.getName(), value);

      List<String> valueParts =
          sendEmptyParameters(request) || (!StringUtils.hasContent(value) && param.getRequired())
              ? RestUtils.splitMultipleParametersEmptyIncluded(
                  value, request.getMultiValueDelimiter())
              : RestUtils.splitMultipleParameters(value, request.getMultiValueDelimiter());

      // skip HEADER and TEMPLATE parameter encoding (TEMPLATE is encoded by
      // the URI handling further down)
      if (value != null
          && param.getStyle() != ParameterStyle.HEADER
          && param.getStyle() != ParameterStyle.TEMPLATE
          && !param.isDisableUrlEncoding()) {
        try {
          if (StringUtils.hasContent(encoding)) {
            value = URLEncoder.encode(value, encoding);
            for (int i = 0; i < valueParts.size(); i++)
              valueParts.set(i, URLEncoder.encode(valueParts.get(i), encoding));
          } else {
            value = URLEncoder.encode(value);
            for (int i = 0; i < valueParts.size(); i++)
              valueParts.set(i, URLEncoder.encode(valueParts.get(i)));
          }
        } catch (UnsupportedEncodingException e1) {
          SoapUI.logError(e1);
          value = URLEncoder.encode(value);
          for (int i = 0; i < valueParts.size(); i++)
            valueParts.set(i, URLEncoder.encode(valueParts.get(i)));
        }
        // URLEncoder replaces space with "+", but we want "%20".
        value = value.replaceAll("\\+", "%20");
        for (int i = 0; i < valueParts.size(); i++)
          valueParts.set(i, valueParts.get(i).replaceAll("\\+", "%20"));
      }

      if (param.getStyle() == ParameterStyle.QUERY && !sendEmptyParameters(request)) {
        if (!StringUtils.hasContent(value) && !param.getRequired()) continue;
      }

      switch (param.getStyle()) {
        case HEADER:
          for (String valuePart : valueParts) httpMethod.addHeader(param.getName(), valuePart);
          break;
        case QUERY:
          if (formMp == null || !request.isPostQueryString()) {
            for (String valuePart : valueParts) {
              if (query.length() > 0) query.append('&');

              query.append(URLEncoder.encode(param.getName()));
              query.append('=');
              if (StringUtils.hasContent(valuePart)) query.append(valuePart);
            }
          } else {
            try {
              addFormMultipart(
                  request, formMp, param.getName(), responseProperties.get(param.getName()));
            } catch (MessagingException e) {
              SoapUI.logError(e);
            }
          }

          break;
        case TEMPLATE:
          try {
            value =
                getEncodedValue(
                    value,
                    encoding,
                    param.isDisableUrlEncoding(),
                    request.getSettings().getBoolean(HttpSettings.ENCODED_URLS));
            path = path.replaceAll("\\{" + param.getName() + "\\}", value == null ? "" : value);
          } catch (UnsupportedEncodingException e) {
            SoapUI.logError(e);
          }
          break;
        case MATRIX:
          try {
            value =
                getEncodedValue(
                    value,
                    encoding,
                    param.isDisableUrlEncoding(),
                    request.getSettings().getBoolean(HttpSettings.ENCODED_URLS));
          } catch (UnsupportedEncodingException e) {
            SoapUI.logError(e);
          }

          if (param.getType().equals(XmlBoolean.type.getName())) {
            if (value.toUpperCase().equals("TRUE") || value.equals("1")) {
              path += ";" + param.getName();
            }
          } else {
            path += ";" + param.getName();
            if (StringUtils.hasContent(value)) {
              path += "=" + value;
            }
          }
          break;
        case PLAIN:
          break;
      }
    }

    if (request.getSettings().getBoolean(HttpSettings.FORWARD_SLASHES))
      path = PathUtils.fixForwardSlashesInPath(path);

    if (PathUtils.isHttpPath(path)) {
      try {
        // URI(String) automatically URLencodes the input, so we need to
        // decode it first...
        URI uri = new URI(path, request.getSettings().getBoolean(HttpSettings.ENCODED_URLS));
        context.setProperty(BaseHttpRequestTransport.REQUEST_URI, uri);
        java.net.URI oldUri = httpMethod.getURI();
        httpMethod.setURI(
            HttpUtils.createUri(
                oldUri.getScheme(),
                oldUri.getRawUserInfo(),
                oldUri.getHost(),
                oldUri.getPort(),
                oldUri.getRawPath(),
                uri.getEscapedQuery(),
                oldUri.getRawFragment()));
      } catch (Exception e) {
        SoapUI.logError(e);
      }
    } else if (StringUtils.hasContent(path)) {
      try {
        java.net.URI oldUri = httpMethod.getURI();
        String pathToSet =
            StringUtils.hasContent(oldUri.getRawPath()) && !"/".equals(oldUri.getRawPath())
                ? oldUri.getRawPath() + path
                : path;
        java.net.URI newUri =
            URIUtils.createURI(
                oldUri.getScheme(),
                oldUri.getHost(),
                oldUri.getPort(),
                pathToSet,
                oldUri.getQuery(),
                oldUri.getFragment());
        httpMethod.setURI(newUri);
        context.setProperty(
            BaseHttpRequestTransport.REQUEST_URI,
            new URI(
                newUri.toString(), request.getSettings().getBoolean(HttpSettings.ENCODED_URLS)));
      } catch (Exception e) {
        SoapUI.logError(e);
      }
    }

    if (query.length() > 0 && !request.isPostQueryString()) {
      try {
        java.net.URI oldUri = httpMethod.getURI();
        httpMethod.setURI(
            URIUtils.createURI(
                oldUri.getScheme(),
                oldUri.getHost(),
                oldUri.getPort(),
                oldUri.getRawPath(),
                query.toString(),
                oldUri.getFragment()));
      } catch (Exception e) {
        SoapUI.logError(e);
      }
    }

    if (request instanceof RestRequest) {
      String acceptEncoding = ((RestRequest) request).getAccept();
      if (StringUtils.hasContent(acceptEncoding)) {
        httpMethod.setHeader("Accept", acceptEncoding);
      }
    }

    if (formMp != null) {
      // create request message
      try {
        if (request.hasRequestBody() && httpMethod instanceof HttpEntityEnclosingRequest) {
          String requestContent =
              PropertyExpander.expandProperties(
                  context, request.getRequestContent(), request.isEntitizeProperties());
          if (StringUtils.hasContent(requestContent)) {
            initRootPart(request, requestContent, formMp);
          }
        }

        for (Attachment attachment : request.getAttachments()) {
          MimeBodyPart part = new PreencodedMimeBodyPart("binary");

          if (attachment instanceof FileAttachment<?>) {
            String name = attachment.getName();
            if (StringUtils.hasContent(attachment.getContentID())
                && !name.equals(attachment.getContentID())) name = attachment.getContentID();

            part.setDisposition(
                "form-data; name=\"" + name + "\"; filename=\"" + attachment.getName() + "\"");
          } else part.setDisposition("form-data; name=\"" + attachment.getName() + "\"");

          part.setDataHandler(new DataHandler(new AttachmentDataSource(attachment)));

          formMp.addBodyPart(part);
        }

        MimeMessage message = new MimeMessage(AttachmentUtils.JAVAMAIL_SESSION);
        message.setContent(formMp);
        message.saveChanges();
        RestRequestMimeMessageRequestEntity mimeMessageRequestEntity =
            new RestRequestMimeMessageRequestEntity(message, request);
        ((HttpEntityEnclosingRequest) httpMethod).setEntity(mimeMessageRequestEntity);
        httpMethod.setHeader("Content-Type", mimeMessageRequestEntity.getContentType().getValue());
        httpMethod.setHeader("MIME-Version", "1.0");
      } catch (Throwable e) {
        SoapUI.logError(e);
      }
    } else if (request.hasRequestBody() && httpMethod instanceof HttpEntityEnclosingRequest) {
      if (StringUtils.hasContent(request.getMediaType()))
        httpMethod.setHeader(
            "Content-Type", getContentTypeHeader(request.getMediaType(), encoding));

      if (request.isPostQueryString()) {
        try {
          ((HttpEntityEnclosingRequest) httpMethod).setEntity(new StringEntity(query.toString()));
        } catch (UnsupportedEncodingException e) {
          SoapUI.logError(e);
        }
      } else {
        String requestContent =
            PropertyExpander.expandProperties(
                context, request.getRequestContent(), request.isEntitizeProperties());
        List<Attachment> attachments = new ArrayList<Attachment>();

        for (Attachment attachment : request.getAttachments()) {
          if (attachment.getContentType().equals(request.getMediaType())) {
            attachments.add(attachment);
          }
        }

        if (StringUtils.hasContent(requestContent) && attachments.isEmpty()) {
          try {
            byte[] content =
                encoding == null ? requestContent.getBytes() : requestContent.getBytes(encoding);
            ((HttpEntityEnclosingRequest) httpMethod).setEntity(new ByteArrayEntity(content));
          } catch (UnsupportedEncodingException e) {
            ((HttpEntityEnclosingRequest) httpMethod)
                .setEntity(new ByteArrayEntity(requestContent.getBytes()));
          }
        } else if (attachments.size() > 0) {
          try {
            MimeMultipart mp = null;

            if (StringUtils.hasContent(requestContent)) {
              mp = new MimeMultipart();
              initRootPart(request, requestContent, mp);
            } else if (attachments.size() == 1) {
              ((HttpEntityEnclosingRequest) httpMethod)
                  .setEntity(new InputStreamEntity(attachments.get(0).getInputStream(), -1));

              httpMethod.setHeader(
                  "Content-Type", getContentTypeHeader(request.getMediaType(), encoding));
            }

            if (((HttpEntityEnclosingRequest) httpMethod).getEntity() == null) {
              if (mp == null) mp = new MimeMultipart();

              // init mimeparts
              AttachmentUtils.addMimeParts(request, attachments, mp, new StringToStringMap());

              // create request message
              MimeMessage message = new MimeMessage(AttachmentUtils.JAVAMAIL_SESSION);
              message.setContent(mp);
              message.saveChanges();
              RestRequestMimeMessageRequestEntity mimeMessageRequestEntity =
                  new RestRequestMimeMessageRequestEntity(message, request);
              ((HttpEntityEnclosingRequest) httpMethod).setEntity(mimeMessageRequestEntity);
              httpMethod.setHeader(
                  "Content-Type",
                  getContentTypeHeader(
                      mimeMessageRequestEntity.getContentType().getValue(), encoding));
              httpMethod.setHeader("MIME-Version", "1.0");
            }
          } catch (Exception e) {
            SoapUI.logError(e);
          }
        }
      }
    }
  }
Example #26
0
  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();
        }
      }
    }
  }
 /**
  * @param req
  * @param headers
  */
 private void addHeaders(HttpRequestBase req, Map<String, String> headers) {
   if (headers == null) return;
   for (Map.Entry<String, String> h : headers.entrySet()) {
     req.addHeader(h.getKey(), h.getValue());
   }
 }
Example #28
0
  /**
   * @param uri Url.
   * @param params Params.
   * @param demo Use demo node.
   * @param mtd Method.
   * @param headers Headers.
   * @param body Body.
   */
  protected RestResult executeRest(
      String uri,
      Map<String, Object> params,
      boolean demo,
      String mtd,
      Map<String, Object> headers,
      String body)
      throws IOException, URISyntaxException {
    if (log.isDebugEnabled())
      log.debug(
          "Start execute REST command [method="
              + mtd
              + ", uri=/"
              + (uri == null ? "" : uri)
              + ", parameters="
              + params
              + "]");

    final URIBuilder builder;

    if (demo) {
      // try start demo if needed.
      AgentClusterDemo.testDrive(cfg);

      // null if demo node not started yet.
      if (cfg.demoNodeUri() == null) return RestResult.fail("Demo node is not started yet.", 404);

      builder = new URIBuilder(cfg.demoNodeUri());
    } else builder = new URIBuilder(cfg.nodeUri());

    if (builder.getPort() == -1) builder.setPort(DFLT_NODE_PORT);

    if (uri != null) {
      if (!uri.startsWith("/") && !cfg.nodeUri().endsWith("/")) uri = '/' + uri;

      builder.setPath(uri);
    }

    if (params != null) {
      for (Map.Entry<String, Object> entry : params.entrySet()) {
        if (entry.getValue() != null)
          builder.addParameter(entry.getKey(), entry.getValue().toString());
      }
    }

    HttpRequestBase httpReq = null;

    try {
      if ("GET".equalsIgnoreCase(mtd)) httpReq = new HttpGet(builder.build());
      else if ("POST".equalsIgnoreCase(mtd)) {
        HttpPost post;

        if (body == null) {
          List<NameValuePair> nvps = builder.getQueryParams();

          builder.clearParameters();

          post = new HttpPost(builder.build());

          if (!nvps.isEmpty()) post.setEntity(new UrlEncodedFormEntity(nvps));
        } else {
          post = new HttpPost(builder.build());

          post.setEntity(new StringEntity(body));
        }

        httpReq = post;
      } else throw new IOException("Unknown HTTP-method: " + mtd);

      if (headers != null) {
        for (Map.Entry<String, Object> entry : headers.entrySet())
          httpReq.addHeader(
              entry.getKey(), entry.getValue() == null ? null : entry.getValue().toString());
      }

      try (CloseableHttpResponse resp = httpClient.execute(httpReq)) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        resp.getEntity().writeTo(out);

        Charset charset = Charsets.UTF_8;

        Header encodingHdr = resp.getEntity().getContentEncoding();

        if (encodingHdr != null) {
          String encoding = encodingHdr.getValue();

          charset = Charsets.toCharset(encoding);
        }

        return RestResult.success(
            resp.getStatusLine().getStatusCode(), new String(out.toByteArray(), charset));
      } catch (ConnectException e) {
        log.info("Failed connect to node and execute REST command [uri=" + builder.build() + "]");

        return RestResult.fail("Failed connect to node and execute REST command.", 404);
      }
    } finally {
      if (httpReq != null) httpReq.reset();
    }
  }
  public void tempRedirectInvoke(
      @Nonnull String tempEndpoint,
      @Nonnull String method,
      @Nonnull String account,
      @Nonnull String resource,
      @Nonnull String body)
      throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
      logger.trace(
          "enter - " + AzureMethod.class.getName() + ".post(" + account + "," + resource + ")");
    }
    if (wire.isDebugEnabled()) {
      wire.debug(
          "POST --------------------------------------------------------> "
              + endpoint
              + account
              + resource);
      wire.debug("");
    }
    try {
      HttpClient client = getClient();
      String url = tempEndpoint + account + resource;

      HttpRequestBase httpMethod = getMethod(method, url);

      // If it is networking configuration services
      if (httpMethod instanceof HttpPut) {
        if (url.endsWith("/services/networking/media")) {
          httpMethod.addHeader("Content-Type", "text/plain");
        } else {
          httpMethod.addHeader("Content-Type", "application/xml;charset=UTF-8");
        }
      } else {
        httpMethod.addHeader("Content-Type", "application/xml;charset=UTF-8");
      }

      // dmayne version is older for anything to do with images and for disk deletion
      if (url.indexOf("/services/images") > -1
          || (httpMethod instanceof HttpDelete && url.indexOf("/services/disks") > -1)) {
        httpMethod.addHeader("x-ms-version", "2012-08-01");
      } else {
        httpMethod.addHeader("x-ms-version", "2012-03-01");
      }

      if (strategy != null && strategy.getSendAsHeader()) {
        httpMethod.addHeader(strategy.getHeaderName(), strategy.getRequestId());
      }

      if (wire.isDebugEnabled()) {
        wire.debug(httpMethod.getRequestLine().toString());
        for (Header header : httpMethod.getAllHeaders()) {
          wire.debug(header.getName() + ": " + header.getValue());
        }
        wire.debug("");
        if (body != null) {
          wire.debug(body);
          wire.debug("");
        }
      }

      if (httpMethod instanceof HttpEntityEnclosingRequestBase) {

        HttpEntityEnclosingRequestBase entityEnclosingMethod =
            (HttpEntityEnclosingRequestBase) httpMethod;

        if (body != null) {
          try {
            entityEnclosingMethod.setEntity(new StringEntity(body, "application/xml", "utf-8"));
          } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }

      HttpResponse response;
      StatusLine status;

      try {
        response = client.execute(httpMethod);
        status = response.getStatusLine();
      } catch (IOException e) {
        logger.error(
            "post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
        if (logger.isTraceEnabled()) {
          e.printStackTrace();
        }
        throw new CloudException(e);
      }
      if (logger.isDebugEnabled()) {
        logger.debug("post(): HTTP Status " + status);
      }
      Header[] headers = response.getAllHeaders();

      if (wire.isDebugEnabled()) {
        wire.debug(status.toString());
        for (Header h : headers) {
          if (h.getValue() != null) {
            wire.debug(h.getName() + ": " + h.getValue().trim());
          } else {
            wire.debug(h.getName() + ":");
          }
        }
        wire.debug("");
      }
      if (status.getStatusCode() != HttpServletResponse.SC_OK
          && status.getStatusCode() != HttpServletResponse.SC_CREATED
          && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED) {
        logger.error("post(): Expected OK for GET request, got " + status.getStatusCode());

        HttpEntity entity = response.getEntity();

        if (entity == null) {
          throw new AzureException(
              CloudErrorType.GENERAL,
              status.getStatusCode(),
              status.getReasonPhrase(),
              "An error was returned without explanation");
        }
        try {
          body = EntityUtils.toString(entity);
        } catch (IOException e) {
          throw new AzureException(
              CloudErrorType.GENERAL,
              status.getStatusCode(),
              status.getReasonPhrase(),
              e.getMessage());
        }
        if (wire.isDebugEnabled()) {
          wire.debug(body);
        }
        wire.debug("");
        AzureException.ExceptionItems items =
            AzureException.parseException(status.getStatusCode(), body);

        if (items == null) {
          throw new CloudException(
              CloudErrorType.GENERAL, status.getStatusCode(), "Unknown", "Unknown");
        }
        logger.error(
            "post(): [" + status.getStatusCode() + " : " + items.message + "] " + items.details);
        throw new AzureException(items);
      }
    } finally {
      if (logger.isTraceEnabled()) {
        logger.trace("exit - " + AzureMethod.class.getName() + ".post()");
      }
      if (wire.isDebugEnabled()) {
        wire.debug("");
        wire.debug(
            "POST --------------------------------------------------------> "
                + endpoint
                + account
                + resource);
      }
    }
  }