예제 #1
0
 /**
  * 将用户登录名展示在actionbar上,长度大于14用。。。表示
  *
  * @return
  */
 public static String showTitileName() {
   if (StringUtils.isNotEmpty(HttpClientUtil.getAccount().getName())) {
     if (HttpClientUtil.getAccount().getName().length() <= 14) {
       return HttpClientUtil.getAccount().getName();
     } else {
       return StringUtils.substring(HttpClientUtil.getAccount().getName(), 0, 14) + "..";
     }
   }
   return null;
 }
예제 #2
0
 /**
  * Set the maximum number of connections that can be open at any given time. If http client was
  * created outside the operation is not allowed.
  */
 public void setMaxTotalConnections(int max) {
   if (internalClient) {
     HttpClientUtil.setMaxConnections(httpClient, max);
   } else {
     throw new UnsupportedOperationException("Client was created outside of HttpSolrServer");
   }
 }
  /**
   * Create a new client object using multiple string values in a Collection instead of a standard
   * zkHost connection string. Note that this method will not be used if there is only one String
   * argument - that will use {@link #CloudSolrServer(String)} instead.
   *
   * @param zkHosts A Java Collection (List, Set, etc) of HOST:PORT strings, one for each host in
   *     the zookeeper ensemble. Note that with certain Collection types like HashSet, the order of
   *     hosts in the final connect string may not be in the same order you added them.
   * @param chroot A chroot value for zookeeper, starting with a forward slash. If no chroot is
   *     required, use null.
   * @param httpClient the {@link HttpClient} instance to be used for all requests. The provided
   *     httpClient should use a multi-threaded connection manager.
   * @throws IllegalArgumentException if the chroot value does not start with a forward slash.
   * @see #CloudSolrServer(String)
   */
  public CloudSolrServer(Collection<String> zkHosts, String chroot, HttpClient httpClient) {
    StringBuilder zkBuilder = new StringBuilder();
    int lastIndexValue = zkHosts.size() - 1;
    int i = 0;
    for (String zkHost : zkHosts) {
      zkBuilder.append(zkHost);
      if (i < lastIndexValue) {
        zkBuilder.append(",");
      }
      i++;
    }
    if (chroot != null) {
      if (chroot.startsWith("/")) {
        zkBuilder.append(chroot);
      } else {
        throw new IllegalArgumentException("The chroot must start with a forward slash.");
      }
    }

    /* Log the constructed connection string and then initialize. */
    log.info("Final constructed zkHost string: " + zkBuilder.toString());

    this.zkHost = zkBuilder.toString();
    this.clientIsInternal = httpClient == null;
    this.myClient = httpClient == null ? HttpClientUtil.createClient(null) : httpClient;
    this.lbServer = new LBHttpSolrServer(myClient);
    this.lbServer.setRequestWriter(new BinaryRequestWriter());
    this.lbServer.setParser(new BinaryResponseParser());
    this.updatesToLeaders = true;
    shutdownLBHttpSolrServer = true;
  }
예제 #4
0
  @Override
  protected void updateContext(final ProxyRepository repository, final RemoteStorageContext ctx) {
    getLogger()
        .info(
            "Remote storage settings change detected for ProxyRepository ID=\""
                + repository.getId()
                + "\" (\""
                + repository.getName()
                + "\"), updating HttpClient...");

    // reset current http client, if exists
    HttpClientUtil.release(CTX_KEY, ctx);

    // and create a new one
    HttpClientUtil.configure(CTX_KEY, ctx, getLogger());
  }
예제 #5
0
  public static String sendSSLPost(String url) throws ClientProtocolException, IOException {
    String line = null;
    StringBuilder stringBuilder = new StringBuilder();
    CloseableHttpClient httpClient = HttpClientUtil.createSSLClientDefault();
    HttpGet getdd = new HttpGet();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");

    // httpPost.setEntity(new StringEntity(JSON.toJSONString(param), "UTF-8"));

    HttpResponse response = httpClient.execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {

      org.apache.http.HttpEntity httpEntity = response.getEntity();

      if (httpEntity != null) {
        try {
          BufferedReader bufferedReader =
              new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"), 8 * 1024);
          while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
          }

        } catch (Exception e) {

        }
      }
    }
    return stringBuilder.toString();
  }
예제 #6
0
 private HttpRequestBase asHttpRequest(HttpRemoteRequest request) throws IOException {
   HttpRemoteRequest.Method method = request.getMethod();
   if (method == HttpRemoteRequest.Method.GET) {
     return HttpClientUtil.createGetMethod(request.getTarget(), request.getParameters());
   }
   if (method == HttpRemoteRequest.Method.POST) {
     if (CollectionUtil.isEmpty(request.getBinaryItems())) {
       return HttpClientUtil.createPostMethod(request.getTarget(), request.getParameters());
     }
     return HttpClientUtil.createMultipartPostMethod(
         request.getTarget(),
         request.getParameters(),
         request.getBinaryItems(),
         request.getUploadFileCallback());
   }
   throw new IllegalArgumentException("未知Method:" + method);
 }
예제 #7
0
 /**
  * Allow server->client communication to be compressed. Currently gzip and deflate are supported.
  * If the server supports compression the response will be compressed. This method is only allowed
  * if the http client is of type DefatulHttpClient.
  */
 public void setAllowCompression(boolean allowCompression) {
   if (httpClient instanceof DefaultHttpClient) {
     HttpClientUtil.setAllowCompression((DefaultHttpClient) httpClient, allowCompression);
   } else {
     throw new UnsupportedOperationException(
         "HttpClient instance was not of type DefaultHttpClient");
   }
 }
 /**
  * @param zkHost A zookeeper client endpoint.
  * @param updatesToLeaders If true, sends updates only to shard leaders.
  * @param httpClient the {@link HttpClient} instance to be used for all requests. The provided
  *     httpClient should use a multi-threaded connection manager.
  * @see #CloudSolrServer(String) for full description and details on zkHost
  */
 public CloudSolrServer(String zkHost, boolean updatesToLeaders, HttpClient httpClient) {
   this.zkHost = zkHost;
   this.clientIsInternal = httpClient == null;
   this.myClient = httpClient == null ? HttpClientUtil.createClient(null) : httpClient;
   this.lbServer = new LBHttpSolrServer(myClient);
   this.lbServer.setRequestWriter(new BinaryRequestWriter());
   this.lbServer.setParser(new BinaryResponseParser());
   this.updatesToLeaders = updatesToLeaders;
   shutdownLBHttpSolrServer = true;
   lbServer.addQueryParams(STATE_VERSION);
 }
 @Test
 public void testGetRawStream() throws SolrServerException, IOException {
   try (CloseableHttpClient client = HttpClientUtil.createClient(null)) {
     HttpSolrClient solrClient =
         new HttpSolrClient(jetty.getBaseUrl().toString() + "/collection1", client, null);
     QueryRequest req = new QueryRequest();
     NamedList response = solrClient.request(req);
     InputStream stream = (InputStream) response.get("stream");
     assertNotNull(stream);
     stream.close();
   }
 }
  @Test
  public void testCompression() throws Exception {
    try (HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {
      SolrQuery q = new SolrQuery("*:*");

      // verify request header gets set
      DebugServlet.clear();
      try {
        client.query(q);
      } catch (ParseException ignored) {
      }
      assertNull(DebugServlet.headers.get("Accept-Encoding"));
      client.setAllowCompression(true);
      try {
        client.query(q);
      } catch (ParseException ignored) {
      }
      assertNotNull(DebugServlet.headers.get("Accept-Encoding"));
      client.setAllowCompression(false);
      try {
        client.query(q);
      } catch (ParseException ignored) {
      }
      assertNull(DebugServlet.headers.get("Accept-Encoding"));
    }

    // verify server compresses output
    HttpGet get =
        new HttpGet(jetty.getBaseUrl().toString() + "/collection1" + "/select?q=foo&wt=xml");
    get.setHeader("Accept-Encoding", "gzip");
    CloseableHttpClient httpclient = HttpClientUtil.createClient(null);
    HttpEntity entity = null;
    try {
      HttpResponse response = httpclient.execute(get);
      entity = response.getEntity();
      Header ceheader = entity.getContentEncoding();
      assertEquals("gzip", ceheader.getValue());
    } finally {
      if (entity != null) {
        entity.getContent().close();
      }
      httpclient.close();
    }

    // verify compressed response can be handled
    try (HttpSolrClient client =
        new HttpSolrClient(jetty.getBaseUrl().toString() + "/collection1")) {
      client.setAllowCompression(true);
      SolrQuery q = new SolrQuery("foo");
      QueryResponse response = client.query(q);
      assertEquals(0, response.getStatus());
    }
  }
  /**
   * Set cookies via interceptor Change the request via an interceptor Ensure cookies are actually
   * set and that request is actually changed
   */
  @Test
  public void testInterceptors() {
    DebugServlet.clear();
    HttpClientUtil.addRequestInterceptor(changeRequestInterceptor);
    HttpClientUtil.addRequestInterceptor(cookieSettingRequestInterceptor);

    try (HttpSolrClient server = new HttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {

      SolrQuery q = new SolrQuery("foo");
      q.setParam("a", "\u1234");
      try {
        server.query(q, random().nextBoolean() ? METHOD.POST : METHOD.GET);
      } catch (Throwable t) {
      }

      // Assert cookies from UseContextCallback
      assertNotNull(DebugServlet.cookies);
      boolean foundCookie = false;
      for (javax.servlet.http.Cookie cookie : DebugServlet.cookies) {
        if (cookieName.equals(cookie.getName()) && cookieValue.equals(cookie.getValue())) {
          foundCookie = true;
          break;
        }
      }
      assertTrue(foundCookie);

      // Assert request changes by ChangeRequestCallback
      assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
      assertEquals("\u4321", DebugServlet.parameters.get("b")[0]);

    } catch (IOException ex) {
      throw new RuntimeException(ex);
    } finally {
      HttpClientUtil.removeRequestInterceptor(changeRequestInterceptor);
      HttpClientUtil.removeRequestInterceptor(cookieSettingRequestInterceptor);
    }
  }
  @Test
  public void testSetParametersExternalClient() throws IOException {

    try (CloseableHttpClient httpClient = HttpClientUtil.createClient(null);
        HttpSolrClient solrClient = new HttpSolrClient(jetty.getBaseUrl().toString(), httpClient)) {

      try {
        solrClient.setMaxTotalConnections(1);
        fail("Operation should not succeed.");
      } catch (UnsupportedOperationException ignored) {
      }
      try {
        solrClient.setDefaultMaxConnectionsPerHost(1);
        fail("Operation should not succeed.");
      } catch (UnsupportedOperationException ignored) {
      }
    }
  }
예제 #13
0
 private void accessService() {
   String spath;
   Set<String> srs = services.keySet();
   List<String> srsadds;
   for (String sr : srs) {
     if (services.get(sr) != null && services.get(sr).size() > 0) {
       srsadds = new ArrayList<>();
       srsadds.addAll(services.get(sr));
       Collections.shuffle(srsadds);
       spath = "http://" + srsadds.get(0) + ":8080" + sr;
       System.out.println("access path=" + spath);
       String res = "";
       try {
         res = HttpClientUtil.getMethod(spath);
       } catch (Exception e) {
         e.printStackTrace();
       }
       System.out.println("res=" + res);
     } else {
       System.out.println("access path " + sr + ",没有服务提供者");
     }
   }
 }
예제 #14
0
  public HttpSolrServer(String baseURL, HttpClient client, ResponseParser parser) {
    this.baseUrl = baseURL;
    if (baseUrl.endsWith("/")) {
      baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
    }
    if (baseUrl.indexOf('?') >= 0) {
      throw new RuntimeException(
          "Invalid base url for solrj.  The base URL must not contain parameters: " + baseUrl);
    }

    if (client != null) {
      httpClient = client;
      internalClient = false;
    } else {
      internalClient = true;
      ModifiableSolrParams params = new ModifiableSolrParams();
      params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, 128);
      params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 32);
      params.set(HttpClientUtil.PROP_FOLLOW_REDIRECTS, followRedirects);
      httpClient = HttpClientUtil.createClient(params);
    }

    this.parser = parser;
  }
예제 #15
0
  /**
   * Executes the HTTP request.
   *
   * <p>In case of any exception thrown by HttpClient, it will release the connection. In other
   * cases it is the duty of caller to do it, or process the input stream.
   *
   * @param repository to execute the HTTP method fpr
   * @param request resource store request that triggered the HTTP request
   * @param httpRequest HTTP request to be executed
   * @return response of making the request
   * @throws RemoteStorageException If an error occurred during execution of HTTP request
   */
  private HttpResponse executeRequest(
      final ProxyRepository repository,
      final ResourceStoreRequest request,
      final HttpUriRequest httpRequest)
      throws RemoteStorageException {
    final URI methodUri = httpRequest.getURI();

    if (getLogger().isDebugEnabled()) {
      getLogger()
          .debug(
              "Invoking HTTP "
                  + httpRequest.getMethod()
                  + " method against remote location "
                  + methodUri);
    }

    final RemoteStorageContext ctx = getRemoteStorageContext(repository);

    final HttpClient httpClient = HttpClientUtil.getHttpClient(CTX_KEY, ctx);

    httpRequest.setHeader("user-agent", formatUserAgentString(ctx, repository));
    httpRequest.setHeader("accept", "*/*");
    httpRequest.setHeader("accept-language", "en-us");
    httpRequest.setHeader("accept-encoding", "gzip,deflate,identity");
    httpRequest.setHeader("cache-control", "no-cache");

    // HTTP keep alive should not be used, except when NTLM is used
    final Boolean isNtlmUsed = HttpClientUtil.isNTLMAuthenticationUsed(CTX_KEY, ctx);
    if (isNtlmUsed == null || !isNtlmUsed) {
      httpRequest.setHeader("Connection", "close");
      httpRequest.setHeader("Proxy-Connection", "close");
    }

    HttpResponse httpResponse = null;
    try {
      httpResponse = httpClient.execute(httpRequest);
      final int statusCode = httpResponse.getStatusLine().getStatusCode();

      final Header httpServerHeader = httpResponse.getFirstHeader("server");
      checkForRemotePeerAmazonS3Storage(
          repository, httpServerHeader == null ? null : httpServerHeader.getValue());

      Header proxyReturnedErrorHeader = httpResponse.getFirstHeader(NEXUS_MISSING_ARTIFACT_HEADER);
      boolean proxyReturnedError =
          proxyReturnedErrorHeader != null && Boolean.valueOf(proxyReturnedErrorHeader.getValue());

      if (statusCode == HttpStatus.SC_FORBIDDEN) {
        throw new RemoteAccessDeniedException(
            repository, methodUri.toASCIIString(), httpResponse.getStatusLine().getReasonPhrase());
      } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
        throw new RemoteAuthenticationNeededException(
            repository, httpResponse.getStatusLine().getReasonPhrase());
      } else if (statusCode == HttpStatus.SC_OK && proxyReturnedError) {
        throw new RemoteStorageException(
            "Invalid artifact found, most likely a proxy redirected to an HTML error page.");
      }

      return httpResponse;
    } catch (RemoteStorageException ex) {
      release(httpResponse);
      throw ex;
    } catch (ClientProtocolException ex) {
      release(httpResponse);
      throw new RemoteStorageException(
          "Protocol error while executing "
              + httpRequest.getMethod()
              + " method. [repositoryId=\""
              + repository.getId()
              + "\", requestPath=\""
              + request.getRequestPath()
              + "\", remoteUrl=\""
              + methodUri.toASCIIString()
              + "\"]",
          ex);
    } catch (IOException ex) {
      release(httpResponse);
      throw new RemoteStorageException(
          "Transport error while executing "
              + httpRequest.getMethod()
              + " method [repositoryId=\""
              + repository.getId()
              + "\", requestPath=\""
              + request.getRequestPath()
              + "\", remoteUrl=\""
              + methodUri.toASCIIString()
              + "\"]",
          ex);
    }
  }
예제 #16
0
 @Override
 protected String getS3FlagKey() {
   return HttpClientUtil.getS3FlagKey(CTX_KEY);
 }
예제 #17
0
 public void testHttpGet() {
   String url = "http://www.runoob.com/java/java-regular-expressions.html";
   System.out.println(HttpClientUtil.httpGetRequest(url));
 }
예제 #18
0
 /**
  * HttpConnectionParams.setConnectionTimeout
  *
  * @param timeout Timeout in milliseconds
  */
 public void setConnectionTimeout(int timeout) {
   HttpClientUtil.setConnectionTimeout(httpClient, timeout);
 }
예제 #19
0
 /**
  * Configure whether the client should follow redirects or not.
  *
  * <p>This defaults to false under the assumption that if you are following a redirect to get to a
  * Solr installation, something is misconfigured somewhere.
  */
 public void setFollowRedirects(boolean followRedirects) {
   this.followRedirects = true;
   HttpClientUtil.setFollowRedirects(httpClient, followRedirects);
 }
예제 #20
0
 /**
  * Set SoTimeout (read timeout). This is desirable for queries, but probably not for indexing.
  *
  * @param timeout Timeout in milliseconds
  */
 public void setSoTimeout(int timeout) {
   HttpClientUtil.setSoTimeout(httpClient, timeout);
 }