/** * @param status Length */ public DelayedHttpMultipartEntity(final String filename, final TransferStatus status, final String boundary) { this.status = status; final StringBuilder multipartHeader = new StringBuilder(); multipartHeader.append(TWO_DASHES); multipartHeader.append(boundary); multipartHeader.append(CR_LF); multipartHeader.append(String.format("Content-Disposition: form-data; name=\"file\"; filename=\"%s\"", filename)); multipartHeader.append(CR_LF); multipartHeader.append(String.format("%s: %s", HTTP.CONTENT_TYPE, StringUtils.isBlank(status.getMime()) ? MimeTypeService.DEFAULT_CONTENT_TYPE : status.getMime())); multipartHeader.append(CR_LF); multipartHeader.append(CR_LF); header = encode(MIME.DEFAULT_CHARSET, multipartHeader.toString()).buffer(); final StringBuilder multipartFooter = new StringBuilder(); multipartFooter.append(CR_LF); multipartFooter.append(TWO_DASHES); multipartFooter.append(boundary); multipartFooter.append(TWO_DASHES); multipartFooter.append(CR_LF); footer = encode(MIME.DEFAULT_CHARSET, multipartFooter.toString()).buffer(); }
@Test public void testUpdateRequest() { UpdateRequest updateRequest = Mockito.mock(UpdateRequest.class); when(updateRequest.getCommand()).thenReturn("getUpdates"); MultipartEntityBuilder requestEntity = MultipartEntityBuilder.create(); // New MIME type, data need to be UTF-8 encrypted ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), MIME.UTF8_CHARSET); // Set timout, limit and offset requestEntity.addTextBody("timeout", "60", contentType); requestEntity.addTextBody("limit", "100", contentType); requestEntity.addTextBody("offset", "1337", contentType); when(updateRequest.getRequestEntity()).thenReturn(requestEntity); BotRequest request = new BotRequest(updateRequest); assertEquals("getUpdates", request.getCommand()); assertEquals(requestEntity, request.getContent()); }
/** * Runs a job with the defined parameters. * * @param jobName name of the job * @param jobParameters job parameters for the execution * @param springBatchServiceUri the URI of Spring Batch Admin * @return JobExecution * @throws ServiceNotAvailableException * @throws ServiceInternalServerError * @throws Exception */ public static JobExecution runJob(String jobName, String jobParameters, String springBatchServiceUri) throws ResourceNotFoundException, UnknownException, IOException, ServiceNotAvailableException, ServiceInternalServerError { HttpPost postJob = new HttpPost(springBatchServiceUri + "/jobs/" + jobName + ".json"); postJob.addHeader(MIME.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED); log.debug("execute " + jobName + " with parameters: " + jobParameters); ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("jobParameters", jobParameters)); postJob.setEntity(new UrlEncodedFormEntity(postParameters)); String jsonString = apiRequest(postJob); Gson gson = new Gson(); JobExecutionWrapper execution = gson.fromJson(jsonString, JobExecutionWrapper.class); return execution.getJobExecution(); }
@Override protected ContentResult doCreate(Response response, InputStream resultStream, Runnable closeResult) { final int length = response.getHeaderValue("Content-Length", -1); final String format = response.getHeaderValue(MIME.CONTENT_TYPE, MediaTypes.BINARY); final String name = extractFileNameFrom(response.getHeaderValue(MIME.CONTENT_DISPOSITION, "")); return new DefaultContentResult(name, length, format, resultStream, closeResult); }
@Override public T handleResponse(HttpResponse httpResponse) throws IOException { StatusLine statusLine = httpResponse.getStatusLine(); if (statusLine.getStatusCode() == 200) { String result = EntityUtils.toString(httpResponse.getEntity(), MIME.UTF8_CHARSET); Gson gson = new Gson(); return gson.fromJson(result, resultClass); } else { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } }
public UpdateRequest(Integer nextExpectedMsg) { this.offset = nextExpectedMsg; this.requestEntity = MultipartEntityBuilder.create(); // New MIME type, data need to be UTF-8 encrypted ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), MIME.UTF8_CHARSET); // Set timout, limit and offset requestEntity.addTextBody(KEY_MSG_UPDATE_TIMEOUT, TIMEOUT.toString(), contentType); requestEntity.addTextBody(KEY_MSG_UPDATE_LIMIT, LIMIT.toString(), contentType); requestEntity.addTextBody(KEY_MSG_UPDATE_OFFSET, offset.toString(), contentType); }
/** * Send a message to Telegram. This could be a text, image, audio, ... * message. * * @param message */ public BotRequest(Message message) { this.command = message.getCommand(); this.requestEntity = MultipartEntityBuilder.create(); // New MIME type, data need to be UTF-8 encrypted ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), MIME.UTF8_CHARSET); // Set recipient, needed for every message requestEntity.addTextBody(KEY_RECIPIENT, message.getRecipient().toString(), contentType); // Add some more things, like a message text or an image switch (message.getMessageType()) { case TEXT_MESSAGE: requestEntity.addTextBody(message.getJsonKey(), (String) message.getMessage(), contentType); break; case IMAGE_MESSAGE: case DOCUMENT_MESSAGE: case AUDIO_MESSAGE: case VIDEO_MESSAGE: requestEntity.addBinaryBody(message.getJsonKey(), (File) message.getMessage()); break; default: LOG.error("Message type is invalid, not supported or not yet implemented"); break; } }
protected FormBodyPart createMultipartBodypart(String name, String message, String contentType) { FormBodyPartBuilder bodyPart = FormBodyPartBuilder.create() .setName(name) .setBody(new StringBody(message, ContentType.create(contentType, getCharSet()))); if (StringUtils.isNotEmpty(getMtomContentTransferEncoding())) bodyPart.setField(MIME.CONTENT_TRANSFER_ENC, getMtomContentTransferEncoding()); return bodyPart.build(); }
public String getTransferEncoding() { return MIME.ENC_BINARY; }
public String getTransferEncoding() { return MIME.ENC_8BIT; }
public String getTransferEncoding() { return (MIME.ENC_BINARY); }
@Override public String getTransferEncoding() { return MIME.ENC_BINARY; }
@Override public String getTransferEncoding() { return MIME.ENC_8BIT; }