/** * Adds a mapping type to an index if it does not exist. * * @param index The name of the index. * @param mappingType The name of the mapping type. * @param mappingFilename The name of the mapping file to use to add the mapping if it does not exist. * @throws IOException If an error occurred during requests to ES. */ private void addMappingToIndex(final String index, final String mappingType, final String mappingFilename) throws IOException { logger.info("Adding '{}' mapping to index '{}'...", mappingType, index); String resourcePath = "/" + index + "/_mapping/" + mappingType; if (doesResourceNotExist(resourcePath)) { InputStream stream = ElasticSearchDAOV5.class.getResourceAsStream(mappingFilename); byte[] mappingSource = IOUtils.toByteArray(stream); HttpEntity entity = new NByteArrayEntity(mappingSource, ContentType.APPLICATION_JSON); elasticSearchAdminClient.performRequest(HttpMethod.PUT, resourcePath, Collections.emptyMap(), entity); logger.info("Added '{}' mapping", mappingType); } else { logger.info("Mapping '{}' already exists", mappingType); } }
public static void toHttpResponse(byte[] byteArray, HttpResponse response, String contentType) throws IOException { NByteArrayEntity body = new NByteArrayEntity(byteArray); body.setContentType(contentType); response.setEntity(body); response.setStatusCode(HttpStatus.SC_OK); }
/** * @since 4.2 */ public NByteArrayEntity(final byte[] b, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); this.b = b; this.off = 0; this.len = b.length; this.buf = ByteBuffer.wrap(b); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
/** * {@inheritDoc} * * @deprecated (4.2) use {@link #close()} */ @Deprecated public void finish() { close(); }
/** * @since 4.2 */ public NByteArrayEntity(final byte[] b, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); this.b = b; this.off = 0; this.len = b.length; this.buf = ByteBuffer.wrap(b); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
/** * {@inheritDoc} * * @deprecated (4.2) use {@link #close()} */ @Deprecated public void finish() { close(); }
/** * Initializes the index with the required templates and mappings. */ private void initIndex() throws Exception { //0. Add the tasklog template if (doesResourceNotExist("/_template/tasklog_template")) { logger.info("Creating the index template 'tasklog_template'"); InputStream stream = ElasticSearchDAOV5.class.getResourceAsStream("/template_tasklog.json"); byte[] templateSource = IOUtils.toByteArray(stream); HttpEntity entity = new NByteArrayEntity(templateSource, ContentType.APPLICATION_JSON); try { elasticSearchAdminClient.performRequest(HttpMethod.PUT, "/_template/tasklog_template", Collections.emptyMap(), entity); } catch (IOException e) { logger.error("Failed to initialize tasklog_template", e); } } }
public static void toHttpResponse(byte[] byteArray, HttpResponse response, String contentType) throws IOException { NByteArrayEntity body = new NByteArrayEntity(byteArray); body.setContentType(contentType); response.setEntity(body); response.setStatusCode(HttpStatus.SC_OK); }
/** * @since 4.2 */ public NByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) < 0) || ((off + len) > b.length)) { throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length); } this.b = b; this.off = off; this.len = len; this.buf = ByteBuffer.wrap(b, off, len); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
@Override protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers); if (this.httpRequest instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest; HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput); entityEnclosingRequest.setEntity(requestEntity); } HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest); Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback); return new ClientHttpResponseFuture(futureResponse, callback); }
private static NByteArrayEntity byteBody(HttpReq config) { NByteArrayEntity entity = new NByteArrayEntity(config.body()); if (config.contentType() != null) { entity.setContentType(config.contentType()); } return entity; }
/** * @since 4.2 */ public NByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) < 0) || ((off + len) > b.length)) { throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length); } this.b = b; this.off = off; this.len = len; this.buf = ByteBuffer.wrap(b, off, len); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
private static NByteArrayEntity paramsBody(Map<String, Object> data, Map<String, List<Upload>> files) { data = U.safe(data); files = U.safe(files); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Map.Entry<String, List<Upload>> entry : files.entrySet()) { for (Upload file : entry.getValue()) { builder = builder.addBinaryBody(entry.getKey(), file.content(), ContentType.DEFAULT_BINARY, file.filename()); } } for (Map.Entry<String, Object> entry : data.entrySet()) { String name = entry.getKey(); String value = JSON.stringify(entry.getValue()); builder = builder.addTextBody(name, value, ContentType.APPLICATION_JSON); } ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { builder.build().writeTo(stream); } catch (IOException e) { throw U.rte(e); } byte[] bytes = stream.toByteArray(); return new NByteArrayEntity(bytes, ContentType.MULTIPART_FORM_DATA); }
public static void toHttpResponse(byte[] byteArray, HttpResponse response, String contentType) throws IOException { NByteArrayEntity body = new NByteArrayEntity(byteArray); body.setContentType(contentType); response.setEntity(body); response.setStatusCode(HttpStatus.SC_OK); }
@Override protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers); if (this.httpRequest instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest; HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput); entityEnclosingRequest.setEntity(requestEntity); } HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest); Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback); return new ClientHttpResponseFuture(futureResponse, callback); }
ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) {
private static void setBody(HttpRequest request, boolean async, HttpRequestBase reqObj) { if (!(request.getHttpMethod() == HttpMethod.GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new RuntimeException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } }
ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) {
ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) {
private void setBody(HttpRequestBase reqObj) { if (!(request.getHttpMethod() == GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new UnirestException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } } }