Example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity

List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity

Introduction

In this page you can find the example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity.

Prototype

public ByteArrayEntity(byte[] bArr) 

Source Link

Usage

From source file:com.ab.network.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 */// w w w . ja va  2 s  .  c o m
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:org.skfiy.typhon.spi.auth.p.UCRoleListener.java

@Override
public void roleCreated(Role role) {
    Object p = SessionContext.getSession().getAttribute(UCAuthenticator.SESSION_SID_KEY);
    if (p == null) {
        return;//  ww  w  .j  a  v a2  s  .  c  o  m
    }

    CloseableHttpClient hc = UCAuthenticator.HC_BUILDER.build();
    HttpPost httpPost = new HttpPost("http://sdk.g.uc.cn/cp/account.verifySession");

    JSONObject json = new JSONObject();
    json.put("id", System.currentTimeMillis());
    json.put("service", "ucid.game.gameData");

    String sid = (String) p;
    String gameData = getGameData();

    JSONObject data = new JSONObject();
    data.put("sid", sid);
    data.put("gameData", gameData);
    json.put("data", data);

    JSONObject game = new JSONObject();
    game.put("gameId", UCAuthenticator.gameId);
    json.put("game", game);

    json.put("sign", getSign(gameData, sid));

    try {
        String body = json.toJSONString();
        LOG.debug("UC loginGameRole request: {}", body);

        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new ByteArrayEntity(body.getBytes("UTF-8")));
        hc.execute(httpPost, new ResponseHandler<JSONObject>() {

            @Override
            public JSONObject handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {
                String str = StreamUtils.copyToString(response.getEntity().getContent(),
                        StandardCharsets.UTF_8);
                LOG.debug("UC loginGameRole response: {}", str);
                return JSON.parseObject(str);
            }
        });
    } catch (IOException e) {
        throw new OAuth2Exception("UC??", e);
    } finally {
        try {
            hc.close();
        } catch (IOException ex) {
        }
    }
}

From source file:com.volley.android.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///from w w  w  .j a  v a  2s .  c om
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Request.Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Request.Method.GET:
        return new HttpGet(request.getUrl());
    case Request.Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Request.Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Request.Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:com.jkoolcloud.jesl.net.http.apache.HttpMessageUtils.java

/**
 * Set HTTP message content/*from w  ww  .  j  a v a  2s .c  o  m*/
 *
 * @param message HTTP message
 * @param contentType contentType
 * @param content content bytes
 * @param contentEncoding content encoding
 * @throws IOException if error writing message content
 */
public static void setContent(HttpMessage message, String contentType, byte[] content, String contentEncoding)
        throws IOException {
    ByteArrayEntity httpContent = new ByteArrayEntity(content);
    if (!StringUtils.isEmpty(contentEncoding))
        httpContent.setContentEncoding(contentEncoding);
    message.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(httpContent.getContentLength()));
    if (!StringUtils.isEmpty(contentType))
        message.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
    setEntity(message, httpContent);
}

From source file:com.wudoumi.batter.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from  www  .  j  a v  a  2s. co  m*/
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case RequestType.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case RequestType.GET:
        return new HttpGet(request.getUrl());
    case RequestType.DELETE:
        return new HttpDelete(request.getUrl());
    case RequestType.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case RequestType.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:com.mr.http.toolbox.MR_HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from w ww.  j av  a 2s .  c o m*/
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(MR_Request<?> request,
        Map<String, String> additionalHeaders) throws MR_AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:org.obm.sync.push.client.commands.SmartEmailCommand.java

@Override
public Boolean run(AccountInfos ai, OPClient opc, HttpClient hc) throws Exception {
    return Request
            .Post(opc.buildUrl(ai.getUrl(), ai.getLogin(), ai.getDevId(), ai.getDevType(), getCommandName(),
                    commandParams()))/* ww w .  j av a  2 s.  com*/
            .addHeader("User-Agent", ai.getUserAgent()).addHeader("Authorization", ai.authValue())
            .addHeader("Ms-Asprotocolversion", ProtocolVersion.V121.asSpecificationValue())
            .addHeader("Accept", "*/*").addHeader("Accept-Language", "fr-fr")
            .addHeader("Connection", "keep-alive").body(new ByteArrayEntity(emailData)).execute()
            .returnResponse().getStatusLine().getStatusCode() == HttpStatus.SC_OK;
}

From source file:com.autonavi.gxdtaojin.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///  w  ww .ja va  2s .  c o  m
@SuppressWarnings("deprecation")
/* protected */static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:org.fcrepo.camel.FcrepoClientErrorTest.java

@Test
public void testGetError() throws Exception {
    final int status = 400;
    final URI uri = create(baseUrl);
    final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());

    entity.setContentType(RDF_XML);/* w  ww .  ja v  a  2 s  . co  m*/
    doSetupMockRequest(RDF_XML, entity, status);

    final FcrepoResponse response = testClient.get(uri, RDF_XML, "return=representation;");

    assertEquals(response.getUrl(), uri);
    assertEquals(response.getStatusCode(), status);
    assertEquals(response.getContentType(), RDF_XML);
    assertEquals(response.getLocation(), null);
    assertEquals(IOUtils.toString(response.getBody()), rdfXml);
}

From source file:com.klinker.android.twitter.utils.api_helper.TweetMarkerHelper.java

public boolean sendCurrentId(String collection, long id) {
    try {/*from  w  w w  . ja  v  a2 s.c o  m*/
        HttpPost post = new HttpPost(postURL);
        post.addHeader("X-Auth-Service-Provider", SERVICE_PROVIDER);
        post.addHeader("X-Verify-Credentials-Authorization", getAuthrityHeader(twitter));

        JSONObject json = new JSONObject();
        json.put("id", id);
        JSONObject base = new JSONObject();
        base.put(collection, json);

        Log.v("talon_tweetmarker", "sending " + id + " to " + screenname);

        post.setEntity(new ByteArrayEntity(base.toString().getBytes("UTF8")));
        DefaultHttpClient client = new DefaultHttpClient();

        HttpResponse response = client.execute(post);
        int responseCode = response.getStatusLine().getStatusCode();
        Log.v("talon_tweetmarker", "sending response code: " + responseCode);

        if (responseCode != 200) { // there was an error, we will retry once
            // wait first
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {

            }

            response = client.execute(post);
            responseCode = response.getStatusLine().getStatusCode();
            Log.v("talon_tweetmarker", "sending response code: " + responseCode);

            if (responseCode == 200) {
                // success, return true
                int currentVersion = sharedPrefs.getInt("last_version_account_" + currentAccount, 0);
                sharedPrefs.edit().putInt("last_version_account_" + currentAccount, currentVersion + 1)
                        .commit();
                return true;
            }

        } else {
            int currentVersion = sharedPrefs.getInt("last_version_account_" + currentAccount, 0);
            sharedPrefs.edit().putInt("last_version_account_" + currentAccount, currentVersion + 1).commit();
            return true;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}