Example usage for twitter4j TwitterException TwitterException

List of usage examples for twitter4j TwitterException TwitterException

Introduction

In this page you can find the example usage for twitter4j TwitterException TwitterException.

Prototype

public TwitterException(String message, HttpResponse res) 

Source Link

Usage

From source file:cmu.edu.homework.mediaUpload.MiMp4Upload.java

License:Apache License

@Override
protected String postUpload() throws TwitterException {
    int statusCode = httpResponse.getStatusCode();
    if (statusCode != 200) {
        throw new TwitterException("YFrog image upload returned invalid status code", httpResponse);
    }/*w  w w . j  a  v a 2 s.co  m*/

    String response = httpResponse.asString();
    if (response.contains("<rsp stat=\"fail\">")) {
        String error = response.substring(response.indexOf("msg") + 5, response.lastIndexOf("\""));
        throw new TwitterException("YFrog image upload failed with this error message: " + error, httpResponse);
    }
    if (response.contains("<rsp stat=\"ok\">")) {
        return response.substring(response.indexOf("<mediaurl>") + "<mediaurl>".length(),
                response.indexOf("</mediaurl>"));
    }

    throw new TwitterException("Unknown Mi  response", httpResponse);
}

From source file:com.dwdesign.tweetings.util.httpclient.HttpClientImpl.java

License:Apache License

@Override
public twitter4j.internal.http.HttpResponse request(final twitter4j.internal.http.HttpRequest req)
        throws TwitterException {
    try {//from   www . j av a 2  s  .c o  m
        HttpRequestBase commonsRequest;

        //final HostAddressResolver resolver = conf.getHostAddressResolver();
        final String url_string = req.getURL();
        final URL url_orig = new URL(url_string);
        final String host = url_orig.getHost();
        //final String resolved_host = resolver != null ? resolver.resolve(host) : null;
        //final String resolved_url = resolved_host != null ? url_string.replace("://" + host, "://" + resolved_host)
        //      : url_string;
        final String resolved_url = url_string;
        if (req.getMethod() == RequestMethod.GET) {
            commonsRequest = new HttpGet(resolved_url);
        } else if (req.getMethod() == RequestMethod.POST) {
            final HttpPost post = new HttpPost(resolved_url);
            // parameter has a file?
            boolean hasFile = false;
            if (req.getParameters() != null) {
                for (final HttpParameter parameter : req.getParameters()) {
                    if (parameter.isFile()) {
                        hasFile = true;
                        break;
                    }
                }
                if (!hasFile) {
                    final ArrayList<NameValuePair> args = new ArrayList<NameValuePair>();
                    for (final HttpParameter parameter : req.getParameters()) {
                        args.add(new BasicNameValuePair(parameter.getName(), parameter.getValue()));
                    }
                    if (args.size() > 0) {
                        post.setEntity(new UrlEncodedFormEntity(args, "UTF-8"));
                    }
                } else {
                    final MultipartEntity me = new MultipartEntity();
                    for (final HttpParameter parameter : req.getParameters()) {
                        if (parameter.isFile()) {
                            final ContentBody body = new FileBody(parameter.getFile(),
                                    parameter.getContentType());
                            me.addPart(parameter.getName(), body);
                        } else {
                            final ContentBody body = new StringBody(parameter.getValue(),
                                    "text/plain; charset=UTF-8", Charset.forName("UTF-8"));
                            me.addPart(parameter.getName(), body);
                        }
                    }
                    post.setEntity(me);
                }
            }
            post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
            commonsRequest = post;
        } else if (req.getMethod() == RequestMethod.DELETE) {
            commonsRequest = new HttpDelete(resolved_url);
        } else if (req.getMethod() == RequestMethod.HEAD) {
            commonsRequest = new HttpHead(resolved_url);
        } else if (req.getMethod() == RequestMethod.PUT) {
            commonsRequest = new HttpPut(resolved_url);
        } else
            throw new AssertionError();
        final Map<String, String> headers = req.getRequestHeaders();
        for (final String headerName : headers.keySet()) {
            commonsRequest.addHeader(headerName, headers.get(headerName));
        }
        String authorizationHeader;
        if (req.getAuthorization() != null
                && (authorizationHeader = req.getAuthorization().getAuthorizationHeader(req)) != null) {
            commonsRequest.addHeader("Authorization", authorizationHeader);
        }
        //if (resolved_host != null && !host.equals(resolved_host)) {
        //commonsRequest.addHeader("Host", host);
        //}
        final ApacheHttpClientHttpResponseImpl res = new ApacheHttpClientHttpResponseImpl(
                client.execute(commonsRequest), conf);
        final int statusCode = res.getStatusCode();
        if (statusCode < OK && statusCode > ACCEPTED)
            throw new TwitterException(res.asString(), res);
        return res;
    } catch (final IOException e) {
        throw new TwitterException(e);
    }
}