Java Code Examples for org.apache.http.protocol.HttpContext#setAttribute()

The following examples show how to use org.apache.http.protocol.HttpContext#setAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: 103335311.java    From docs-apim with Apache License 2.0 6 votes vote down vote up
@Override
public boolean deleteFromStore(APIIdentifier apiId, APIStore store) throws APIManagementException {
	boolean deleted = false;
    if (store.getEndpoint() == null || store.getUsername() == null || store.getPassword() == null) {
        String msg = "External APIStore endpoint URL or credentials are not defined.Cannot proceed with publishing API to the APIStore - " + store.getDisplayName();
        throw new APIManagementException(msg);

    } else {
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        boolean authenticated = authenticateAPIM(store,httpContext);
        if (authenticated) {
        	deleted = deleteWSO2Store(apiId, store.getUsername(), store.getEndpoint(), httpContext,store.getDisplayName());
        	logoutFromExternalStore(store, httpContext);
        }
        return deleted;
    }
}
 
Example 2
Source File: SofaTracerAsyncHttpInterceptor.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException,
                                                                     IOException {
    //lazy init
    RequestLine requestLine = httpRequest.getRequestLine();
    String methodName = requestLine.getMethod();
    //span generated
    SofaTracerSpan httpClientSpan = httpClientTracer.clientSend(methodName);
    super.appendHttpClientRequestSpanTags(httpRequest, httpClientSpan);
    //async handle
    httpContext.setAttribute(CURRENT_ASYNC_HTTP_SPAN_KEY, httpClientSpan);
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    //client span
    if (httpClientSpan.getParentSofaTracerSpan() != null) {
        //restore parent
        sofaTraceContext.push(httpClientSpan.getParentSofaTracerSpan());
    } else {
        //pop async span
        sofaTraceContext.pop();
    }
}
 
Example 3
Source File: HttpClientAdapter.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Take a request as {@link HttpMethod} and execute it.
 *
 * @param request the {@link HttpMethod} to be executed
 * @param ignoreNoProxy set true to ignore the "no_proxy" setting
 * @return the return code of the request
 * @throws IOException in case of errors
 */
public HttpResponse executeRequest(HttpRequestBase request, boolean ignoreNoProxy)
        throws IOException {
    if (log.isDebugEnabled()) {
        log.debug(request.getMethod() + " " + request.getURI());
    }
    // Decide if a proxy should be used for this request

    HttpContext httpContxt = new BasicHttpContext();
    httpContxt.setAttribute(IGNORE_NO_PROXY, ignoreNoProxy);

    httpContxt.setAttribute(REQUEST_URI, request.getURI());

    // Execute the request
    request.setConfig(requestConfig);
    HttpResponse httpResponse = httpClient.execute(request, httpContxt);

    if (log.isDebugEnabled()) {
        log.debug("Response code: " + httpResponse.getStatusLine().getStatusCode());
    }
    return httpResponse;
}
 
Example 4
Source File: 103335311.java    From docs-apim with Apache License 2.0 6 votes vote down vote up
public boolean isAPIAvailable(API api, APIStore store) throws APIManagementException {

            boolean available = false;
            if (store.getEndpoint() == null || store.getUsername() == null || store.getPassword() == null) {
            String msg = "External APIStore endpoint URL or credentials are not defined. Cannot proceed with checking API availabiltiy from the APIStore - "
                		+ store.getDisplayName();
            throw new APIManagementException(msg);

            } else {
            CookieStore cookieStore = new BasicCookieStore();
            HttpContext httpContext = new BasicHttpContext();
            httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    		boolean authenticated = authenticateAPIM(store, httpContext);
    		if (authenticated) {
    		available = isAPIAvailableInWSO2Store(api, store.getUsername(), store.getEndpoint(), httpContext);
    		logoutFromExternalStore(store, httpContext);
    		}
    		return available;
    	}
    }
 
Example 5
Source File: 103335311.java    From docs-apim with Apache License 2.0 6 votes vote down vote up
public boolean updateToStore(API api, APIStore store) throws APIManagementException {
	boolean updated = false;
    if (store.getEndpoint() == null || store.getUsername() == null || store.getPassword() == null) {
        String msg = "External APIStore endpoint URL or credentials are not defined.Cannot proceed with publishing API to the APIStore - " + store.getDisplayName();
        throw new APIManagementException(msg);

    }
    else{
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    boolean authenticated = authenticateAPIM(store, httpContext);
    if (authenticated) {
    	updated = updateWSO2Store(api, store.getUsername(), store.getEndpoint(), httpContext,store.getDisplayName());
    	logoutFromExternalStore(store, httpContext);
    }
    return updated;
    }
}
 
Example 6
Source File: 103335311.java    From docs-apim with Apache License 2.0 6 votes vote down vote up
/**
 * The method to publish API to external WSO2 Store
 * @param api      API
 * @param store    Store
 * @return   published/not
 */

public boolean publishToStore(API api,APIStore store) throws APIManagementException {
    boolean published = false;

    if (store.getEndpoint() == null || store.getUsername() == null || store.getPassword() == null) {
        String msg = "External APIStore endpoint URL or credentials are not defined.Cannot proceed with publishing API to the APIStore - "+store.getDisplayName();
        throw new APIManagementException(msg);
    }
    else{
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    boolean authenticated = authenticateAPIM(store,httpContext);
    if(authenticated){  //First try to login to store
        boolean added = addAPIToStore(api,store.getEndpoint(), store.getUsername(), httpContext,store.getDisplayName());
        if (added) {   //If API creation success,then try publishing the API
            published = publishAPIToStore(api.getId(), store.getEndpoint(), store.getUsername(), httpContext,store.getDisplayName());
        }
        logoutFromExternalStore(store, httpContext);
    }
    }
    return published;
}
 
Example 7
Source File: HttpComponentsAsyncClientHttpRequestFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
	HttpAsyncClient client = startAsyncClient();

	HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
	postProcessHttpRequest(httpRequest);
	HttpContext context = createHttpContext(httpMethod, uri);
	if (context == null) {
		context = HttpClientContext.create();
	}

	// Request configuration not set in the context
	if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
		// Use request configuration given by the user, when available
		RequestConfig config = null;
		if (httpRequest instanceof Configurable) {
			config = ((Configurable) httpRequest).getConfig();
		}
		if (config == null) {
			config = createRequestConfig(client);
		}
		if (config != null) {
			context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
		}
	}

	return new HttpComponentsAsyncClientHttpRequest(client, httpRequest, context);
}
 
Example 8
Source File: HttpRestConfig.java    From jeeves with MIT License 5 votes vote down vote up
@Bean
public RestTemplate restTemplate() {
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    httpContext.setAttribute(HttpClientContext.REQUEST_CONFIG, RequestConfig.custom().setRedirectsEnabled(false).build());
    return new StatefullRestTemplate(httpContext);
}
 
Example 9
Source File: VSCrawlerRoutePlanner.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpHost determineProxy(HttpHost host, HttpRequest request, HttpContext context) throws HttpException {
    HttpClientContext httpClientContext = HttpClientContext.adapt(context);
    Proxy proxy = proxyPlanner.determineProxy(host, request, context, ipPool, crawlerSession);

    if (proxy == null) {
        return null;
    }
    if (log.isDebugEnabled()) {
        log.debug("{} 当前使用IP为:{}:{}", host.getHostName(), proxy.getIp(), proxy.getPort());
    }
    context.setAttribute(VSCRAWLER_AVPROXY_KEY, proxy);
    crawlerSession.setExtInfo(VSCRAWLER_AVPROXY_KEY, proxy);

    if (proxy.getAuthenticationHeaders() != null) {
        for (Header header : proxy.getAuthenticationHeaders()) {
            request.addHeader(header);
        }
    }

    if (StringUtils.isNotEmpty(proxy.getUsername()) && StringUtils.isNotEmpty(proxy.getPassword())) {
        BasicCredentialsProvider credsProvider1 = new BasicCredentialsProvider();
        httpClientContext.setCredentialsProvider(credsProvider1);
        credsProvider1.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
    }
    return new HttpHost(proxy.getIp(), proxy.getPort());
}
 
Example 10
Source File: HttpComponentsClientHttpRequestFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
	HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
	postProcessHttpRequest(httpRequest);
	HttpContext context = createHttpContext(httpMethod, uri);
	if (context == null) {
		context = HttpClientContext.create();
	}

	// Request configuration not set in the context
	if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
		// Use request configuration given by the user, when available
		RequestConfig config = null;
		if (httpRequest instanceof Configurable) {
			config = ((Configurable) httpRequest).getConfig();
		}
		if (config == null) {
			config = createRequestConfig(getHttpClient());
		}
		if (config != null) {
			context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
		}
	}

	if (this.bufferRequestBody) {
		return new HttpComponentsClientHttpRequest(getHttpClient(), httpRequest, context);
	}
	else {
		return new HttpComponentsStreamingClientHttpRequest(getHttpClient(), httpRequest, context);
	}
}
 
Example 11
Source File: HttpComponentsAsyncClientHttpRequestFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
	startAsyncClient();

	HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
	postProcessHttpRequest(httpRequest);
       HttpContext context = createHttpContext(httpMethod, uri);
       if (context == null) {
           context = HttpClientContext.create();
       }

	// Request configuration not set in the context
	if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
		// Use request configuration given by the user, when available
		RequestConfig config = null;
		if (httpRequest instanceof Configurable) {
			config = ((Configurable) httpRequest).getConfig();
		}
		if (config == null) {
			config = createRequestConfig(getAsyncClient());
		}
		if (config != null) {
			context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
		}
	}

	return new HttpComponentsAsyncClientHttpRequest(getAsyncClient(), httpRequest, context);
}
 
Example 12
Source File: TSDBHttpAsyncCallbackExecutor.java    From aliyun-tsdb-java-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void requestReady(NHttpClientConnection conn) throws IOException, HttpException {
	try {
		super.requestReady(conn);
	} catch (Exception ex) {
		LOGGER.error("", ex);
	}

	// 需要自动关闭连接
	if (this.liveTime > 0) {
		HttpRequest httpRequest = conn.getHttpRequest();
		if (httpRequest == null) {
			return;
		}

		HttpContext context = conn.getContext();

		long currentTimeMillis = System.currentTimeMillis();
		Object oldTimeMillisObj = context.getAttribute("t");
		if (oldTimeMillisObj == null) {
			context.setAttribute("t", currentTimeMillis);
		} else {
			long oldTimeMillis = (Long) oldTimeMillisObj;
			long dt = currentTimeMillis - oldTimeMillis;
			if (dt > 1000 * liveTime) { // 超时,重连
				tryCloseConnection(httpRequest);
				context.setAttribute("t", currentTimeMillis);
			}
		}
	}
}
 
Example 13
Source File: ServiceNowExecution.java    From service-now-plugin with MIT License 5 votes vote down vote up
private CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpRequestBase requestBase, HttpContext httpContext) {
    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(
            new AuthScope(requestBase.getURI().getHost(), requestBase.getURI().getPort()),
            CredentialsUtil.readCredentials(credentials, vaultConfiguration));
    clientBuilder.setDefaultCredentialsProvider(provider);

    AuthCache authCache = new BasicAuthCache();
    authCache.put(URIUtils.extractHost(requestBase.getURI()), new BasicScheme());
    httpContext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);

    return clientBuilder.build();
}
 
Example 14
Source File: HttpComponentsClientHttpRequestFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
	HttpClient client = getHttpClient();

	HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
	postProcessHttpRequest(httpRequest);
	HttpContext context = createHttpContext(httpMethod, uri);
	if (context == null) {
		context = HttpClientContext.create();
	}

	// Request configuration not set in the context
	if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
		// Use request configuration given by the user, when available
		RequestConfig config = null;
		if (httpRequest instanceof Configurable) {
			config = ((Configurable) httpRequest).getConfig();
		}
		if (config == null) {
			config = createRequestConfig(client);
		}
		if (config != null) {
			context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
		}
	}

	if (this.bufferRequestBody) {
		return new HttpComponentsClientHttpRequest(client, httpRequest, context);
	}
	else {
		return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context);
	}
}
 
Example 15
Source File: HttpComponentsAsyncClientHttpRequestFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
	HttpAsyncClient client = startAsyncClient();

	HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
	postProcessHttpRequest(httpRequest);
	HttpContext context = createHttpContext(httpMethod, uri);
	if (context == null) {
		context = HttpClientContext.create();
	}

	// Request configuration not set in the context
	if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
		// Use request configuration given by the user, when available
		RequestConfig config = null;
		if (httpRequest instanceof Configurable) {
			config = ((Configurable) httpRequest).getConfig();
		}
		if (config == null) {
			config = createRequestConfig(client);
		}
		if (config != null) {
			context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
		}
	}

	return new HttpComponentsAsyncClientHttpRequest(client, httpRequest, context);
}
 
Example 16
Source File: HttpComponentsClientHttpRequestFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
	HttpClient client = getHttpClient();

	HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
	postProcessHttpRequest(httpRequest);
	HttpContext context = createHttpContext(httpMethod, uri);
	if (context == null) {
		context = HttpClientContext.create();
	}

	// Request configuration not set in the context
	if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
		// Use request configuration given by the user, when available
		RequestConfig config = null;
		if (httpRequest instanceof Configurable) {
			config = ((Configurable) httpRequest).getConfig();
		}
		if (config == null) {
			config = createRequestConfig(client);
		}
		if (config != null) {
			context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
		}
	}

	if (this.bufferRequestBody) {
		return new HttpComponentsClientHttpRequest(client, httpRequest, context);
	}
	else {
		return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context);
	}
}
 
Example 17
Source File: TestTwitterSocket.java    From albert with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	  OAuthConsumer consumer = new CommonsHttpOAuthConsumer(
				Constants.ConsumerKey,
				Constants.ConsumerSecret);
	  consumer.setTokenWithSecret(Constants.AccessToken, Constants.AccessSecret);
	  
	  
    HttpParams params = new BasicHttpParams();
    // HTTP 协议的版本,1.1/1.0/0.9
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    // 字符集
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    // 伪装的浏览器类型
    // IE7 是 
    // Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)
    //
    // Firefox3.03
    // Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
    //
    HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
    HttpProtocolParams.setUseExpectContinue(params, true);
    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());
    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
    HttpContext context = new BasicHttpContext(null);
    HttpHost host = new HttpHost("127.0.0.1", 1080);
    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
    try {
      String[] targets = { "https://www.twitter.com"};
      for (int i = 0; i < targets.length; i++) {
        if (!conn.isOpen()) {
          Socket socket = new Socket(host.getHostName(), host.getPort());
          conn.bind(socket, params);
        }
        
	  
        BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
//        consumer.sign(request);
        
        System.out.println(">> Request URI: " + request.getRequestLine().getUri());
        context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
        request.setParams(params);
        httpexecutor.preProcess(request, httpproc, context);
        HttpResponse response = httpexecutor.execute(request, conn, context);
        response.setParams(params);
        httpexecutor.postProcess(response, httpproc, context);
        // 返回码
        System.out.println("<< Response: " + response.getStatusLine());
        // 返回的文件头信息
//        Header[] hs = response.getAllHeaders();
//        for (Header h : hs) {
//          System.out.println(h.getName() + ":" + h.getValue());
//        }
        // 输出主体信息
//        System.out.println(EntityUtils.toString(response.getEntity()));
        
        HttpEntity entry = response.getEntity();
        StringBuffer sb = new StringBuffer();
  	  if(entry != null)
  	  {
  	    InputStreamReader is = new InputStreamReader(entry.getContent());
  	    BufferedReader br = new BufferedReader(is);
  	    String str = null;
  	    while((str = br.readLine()) != null)
  	    {
  	     sb.append(str.trim());
  	    }
  	    br.close();
  	  }
  	  System.out.println(sb.toString());
  	  
        System.out.println("==============");
        if (!connStrategy.keepAlive(response, context)) {
          conn.close();
        } else {
          System.out.println("Connection kept alive...");
        }
      }
    } finally {
      conn.close();
    }
  }
 
Example 18
Source File: CRC32ChecksumResponseInterceptor.java    From ibm-cos-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public void process(HttpResponse response, final HttpContext context)
        throws HttpException, IOException {
    final HttpEntity entity = response.getEntity();

    // Only Json protocol has this header, we only wrap CRC32ChecksumCalculatingInputStream in json protocol clients.
    Header[] headers = response.getHeaders("x-amz-crc32");
    if (entity == null || headers == null || headers.length == 0) {
        return;
    }
    HttpEntity crc32ResponseEntity = new HttpEntityWrapper(entity) {

        private final InputStream content = new CRC32ChecksumCalculatingInputStream(
                wrappedEntity.getContent());

        @Override
        public InputStream getContent() throws IOException {
            return content;
        }

        /**
         * It's important to override writeTo. Some versions of Apache HTTP
         * client use writeTo for {@link org.apache.http.entity.BufferedHttpEntity}
         * and the default implementation just delegates to the wrapped entity
         * which completely bypasses our CRC32 calculating input stream. The
         * {@link org.apache.http.entity.BufferedHttpEntity} is used for the
         * request timeout and client execution timeout features.
         *
         * @see <a href="https://github.com/aws/aws-sdk-java/issues/526">Issue #526</a>
         *
         * @param outstream OutputStream to write contents to
         */
        @Override
        public void writeTo(OutputStream outstream) throws IOException {
            try {
                IOUtils.copy(this.getContent(), outstream);
            } finally {
                this.getContent().close();
            }
        }
    };

    response.setEntity(crc32ResponseEntity);
    context.setAttribute(CRC32ChecksumCalculatingInputStream.class.getName(),
                         crc32ResponseEntity.getContent());
}
 
Example 19
Source File: HtmlUnitSSLConnectionSocketFactory.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Enables/Disables the exclusive usage of SSL3.
 * @param httpContext the http context
 * @param ssl3Only true or false
 */
public static void setUseSSL3Only(final HttpContext httpContext, final boolean ssl3Only) {
    httpContext.setAttribute(SSL3ONLY, ssl3Only);
}
 
Example 20
Source File: SocksConnectionSocketFactory.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Enables the socks proxy.
 * @param context the HttpContext
 * @param socksProxy the HttpHost
 */
public static void setSocksProxy(final HttpContext context, final HttpHost socksProxy) {
    context.setAttribute(SOCKS_PROXY, socksProxy);
}