Java Code Examples for org.apache.http.HttpRequest

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project httpClient, under directory /httpclient/src/main/java/org/apache/http/impl/client/.

Source file: DefaultHttpRequestRetryHandler.java

  22 
vote

/** 
 * @since 4.2
 * @deprecated (4.3)
 */
protected boolean requestIsAborted(final HttpRequest request){
  HttpRequest req=request;
  if (request instanceof RequestWrapper) {
    req=((RequestWrapper)request).getOriginal();
  }
  return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted());
}
 

Example 2

From project EasySOA, under directory /easysoa-proxy/easysoa-proxy-core/easysoa-proxy-core-httpdiscoveryproxy/src/main/java/org/easysoa/proxy/.

Source file: HttpRetryHandler.java

  21 
vote

public boolean retryRequest(IOException exception,int executionCount,HttpContext context){
  if (executionCount >= maxRetryNumber) {
    return false;
  }
  if (exception instanceof NoHttpResponseException) {
    return true;
  }
  if (exception instanceof SSLHandshakeException) {
    return false;
  }
  HttpRequest request=(HttpRequest)context.getAttribute(ExecutionContext.HTTP_REQUEST);
  boolean idempotent=!(request instanceof HttpEntityEnclosingRequest);
  if (idempotent) {
    return true;
  }
  return false;
}
 

Example 3

From project fanfoudroid, under directory /src/com/ch_linghu/fanfoudroid/http/.

Source file: HttpClient.java

  21 
vote

public boolean retryRequest(IOException exception,int executionCount,HttpContext context){
  if (executionCount >= RETRIED_TIME) {
    return false;
  }
  if (exception instanceof NoHttpResponseException) {
    return true;
  }
  if (exception instanceof SSLHandshakeException) {
    return false;
  }
  HttpRequest request=(HttpRequest)context.getAttribute(ExecutionContext.HTTP_REQUEST);
  boolean idempotent=(request instanceof HttpEntityEnclosingRequest);
  if (!idempotent) {
    return true;
  }
  return false;
}
 

Example 4

From project Fotolia-API, under directory /java/libs/httpcomponents-client-4.1.2/examples/org/apache/http/examples/conn/.

Source file: OperatorConnectDirect.java

  21 
vote

public static void main(String[] args) throws Exception {
  HttpHost target=new HttpHost("jakarta.apache.org",80,"http");
  SchemeRegistry supportedSchemes=new SchemeRegistry();
  supportedSchemes.register(new Scheme("http",80,PlainSocketFactory.getSocketFactory()));
  HttpParams params=new SyncBasicHttpParams();
  HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
  HttpProtocolParams.setUseExpectContinue(params,false);
  ClientConnectionOperator scop=new DefaultClientConnectionOperator(supportedSchemes);
  HttpRequest req=new BasicHttpRequest("OPTIONS","*",HttpVersion.HTTP_1_1);
  req.addHeader("Host",target.getHostName());
  HttpContext ctx=new BasicHttpContext();
  OperatedClientConnection conn=scop.createConnection();
  try {
    System.out.println("opening connection to " + target);
    scop.openConnection(conn,target,null,ctx,params);
    System.out.println("sending request");
    conn.sendRequestHeader(req);
    conn.flush();
    System.out.println("receiving response header");
    HttpResponse rsp=conn.receiveResponseHeader();
    System.out.println("----------------------------------------");
    System.out.println(rsp.getStatusLine());
    Header[] headers=rsp.getAllHeaders();
    for (int i=0; i < headers.length; i++) {
      System.out.println(headers[i]);
    }
    System.out.println("----------------------------------------");
  }
  finally {
    System.out.println("closing connection");
    conn.close();
  }
}
 

Example 5

From project AirCastingAndroidClient, under directory /src/main/java/pl/llp/aircasting/util/http/.

Source file: HttpBuilder.java

  19 
vote

private HttpRequestInterceptor preemptiveAuth(){
  return new HttpRequestInterceptor(){
    @Override public void process(    HttpRequest httpRequest,    HttpContext httpContext) throws HttpException, IOException {
      AuthState authState=(AuthState)httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
      Credentials credentials;
      if (useLogin) {
        credentials=new UsernamePasswordCredentials(login,password);
      }
 else {
        credentials=new UsernamePasswordCredentials(settingsHelper.getAuthToken(),"X");
      }
      authState.setAuthScope(AuthScope.ANY);
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(credentials);
    }
  }
;
}
 

Example 6

From project android-client, under directory /xwiki-android-rest/src/org/xwiki/android/rest/.

Source file: HttpConnector.java

  19 
vote

/** 
 * set user credentials with manually developed preemtive Auth
 */
private void setCredentials(){
  if (isSecured) {
    preemptiveAuth=new HttpRequestInterceptor(){
      public void process(      HttpRequest request,      HttpContext context) throws HttpException, IOException {
        AuthState authState=(AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
        CredentialsProvider credsProvider=(CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost=(HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authState.getAuthScheme() == null) {
          AuthScope authScope=new AuthScope(targetHost.getHostName(),targetHost.getPort());
          Credentials creds=credsProvider.getCredentials(authScope);
          if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
          }
        }
      }
    }
;
    client.addRequestInterceptor(preemptiveAuth,0);
    Credentials defaultcreds=new UsernamePasswordCredentials(username,password);
    client.getCredentialsProvider().setCredentials(new AuthScope(null,-1,AuthScope.ANY_REALM),defaultcreds);
  }
}
 

Example 7

From project androidquery, under directory /auth/com/androidquery/auth/.

Source file: BasicHandle.java

  19 
vote

@Override public void applyToken(AbstractAjaxCallback<?,?> cb,HttpRequest request){
  String cred=username + ":" + password;
  byte[] data=cred.getBytes();
  String auth="Basic " + new String(encode(data,0,data.length));
  Uri uri=Uri.parse(cb.getUrl());
  String host=uri.getHost();
  request.addHeader("Host",host);
  request.addHeader("Authorization",auth);
}
 

Example 8

From project AquaNotesTest, under directory /src/com/google/android/apps/iosched/service/.

Source file: SyncService.java

  19 
vote

/** 
 * Generate and return a  {@link HttpClient} configured for general use,including setting an application-specific user-agent string.
 */
public static HttpClient getHttpClient(Context context){
  final HttpParams params=new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSoTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSocketBufferSize(params,8192);
  HttpProtocolParams.setUserAgent(params,buildUserAgent(context));
  final DefaultHttpClient client=new DefaultHttpClient(params);
  client.addRequestInterceptor(new HttpRequestInterceptor(){
    public void process(    HttpRequest request,    HttpContext context){
      if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
        request.addHeader(HEADER_ACCEPT_ENCODING,ENCODING_GZIP);
      }
    }
  }
);
  client.addResponseInterceptor(new HttpResponseInterceptor(){
    public void process(    HttpResponse response,    HttpContext context){
      final HttpEntity entity=response.getEntity();
      final Header encoding=entity.getContentEncoding();
      if (encoding != null) {
        for (        HeaderElement element : encoding.getElements()) {
          if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
            response.setEntity(new InflatingEntity(response.getEntity()));
            break;
          }
        }
      }
    }
  }
);
  return client;
}
 

Example 9

From project be.norio.twunch.android, under directory /src/be/norio/twunch/android/service/.

Source file: SyncService.java

  19 
vote

/** 
 * Generate and return a  {@link HttpClient} configured for general use,including setting an application-specific user-agent string.
 */
public static HttpClient getHttpClient(Context context){
  final HttpParams params=new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSoTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSocketBufferSize(params,8192);
  HttpProtocolParams.setUserAgent(params,buildUserAgent(context));
  final DefaultHttpClient client=new DefaultHttpClient(params);
  client.addRequestInterceptor(new HttpRequestInterceptor(){
    public void process(    HttpRequest request,    HttpContext context){
      if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
        request.addHeader(HEADER_ACCEPT_ENCODING,ENCODING_GZIP);
      }
    }
  }
);
  client.addResponseInterceptor(new HttpResponseInterceptor(){
    public void process(    HttpResponse response,    HttpContext context){
      final HttpEntity entity=response.getEntity();
      final Header encoding=entity.getContentEncoding();
      if (encoding != null) {
        for (        HeaderElement element : encoding.getElements()) {
          if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
            response.setEntity(new InflatingEntity(response.getEntity()));
            break;
          }
        }
      }
    }
  }
);
  return client;
}
 

Example 10

From project build-info, under directory /build-info-client/src/main/java/org/jfrog/build/client/.

Source file: PreemptiveHttpClient.java

  19 
vote

public void process(final HttpRequest request,final HttpContext context) throws HttpException, IOException {
  AuthState authState=(AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
  if (authState.getAuthScheme() == null) {
    AuthScheme authScheme=(AuthScheme)context.getAttribute("preemptive-auth");
    CredentialsProvider credsProvider=(CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost=(HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    if (authScheme != null) {
      Credentials creds=credsProvider.getCredentials(new AuthScope(targetHost.getHostName(),targetHost.getPort()));
      if (creds == null) {
        throw new HttpException("No credentials for preemptive authentication");
      }
      authState.setAuthScheme(authScheme);
      authState.setCredentials(creds);
    }
  }
}
 

Example 11

From project CalendarPortlet, under directory /src/main/java/org/jasig/portlet/calendar/adapter/exchange/.

Source file: NtlmAuthHttpClient.java

  19 
vote

@Override public void process(HttpRequest request,HttpContext context) throws HttpException, IOException {
  if (request instanceof HttpEntityEnclosingRequest) {
    if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
      request.removeHeaders(HTTP.TRANSFER_ENCODING);
    }
    if (request.containsHeader(HTTP.CONTENT_LEN)) {
      request.removeHeaders(HTTP.CONTENT_LEN);
    }
  }
}
 

Example 12

From project CityBikes, under directory /src/net/homelinux/penecoptero/android/citybikes/app/.

Source file: RESTHelper.java

  19 
vote

private static DefaultHttpClient setCredentials(DefaultHttpClient httpclient,String url,String username,String password) throws HttpException, IOException {
  HttpHost targetHost=new HttpHost(url);
  final UsernamePasswordCredentials access=new UsernamePasswordCredentials(username,password);
  httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(),targetHost.getPort()),access);
  httpclient.addRequestInterceptor(new HttpRequestInterceptor(){
    @Override public void process(    HttpRequest request,    HttpContext context) throws HttpException, IOException {
      AuthState authState=(AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
      if (authState.getAuthScheme() == null) {
        authState.setAuthScheme(new BasicScheme());
        authState.setCredentials(access);
      }
    }
  }
,0);
  return httpclient;
}
 

Example 13

From project cloudify, under directory /rest-client/src/main/java/org/cloudifysource/restclient/.

Source file: GSRestClient.java

  19 
vote

/** 
 * Ctor.
 * @param username Username for the HTTP client, optional.
 * @param password Password for the HTTP client, optional.
 * @param url      URL to the rest service.
 * @param version  cloudify api version of the client 
 * @throws RestException Reporting failure to create a SSL HTTP client.
 */
public GSRestClient(final String username,final String password,final URL url,final String version) throws RestException {
  this.url=url;
  this.urlStr=createUrlStr();
  if (isSSL()) {
    httpClient=getSSLHttpClient();
  }
 else {
    httpClient=new DefaultHttpClient();
  }
  httpClient.addRequestInterceptor(new HttpRequestInterceptor(){
    @Override public void process(    HttpRequest request,    HttpContext context) throws HttpException, IOException {
      request.addHeader(CloudifyConstants.REST_API_VERSION_HEADER,version);
    }
  }
);
  if (StringUtils.notEmpty(username) && StringUtils.notEmpty(password)) {
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY),new UsernamePasswordCredentials(username,password));
  }
}
 

Example 14

From project components-ness-config, under directory /src/main/java/com/nesscomputing/config/util/.

Source file: HttpConfigStrategy.java

  19 
vote

@Override public AbstractConfiguration convert(final HttpRequest request,final HttpResponse response,final InputStream inputStream) throws IOException {
switch (response.getStatusLine().getStatusCode()) {
case HttpServletResponse.SC_NOT_FOUND:
    return null;
case HttpServletResponse.SC_UNAUTHORIZED:
  throw new IllegalStateException(String.format("Could not load configuration from '%s', not authorized!",request.getRequestLine().getUri()));
case HttpServletResponse.SC_OK:
try {
  final PropertiesConfiguration config=new PropertiesConfiguration();
  config.load(inputStream);
  return config;
}
 catch (ConfigurationException ce) {
  throw new IOException("Could not load configuration",ce);
}
default :
throw new IOException("Could not load configuration from " + request.getRequestLine().getUri() + " ("+ response.getStatusLine().getStatusCode()+ ")");
}
}
 

Example 15

From project conference-mobile-app, under directory /android-app/src/com/google/android/apps/iosched/service/.

Source file: SyncService.java

  19 
vote

/** 
 * Generate and return a  {@link HttpClient} configured for general use,including setting an application-specific user-agent string.
 */
public static HttpClient getHttpClient(Context context){
  final HttpParams params=new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSoTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSocketBufferSize(params,8192);
  HttpProtocolParams.setUserAgent(params,buildUserAgent(context));
  final DefaultHttpClient client=new DefaultHttpClient(params);
  client.addRequestInterceptor(new HttpRequestInterceptor(){
    public void process(    HttpRequest request,    HttpContext context){
      if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
        request.addHeader(HEADER_ACCEPT_ENCODING,ENCODING_GZIP);
      }
    }
  }
);
  client.addResponseInterceptor(new HttpResponseInterceptor(){
    public void process(    HttpResponse response,    HttpContext context){
      final HttpEntity entity=response.getEntity();
      final Header encoding=entity.getContentEncoding();
      if (encoding != null) {
        for (        HeaderElement element : encoding.getElements()) {
          if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
            response.setEntity(new InflatingEntity(response.getEntity()));
            break;
          }
        }
      }
    }
  }
);
  return client;
}
 

Example 16

From project couchdb-lucene, under directory /src/main/java/com/github/rnewson/couchdb/lucene/.

Source file: HttpClientFactory.java

  19 
vote

public void process(final HttpRequest request,final HttpContext context) throws HttpException, IOException {
  final AuthState authState=(AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
  final CredentialsProvider credsProvider=(CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER);
  final HttpHost targetHost=(HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
  if (authState.getAuthScheme() == null) {
    AuthScope authScope=new AuthScope(targetHost.getHostName(),targetHost.getPort());
    Credentials creds=credsProvider.getCredentials(authScope);
    if (creds != null) {
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(creds);
    }
  }
}
 

Example 17

From project dccsched, under directory /src/com/underhilllabs/dccsched/service/.

Source file: SyncService.java

  19 
vote

/** 
 * Generate and return a  {@link HttpClient} configured for general use,including setting an application-specific user-agent string.
 */
public static HttpClient getHttpClient(Context context){
  final HttpParams params=new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSoTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSocketBufferSize(params,8192);
  HttpProtocolParams.setUserAgent(params,buildUserAgent(context));
  final DefaultHttpClient client=new DefaultHttpClient(params);
  client.addRequestInterceptor(new HttpRequestInterceptor(){
    public void process(    HttpRequest request,    HttpContext context){
      if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
        request.addHeader(HEADER_ACCEPT_ENCODING,ENCODING_GZIP);
      }
    }
  }
);
  client.addResponseInterceptor(new HttpResponseInterceptor(){
    public void process(    HttpResponse response,    HttpContext context){
      final HttpEntity entity=response.getEntity();
      final Header encoding=entity.getContentEncoding();
      if (encoding != null) {
        for (        HeaderElement element : encoding.getElements()) {
          if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
            response.setEntity(new InflatingEntity(response.getEntity()));
            break;
          }
        }
      }
    }
  }
);
  return client;
}
 

Example 18

From project dcnyc10-android, under directory /android/src/com/lullabot/android/apps/iosched/service/.

Source file: SyncService.java

  19 
vote

/** 
 * Generate and return a  {@link HttpClient} configured for general use,including setting an application-specific user-agent string.
 */
public static HttpClient getHttpClient(Context context){
  final HttpParams params=new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSoTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSocketBufferSize(params,8192);
  HttpProtocolParams.setUserAgent(params,buildUserAgent(context));
  final DefaultHttpClient client=new DefaultHttpClient(params);
  client.addRequestInterceptor(new HttpRequestInterceptor(){
    public void process(    HttpRequest request,    HttpContext context){
      if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
        request.addHeader(HEADER_ACCEPT_ENCODING,ENCODING_GZIP);
      }
    }
  }
);
  client.addResponseInterceptor(new HttpResponseInterceptor(){
    public void process(    HttpResponse response,    HttpContext context){
      final HttpEntity entity=response.getEntity();
      final Header encoding=entity.getContentEncoding();
      if (encoding != null) {
        for (        HeaderElement element : encoding.getElements()) {
          if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
            response.setEntity(new InflatingEntity(response.getEntity()));
            break;
          }
        }
      }
    }
  }
);
  return client;
}
 

Example 19

From project DeliciousDroid, under directory /src/com/deliciousdroid/client/.

Source file: PreemptiveAuthInterceptor.java

  19 
vote

public void process(final HttpRequest request,final HttpContext context) throws HttpException, IOException {
  AuthState authState=(AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
  CredentialsProvider credsProvider=(CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER);
  HttpHost targetHost=(HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
  if (authState.getAuthScheme() == null) {
    AuthScope authScope=new AuthScope(targetHost.getHostName(),targetHost.getPort());
    Credentials creds=credsProvider.getCredentials(authScope);
    if (creds != null) {
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(creds);
    }
  }
}
 

Example 20

From project devoxx-france-android-in-fine, under directory /src/com/infine/android/devoxx/service/.

Source file: RestService.java

  19 
vote

/** 
 * Generate and return a  {@link HttpClient} configured for general use,including setting an application-specific user-agent string.
 */
public static HttpClient getHttpClient(Context context){
  final HttpParams params=new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(params,20 * 1000);
  HttpConnectionParams.setSoTimeout(params,20 * 1000);
  HttpConnectionParams.setSocketBufferSize(params,8192);
  HttpProtocolParams.setUserAgent(params,buildUserAgent(context));
  final DefaultHttpClient client=new DefaultHttpClient(params);
  client.addRequestInterceptor(new HttpRequestInterceptor(){
    public void process(    HttpRequest request,    HttpContext context){
      if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
        request.addHeader(HEADER_ACCEPT_ENCODING,ENCODING_GZIP);
      }
    }
  }
);
  client.addResponseInterceptor(new HttpResponseInterceptor(){
    public void process(    HttpResponse response,    HttpContext context){
      final HttpEntity entity=response.getEntity();
      final Header encoding=entity.getContentEncoding();
      if (encoding != null) {
        for (        HeaderElement element : encoding.getElements()) {
          if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
            response.setEntity(new InflatingEntity(response.getEntity()));
            break;
          }
        }
      }
    }
  }
);
  return client;
}
 

Example 21

From project DiscogsForAndroid, under directory /src/com/discogs/services/.

Source file: NetworkHelper.java

  19 
vote

private void initHTTPClient(){
  SchemeRegistry schemeRegistry=new SchemeRegistry();
  schemeRegistry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(),80));
  schemeRegistry.register(new Scheme("https",SSLSocketFactory.getSocketFactory(),443));
  HttpParams params=new BasicHttpParams();
  HttpProtocolParams.setContentCharset(params,"utf-8");
  params.setBooleanParameter("http.protocol.expect-continue",false);
  HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
  HttpProtocolParams.setContentCharset(params,HTTP.DEFAULT_CONTENT_CHARSET);
  HttpProtocolParams.setUseExpectContinue(params,true);
  ThreadSafeClientConnManager connectionManager=new ThreadSafeClientConnManager(params,schemeRegistry);
  httpClient=new DefaultHttpClient(connectionManager,params);
  httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3,false));
  httpClient.addRequestInterceptor(new HttpRequestInterceptor(){
    public void process(    final HttpRequest request,    final HttpContext context) throws HttpException, IOException {
      if (!request.containsHeader("Accept-Encoding")) {
        request.addHeader("Accept-Encoding","gzip");
      }
    }
  }
);
  httpClient.addResponseInterceptor(new HttpResponseInterceptor(){
    public void process(    final HttpResponse response,    final HttpContext context) throws HttpException, IOException {
      HttpEntity entity=response.getEntity();
      if (entity != null) {
        Header ceheader=entity.getContentEncoding();
        if (ceheader != null) {
          HeaderElement[] codecs=ceheader.getElements();
          for (int i=0; i < codecs.length; i++) {
            if (codecs[i].getName().equalsIgnoreCase("gzip")) {
              response.setEntity(new GzipDecompressingEntity(response.getEntity()));
              return;
            }
          }
        }
      }
    }
  }
);
}
 

Example 22

From project exo.social.client, under directory /src/main/java/org/exoplatform/social/client/core/net/.

Source file: SocialHttpClientImpl.java

  19 
vote

@Override public void process(HttpRequest request,HttpContext context) throws HttpException, IOException {
  AuthState authState=(AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
  CredentialsProvider credsProvider=(CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER);
  HttpHost targetHost=(HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
  if (authState.getAuthScheme() == null) {
    AuthScope authScope=new AuthScope(targetHost.getHostName(),targetHost.getPort());
    Credentials creds=credsProvider.getCredentials(authScope);
    if (creds != null) {
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(creds);
    }
  }
}
 

Example 23

From project gddsched2, under directory /trunk/android/src/com/google/android/apps/iosched2/service/.

Source file: SyncService.java

  19 
vote

/** 
 * Generate and return a  {@link HttpClient} configured for general use,including setting an application-specific user-agent string.
 */
public static HttpClient getHttpClient(Context context){
  final HttpParams params=new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSoTimeout(params,20 * SECOND_IN_MILLIS);
  HttpConnectionParams.setSocketBufferSize(params,8192);
  HttpProtocolParams.setUserAgent(params,buildUserAgent(context));
  final DefaultHttpClient client=new DefaultHttpClient(params);
  client.addRequestInterceptor(new HttpRequestInterceptor(){
    public void process(    HttpRequest request,    HttpContext context){
      if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
        request.addHeader(HEADER_ACCEPT_ENCODING,ENCODING_GZIP);
      }
    }
  }
);
  client.addResponseInterceptor(new HttpResponseInterceptor(){
    public void process(    HttpResponse response,    HttpContext context){
      final HttpEntity entity=response.getEntity();
      final Header encoding=entity.getContentEncoding();
      if (encoding != null) {
        for (        HeaderElement element : encoding.getElements()) {
          if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
            response.setEntity(new InflatingEntity(response.getEntity()));
            break;
          }
        }
      }
    }
  }
);
  return client;
}
 

Example 24

From project httpbuilder, under directory /src/main/java/groovyx/net/http/.

Source file: AuthConfig.java

  19 
vote

public void process(HttpRequest request,HttpContext ctx) throws HttpException, IOException {
  try {
    HttpHost host=(HttpHost)ctx.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    final URI requestURI=new URI(host.toURI()).resolve(request.getRequestLine().getUri());
    oauth.signpost.http.HttpRequest oAuthRequest=new OAuthRequestAdapter(request,requestURI);
    this.oauth.sign(oAuthRequest);
  }
 catch (  URISyntaxException ex) {
    throw new HttpException("Error rebuilding request URI",ex);
  }
catch (  OAuthException e) {
    throw new HttpException("OAuth signing error",e);
  }
}