use of org.apache.http.entity.mime.content.StringBody in project lucene-solr by apache.
the class HttpSolrClient method createMethod.
protected HttpRequestBase createMethod(final SolrRequest request, String collection) throws IOException, SolrServerException {
SolrParams params = request.getParams();
Collection<ContentStream> streams = requestWriter.getContentStreams(request);
String path = requestWriter.getPath(request);
if (path == null || !path.startsWith("/")) {
path = DEFAULT_PATH;
}
ResponseParser parser = request.getResponseParser();
if (parser == null) {
parser = this.parser;
}
// The parser 'wt=' and 'version=' params are used instead of the original
// params
ModifiableSolrParams wparams = new ModifiableSolrParams(params);
if (parser != null) {
wparams.set(CommonParams.WT, parser.getWriterType());
wparams.set(CommonParams.VERSION, parser.getVersion());
}
if (invariantParams != null) {
wparams.add(invariantParams);
}
String basePath = baseUrl;
if (collection != null)
basePath += "/" + collection;
if (request instanceof V2Request) {
if (System.getProperty("solr.v2RealPath") == null) {
basePath = baseUrl.replace("/solr", "/v2");
} else {
basePath = baseUrl + "/____v2";
}
}
if (SolrRequest.METHOD.GET == request.getMethod()) {
if (streams != null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
}
return new HttpGet(basePath + path + wparams.toQueryString());
}
if (SolrRequest.METHOD.DELETE == request.getMethod()) {
return new HttpDelete(basePath + path + wparams.toQueryString());
}
if (SolrRequest.METHOD.POST == request.getMethod() || SolrRequest.METHOD.PUT == request.getMethod()) {
String url = basePath + path;
boolean hasNullStreamName = false;
if (streams != null) {
for (ContentStream cs : streams) {
if (cs.getName() == null) {
hasNullStreamName = true;
break;
}
}
}
boolean isMultipart = ((this.useMultiPartPost && SolrRequest.METHOD.POST == request.getMethod()) || (streams != null && streams.size() > 1)) && !hasNullStreamName;
LinkedList<NameValuePair> postOrPutParams = new LinkedList<>();
if (streams == null || isMultipart) {
// send server list and request list as query string params
ModifiableSolrParams queryParams = calculateQueryParams(this.queryParams, wparams);
queryParams.add(calculateQueryParams(request.getQueryParams(), wparams));
String fullQueryUrl = url + queryParams.toQueryString();
HttpEntityEnclosingRequestBase postOrPut = SolrRequest.METHOD.POST == request.getMethod() ? new HttpPost(fullQueryUrl) : new HttpPut(fullQueryUrl);
if (!isMultipart) {
postOrPut.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
}
List<FormBodyPart> parts = new LinkedList<>();
Iterator<String> iter = wparams.getParameterNamesIterator();
while (iter.hasNext()) {
String p = iter.next();
String[] vals = wparams.getParams(p);
if (vals != null) {
for (String v : vals) {
if (isMultipart) {
parts.add(new FormBodyPart(p, new StringBody(v, StandardCharsets.UTF_8)));
} else {
postOrPutParams.add(new BasicNameValuePair(p, v));
}
}
}
}
// TODO: remove deprecated - first simple attempt failed, see {@link MultipartEntityBuilder}
if (isMultipart && streams != null) {
for (ContentStream content : streams) {
String contentType = content.getContentType();
if (contentType == null) {
// default
contentType = BinaryResponseParser.BINARY_CONTENT_TYPE;
}
String name = content.getName();
if (name == null) {
name = "";
}
parts.add(new FormBodyPart(name, new InputStreamBody(content.getStream(), contentType, content.getName())));
}
}
if (parts.size() > 0) {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
for (FormBodyPart p : parts) {
entity.addPart(p);
}
postOrPut.setEntity(entity);
} else {
//not using multipart
postOrPut.setEntity(new UrlEncodedFormEntity(postOrPutParams, StandardCharsets.UTF_8));
}
return postOrPut;
} else // It is has one stream, it is the post body, put the params in the URL
{
String fullQueryUrl = url + wparams.toQueryString();
HttpEntityEnclosingRequestBase postOrPut = SolrRequest.METHOD.POST == request.getMethod() ? new HttpPost(fullQueryUrl) : new HttpPut(fullQueryUrl);
// Single stream as body
// Using a loop just to get the first one
final ContentStream[] contentStream = new ContentStream[1];
for (ContentStream content : streams) {
contentStream[0] = content;
break;
}
if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
Long size = contentStream[0].getSize();
postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {
@Override
public Header getContentType() {
return new BasicHeader("Content-Type", contentStream[0].getContentType());
}
@Override
public boolean isRepeatable() {
return false;
}
});
} else {
Long size = contentStream[0].getSize();
postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {
@Override
public Header getContentType() {
return new BasicHeader("Content-Type", contentStream[0].getContentType());
}
@Override
public boolean isRepeatable() {
return false;
}
});
}
return postOrPut;
}
}
throw new SolrServerException("Unsupported method: " + request.getMethod());
}
use of org.apache.http.entity.mime.content.StringBody in project streamsx.topology by IBMStreams.
the class RestUtils method postJob.
/**
* Submit an application bundle to execute as a job.
*/
public static JsonObject postJob(CloseableHttpClient httpClient, JsonObject service, File bundle, JsonObject jobConfigOverlay) throws ClientProtocolException, IOException {
final String serviceName = jstring(service, "name");
final JsonObject credentials = service.getAsJsonObject("credentials");
String url = getJobSubmitURL(credentials, bundle);
HttpPost postJobWithConfig = new HttpPost(url);
postJobWithConfig.addHeader("accept", ContentType.APPLICATION_JSON.getMimeType());
postJobWithConfig.addHeader(AUTH.WWW_AUTH_RESP, getAPIKey(credentials));
FileBody bundleBody = new FileBody(bundle, ContentType.APPLICATION_OCTET_STREAM);
StringBody configBody = new StringBody(jobConfigOverlay.toString(), ContentType.APPLICATION_JSON);
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("sab", bundleBody).addPart(DeployKeys.JOB_CONFIG_OVERLAYS, configBody).build();
postJobWithConfig.setEntity(reqEntity);
JsonObject jsonResponse = getGsonResponse(httpClient, postJobWithConfig);
RemoteContext.REMOTE_LOGGER.info("Streaming Analytics service (" + serviceName + "): submit job response:" + jsonResponse.toString());
return jsonResponse;
}
use of org.apache.http.entity.mime.content.StringBody in project sling by apache.
the class ConsoleTestClient method testResourceResolver.
@Test
public void testResourceResolver() throws Exception {
RequestBuilder rb = new RequestBuilder("http://localhost:9000");
final MultipartEntity entity = new MultipartEntity();
// Add Sling POST options
entity.addPart("lang", new StringBody("esp"));
entity.addPart("code", new InputStreamBody(getClass().getResourceAsStream("/test.js"), "test.js"));
executor.execute(rb.buildPostRequest("/system/console/scriptconsole.json").withEntity(entity).withCredentials("admin", "admin")).assertStatus(200);
}
use of org.apache.http.entity.mime.content.StringBody in project sling by apache.
the class WebconsoleClient method startBundle.
/** Start specified bundle */
public void startBundle(String symbolicName) throws Exception {
// To start the bundle we POST action=start to its URL
final String path = getBundlePath(symbolicName, null);
log.info("Starting bundle {} via {}", symbolicName, path);
final MultipartEntity entity = new MultipartEntity();
entity.addPart("action", new StringBody("start"));
executor.execute(builder.buildPostRequest(path).withCredentials(username, password).withEntity(entity)).assertStatus(200);
}
use of org.apache.http.entity.mime.content.StringBody in project sling by apache.
the class WebconsoleClient method refreshPackages.
/** Calls PackageAdmin.refreshPackages to enforce re-wiring of all bundles. */
public void refreshPackages() throws Exception {
log.info("Refresh packages.");
final MultipartEntity entity = new MultipartEntity();
entity.addPart("action", new StringBody("refreshPackages"));
executor.execute(builder.buildPostRequest(CONSOLE_BUNDLES_PATH).withCredentials(username, password).withEntity(entity)).assertStatus(200);
}
Aggregations