Example usage for twitter4j.auth Authorization getAuthorizationHeader

List of usage examples for twitter4j.auth Authorization getAuthorizationHeader

Introduction

In this page you can find the example usage for twitter4j.auth Authorization getAuthorizationHeader.

Prototype

String getAuthorizationHeader(HttpRequest req);

Source Link

Usage

From source file:de.vanita5.twittnuker.util.net.TwidereHttpClientImpl.java

License:Apache License

@Override
public twitter4j.http.HttpResponse request(final twitter4j.http.HttpRequest req) throws TwitterException {
    final HostAddressResolver resolver = FactoryUtils.getHostAddressResolver(conf);
    final String urlString = req.getURL();
    final URI urlOrig = ParseUtils.parseURI(urlString);
    final String host = urlOrig.getHost(), authority = urlOrig.getAuthority();
    try {/*from  ww  w  .  j  a  va  2  s. c  o m*/
        HttpRequestBaseHC4 commonsRequest;
        final String resolvedHost = resolver != null ? resolver.resolve(host) : null;
        final String resolvedUrl = !isEmpty(resolvedHost)
                ? urlString.replace("://" + host, "://" + resolvedHost)
                : urlString;
        final RequestMethod method = req.getMethod();
        if (method == RequestMethod.GET) {
            commonsRequest = new HttpGetHC4(resolvedUrl);
        } else if (method == RequestMethod.POST) {
            final HttpPostHC4 post = new HttpPostHC4(resolvedUrl);
            post.setEntity(getAsEntity(req.getParameters()));
            post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
            commonsRequest = post;
        } else if (method == RequestMethod.DELETE) {
            commonsRequest = new HttpDeleteHC4(resolvedUrl);
        } else if (method == RequestMethod.HEAD) {
            commonsRequest = new HttpHeadHC4(resolvedUrl);
        } else if (method == RequestMethod.PUT) {
            final HttpPutHC4 put = new HttpPutHC4(resolvedUrl);
            put.setEntity(getAsEntity(req.getParameters()));
            commonsRequest = put;
        } else
            throw new TwitterException("Unsupported request method " + method);
        final HttpParams httpParams = commonsRequest.getParams();
        HttpClientParams.setRedirecting(httpParams, false);
        final Map<String, String> headers = req.getRequestHeaders();
        for (final String headerName : headers.keySet()) {
            commonsRequest.addHeader(headerName, headers.get(headerName));
        }
        final Authorization authorization = req.getAuthorization();
        final String authorizationHeader = authorization != null ? authorization.getAuthorizationHeader(req)
                : null;
        if (authorizationHeader != null) {
            commonsRequest.addHeader(HttpHeaders.AUTHORIZATION, authorizationHeader);
        }
        if (resolvedHost != null && !resolvedHost.isEmpty() && !resolvedHost.equals(host)) {
            commonsRequest.addHeader(HttpHeaders.HOST, authority);
        }

        final ApacheHttpClientHttpResponseImpl res;
        try {
            final HttpContext httpContext = new BasicHttpContextHC4();
            httpContext.setAttribute(HostResolvedSSLConnectionSocketFactory.HTTP_CONTEXT_KEY_ORIGINAL_HOST,
                    host);
            res = new ApacheHttpClientHttpResponseImpl(client.execute(commonsRequest, httpContext), conf);
        } catch (final IllegalStateException e) {
            throw new TwitterException("Please check your API settings.", e);
        } catch (final NullPointerException e) {
            // Bug http://code.google.com/p/android/issues/detail?id=5255
            throw new TwitterException("Please check your APN settings, make sure not to use WAP APNs.", e);
        } catch (final OutOfMemoryError e) {
            // I don't know why OOM thown, but it should be catched.
            System.gc();
            throw new TwitterException("Unknown error", e);
        }
        final int statusCode = res.getStatusCode();
        if (statusCode < OK || statusCode > ACCEPTED)
            throw new TwitterException(res.asString(), req, res);
        return res;
    } catch (final IOException e) {
        // TODO
        if (resolver instanceof TwidereHostAddressResolver) {
            final TwidereHostAddressResolver twidereResolver = (TwidereHostAddressResolver) resolver;
            twidereResolver.removeCachedHost(host);
        }
        throw new TwitterException(e);
    }
}

From source file:org.getlantern.firetweet.extension.streaming.util.OkHttpClientImpl.java

License:Open Source License

@Override
public HttpResponse request(HttpRequest req) throws TwitterException {
    final Builder builder = new Builder();
    for (Entry<String, List<String>> headerEntry : req.getRequestHeaders().entrySet()) {
        final String name = headerEntry.getKey();
        for (String value : headerEntry.getValue()) {
            builder.addHeader(name, value);
        }//from   ww  w . j a  v  a 2  s  .co  m
    }
    final Authorization authorization = req.getAuthorization();
    if (authorization != null) {
        final String authHeader = authorization.getAuthorizationHeader(req);
        if (authHeader != null) {
            builder.header("Authorization", authHeader);
        }
    }
    try {
        setupRequestBuilder(builder, req);
        final Response response = client.newCall(builder.build()).execute();
        return new OkHttpResponse(conf, null, response);
    } catch (IOException e) {
        throw new TwitterException(e);
    }
}

From source file:org.getlantern.firetweet.util.net.OkHttpClientImpl.java

License:Open Source License

@Override
public HttpResponse request(HttpRequest req) throws TwitterException {
    final Builder builder = new Builder();
    for (Entry<String, List<String>> headerEntry : req.getRequestHeaders().entrySet()) {
        final String name = headerEntry.getKey();
        for (String value : headerEntry.getValue()) {
            builder.addHeader(name, value);
        }/* w w  w  .j ava  2s  .co  m*/
    }
    final Authorization authorization = req.getAuthorization();
    if (authorization != null) {
        final String authHeader = authorization.getAuthorizationHeader(req);
        if (authHeader != null) {
            builder.header("Authorization", authHeader);
        }
    }
    Response response = null;
    try {
        setupRequestBuilder(builder, req);
        response = client.newCall(builder.build()).execute();
        return new OkHttpResponse(conf, null, response);
    } catch (IOException e) {
        Crashlytics.logException(e);
        throw new TwitterException(e);
    }
}

From source file:org.mariotaku.twidere.util.net.HttpClientImpl.java

License:Apache License

@Override
public twitter4j.http.HttpResponse request(final twitter4j.http.HttpRequest req) throws TwitterException {
    try {/*from  w  ww.  ja v  a 2s .  c  om*/
        HttpRequestBase commonsRequest;

        final HostAddressResolver resolver = conf.getHostAddressResolver();
        final String urlString = req.getURL();
        final URI urlOrig;
        try {
            urlOrig = new URI(urlString);
        } catch (final URISyntaxException e) {
            throw new TwitterException(e);
        }
        final String host = urlOrig.getHost(), authority = urlOrig.getAuthority();
        final String resolvedHost = resolver != null ? resolver.resolve(host) : null;
        final String resolvedUrl = !isEmpty(resolvedHost)
                ? urlString.replace("://" + host, "://" + resolvedHost)
                : urlString;

        final RequestMethod method = req.getMethod();
        if (method == RequestMethod.GET) {
            commonsRequest = new HttpGet(resolvedUrl);
        } else if (method == RequestMethod.POST) {
            final HttpPost post = new HttpPost(resolvedUrl);
            // parameter has a file?
            boolean hasFile = false;
            final HttpParameter[] params = req.getParameters();
            if (params != null) {
                for (final HttpParameter param : params) {
                    if (param.isFile()) {
                        hasFile = true;
                        break;
                    }
                }
                if (!hasFile) {
                    if (params.length > 0) {
                        post.setEntity(new UrlEncodedFormEntity(params));
                    }
                } else {
                    final MultipartEntity me = new MultipartEntity();
                    for (final HttpParameter param : params) {
                        if (param.isFile()) {
                            final ContentBody body;
                            if (param.getFile() != null) {
                                body = new FileBody(param.getFile(), param.getContentType());
                            } else {
                                body = new InputStreamBody(param.getFileBody(), param.getFileName(),
                                        param.getContentType());
                            }
                            me.addPart(param.getName(), body);
                        } else {
                            final ContentBody body = new StringBody(param.getValue(),
                                    "text/plain; charset=UTF-8", Charset.forName("UTF-8"));
                            me.addPart(param.getName(), body);
                        }
                    }
                    post.setEntity(me);
                }
            }
            post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
            commonsRequest = post;
        } else if (method == RequestMethod.DELETE) {
            commonsRequest = new HttpDelete(resolvedUrl);
        } else if (method == RequestMethod.HEAD) {
            commonsRequest = new HttpHead(resolvedUrl);
        } else if (method == RequestMethod.PUT) {
            commonsRequest = new HttpPut(resolvedUrl);
        } else
            throw new TwitterException("Unsupported request method " + method);
        final Map<String, String> headers = req.getRequestHeaders();
        for (final String headerName : headers.keySet()) {
            commonsRequest.addHeader(headerName, headers.get(headerName));
        }
        final Authorization authorization = req.getAuthorization();
        final String authorizationHeader = authorization != null ? authorization.getAuthorizationHeader(req)
                : null;
        if (authorizationHeader != null) {
            commonsRequest.addHeader("Authorization", authorizationHeader);
        }
        if (resolvedHost != null && !resolvedHost.isEmpty() && !resolvedHost.equals(host)) {
            commonsRequest.addHeader("Host", authority);
        }

        final ApacheHttpClientHttpResponseImpl res;
        try {
            res = new ApacheHttpClientHttpResponseImpl(client.execute(commonsRequest), conf);
        } catch (final IllegalStateException e) {
            throw new TwitterException("Please check your API settings.", e);
        } catch (final NullPointerException e) {
            // Bug http://code.google.com/p/android/issues/detail?id=5255
            throw new TwitterException("Please check your APN settings, make sure not to use WAP APNs.", e);
        } catch (final OutOfMemoryError e) {
            // I don't know why OOM thown, but it should be catched.
            System.gc();
            throw new TwitterException("Unknown error", e);
        }
        final int statusCode = res.getStatusCode();
        if (statusCode < OK || statusCode > ACCEPTED)
            throw new TwitterException(res.asString(), req, res);
        return res;
    } catch (final IOException e) {
        throw new TwitterException(e);
    }
}

From source file:org.mariotaku.twidere.util.net.OkHttpClientImpl.java

License:Open Source License

@Override
public HttpResponse request(HttpRequest req) throws TwitterException {
    final Builder builder = new Builder();
    for (Entry<String, List<String>> headerEntry : req.getRequestHeaders().entrySet()) {
        final String name = headerEntry.getKey();
        for (String value : headerEntry.getValue()) {
            builder.addHeader(name, value);
        }/*from  w  ww.  j  ava  2  s.  c o m*/
    }
    final Authorization authorization = req.getAuthorization();
    if (authorization != null) {
        final String authHeader = authorization.getAuthorizationHeader(req);
        if (authHeader != null) {
            builder.header("Authorization", authHeader);
        }
    }
    Response response = null;
    try {
        setupRequestBuilder(builder, req);
        response = client.newCall(builder.build()).execute();
        return new OkHttpResponse(conf, null, response);
    } catch (IOException e) {
        throw new TwitterException(e);
    }
}