use of org.apache.http.entity.ByteArrayEntity in project feign by OpenFeign.
the class ApacheHttpClient method toHttpUriRequest.
HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
RequestBuilder requestBuilder = RequestBuilder.create(request.method());
//per request timeouts
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(options.connectTimeoutMillis()).setSocketTimeout(options.readTimeoutMillis()).build();
requestBuilder.setConfig(requestConfig);
URI uri = new URIBuilder(request.url()).build();
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
//request query params
List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset().name());
for (NameValuePair queryParam : queryParams) {
requestBuilder.addParameter(queryParam);
}
//request headers
boolean hasAcceptHeader = false;
for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
String headerName = headerEntry.getKey();
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
hasAcceptHeader = true;
}
if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)) {
// doesn't like us to set it as well.
continue;
}
for (String headerValue : headerEntry.getValue()) {
requestBuilder.addHeader(headerName, headerValue);
}
}
//some servers choke on the default accept string, so we'll set it to anything
if (!hasAcceptHeader) {
requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
}
//request body
if (request.body() != null) {
HttpEntity entity = null;
if (request.charset() != null) {
ContentType contentType = getContentType(request);
String content = new String(request.body(), request.charset());
entity = new StringEntity(content, contentType);
} else {
entity = new ByteArrayEntity(request.body());
}
requestBuilder.setEntity(entity);
}
return requestBuilder.build();
}
use of org.apache.http.entity.ByteArrayEntity in project platformlayer by platformlayer.
the class MetricClientImpl method sendMetrics.
// TODO: Throw on failure??
@Override
public boolean sendMetrics(MetricTreeObject tree) {
if (tags != null) {
tree.mergeTree(tags);
}
URI url = metricBaseUrl.resolve("api/metric/").resolve(project);
HttpPost request = new HttpPost(url);
HttpResponse response = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
metricTreeSerializer.serialize(tree, baos);
baos.close();
byte[] data = baos.toByteArray();
log.debug("POSTing " + new String(data));
// TODO: Stream body? We'd just need a custom ByteArrayEntity class
request.setEntity(new ByteArrayEntity(data));
response = httpClient.execute(request);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() != 200) {
log.warn("Error writing to PlatformLayer metrics server: " + statusLine);
return false;
} else {
EntityUtils.consume(response.getEntity());
response = null;
// consumeResponse(request, response);
log.debug("Posted metrics");
return true;
}
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug("Error writing to PlatformLayer metrics server", e);
}
log.warn("Error writing to PlatformLayer metrics server {}", e.getMessage());
return false;
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
log.warn("Error consuming response", e);
}
}
}
}
use of org.apache.http.entity.ByteArrayEntity in project JamsMusicPlayer by psaravan.
the class GMusicClientCalls method getPlaylistEntriesWebClient.
/**************************************************************************************************
* Retrieves a JSONAray with all songs within the <b><i>specified</b></i> playlist. The JSONArray
* contains the fields of the songs such as "id", "clientId", "trackId", etc. (for a list
* of all fields, see WebClientSongsSchema.java). Uses the WebClient endpoint.
*
* @return A JSONArray object that contains the songs and their fields within the specified playlist.
* @param context The context to use while retrieving songs from the playlist.
* @param playlistId The id of the playlist we need to fetch the songs from.
**************************************************************************************************/
public static final JSONArray getPlaylistEntriesWebClient(Context context, String playlistId) throws JSONException, IllegalArgumentException {
JSONObject jsonParam = new JSONObject();
jsonParam.putOpt("id", playlistId);
JSONForm form = new JSONForm();
form.addField("json", jsonParam.toString());
form.close();
mHttpClient.setUserAgent(mMobileClientUserAgent);
String result = mHttpClient.post(context, "https://play.google.com/music/services/loadplaylist?u=0&xt=" + getXtCookieValue(), new ByteArrayEntity(form.toString().getBytes()), form.getContentType());
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject(result);
if (jsonObject != null) {
jsonArray = jsonObject.getJSONArray("playlist");
}
return jsonArray;
}
use of org.apache.http.entity.ByteArrayEntity in project JamsMusicPlayer by psaravan.
the class GMusicClientCalls method login.
/*******************************************************************************
* Attempts to log the user into the "sj" (SkyJam) service using the provided
* authentication token. The authentication token is unique for each session
* and user account. It can be obtained via the GoogleAuthUtil.getToken()
* method. See AsyncGoogleMusicAuthenticationTask.java for the current
* implementation of this process. This method will return true if the login
* process succeeded. Returns false for any other type of failure.
*
* @param context The context that will be used for the login process.
* @param authToken The authentication token that will be used to login.
*******************************************************************************/
public static final boolean login(Context context, String authToken) {
if (!TextUtils.isEmpty(authToken)) {
JSONForm form = new JSONForm().close();
GMusicClientCalls.setAuthorizationHeader(authToken);
String response = mHttpClient.post(context, "https://play.google.com/music/listen?hl=en&u=0", new ByteArrayEntity(form.toString().getBytes()), form.getContentType());
//Check if the required paramters are null.
if (response != null) {
if (getXtCookieValue() != null) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
use of org.apache.http.entity.ByteArrayEntity in project JamsMusicPlayer by psaravan.
the class GMusicClientCalls method modifyPlaylist.
/******************************************************************************************
* Executes a single/batch modification operation on a playlist's entry(ies). This method
* is a general purpose method that simply hits the MobileClient endpoints using
* mPlaylistEntriesMutationsArray. Supported mutation operations include "create",
* "delete", and "update".
*
* @param context The context to use while carrying out the modification operation.
* @param mutationsArray The JSONArray that contains the mutations command to be
* carried out.
* @return The JSON response as a String.
* @throws JSONException
* @throws IllegalArgumentException
******************************************************************************************/
public static final String modifyPlaylist(Context context) throws JSONException, IllegalArgumentException {
JSONObject jsonParam = new JSONObject();
jsonParam.put("mutations", mPlaylistEntriesMutationsArray);
mHttpClient.setUserAgent(mMobileClientUserAgent);
String result = mHttpClient.post(context, "https://www.googleapis.com/sj/v1.1/plentriesbatch?alt=json&hl=en_US", new ByteArrayEntity(jsonParam.toString().getBytes()), "application/json");
mHttpClient.setUserAgent(mWebClientUserAgent);
//Clear out and reset the mutationsArray now that we're done using it.
mPlaylistEntriesMutationsArray = null;
mPlaylistEntriesMutationsArray = new JSONArray();
return result;
}
Aggregations