org.apache.http.protocol.HttpContext#setAttribute ( )源码实例Demo

下面列出了org.apache.http.protocol.HttpContext#setAttribute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: docs-apim   文件: 103335311.java
/**
 * 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;
}
 
源代码2 项目: docs-apim   文件: 103335311.java
@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;
    }
}
 
源代码3 项目: docs-apim   文件: 103335311.java
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;
    }
}
 
源代码4 项目: docs-apim   文件: 103335311.java
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;
    	}
    }
 
源代码5 项目: uyuni   文件: HttpClientAdapter.java
/**
 * 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;
}
 
@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();
    }
}
 
@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);
}
 
@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);
	}
}
 
@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);
}
 
@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);
	}
}
 
源代码11 项目: service-now-plugin   文件: ServiceNowExecution.java
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();
}
 
@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);
			}
		}
	}
}
 
@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);
}
 
@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);
	}
}
 
源代码15 项目: vscrawler   文件: VSCrawlerRoutePlanner.java
@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());
}
 
源代码16 项目: jeeves   文件: HttpRestConfig.java
@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);
}
 
源代码17 项目: albert   文件: TestTwitterSocket.java
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();
    }
  }
 
@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());
}
 
源代码19 项目: htmlunit   文件: SocksConnectionSocketFactory.java
/**
 * 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);
}
 
/**
 * 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);
}