List of usage examples for org.apache.http.entity ContentType parse
public static ContentType parse(String str) throws ParseException, UnsupportedCharsetException
From source file:com.qwazr.webapps.transaction.body.HttpBodyInterface.java
static HttpBodyInterface newEntity(HttpServletRequest request) throws IOException, ServletException { final String contentTypeString = request.getContentType(); if (contentTypeString != null) { final ContentType contentType = ContentType.parse(contentTypeString); final String mimeType = contentType.getMimeType(); if ("application/x-www-form-urlencoded".equals(mimeType)) return new FormHttpBody(request); if ("multipart/form-data".equals(mimeType)) return new MultipartHttpBody(request); if ("application/xml".equals(mimeType)) return new XMLHttpBody(request); }//from w w w . ja v a 2s . c o m throw new WebappException(Status.NOT_ACCEPTABLE, Title.BODY_ERROR, "Not supported content type: " + contentTypeString); }
From source file:com.joyent.manta.http.ContentTypeLookup.java
/** * Finds the content type set in {@link MantaHttpHeaders} and returns that if it * is not null. Otherwise, it will return the specified default content type. * * @param headers headers to parse for content type * @param defaultContentType content type to default to * @return content type object/*from w w w . j a va2s . c o m*/ */ public static ContentType findOrDefaultContentType(final MantaHttpHeaders headers, final ContentType defaultContentType) { if (headers == null || headers.getContentType() == null) { return defaultContentType; } else { return ContentType.parse(headers.getContentType()); } }
From source file:io.cloudslang.content.httpclient.build.HeadersBuilderTest.java
@Test public void build() { ContentType contentType = ContentType.parse(CONTENT_TYPE); List<Header> headers = new HeadersBuilder().setHeaders("header1:value1\nheader2:value2") .setContentType(contentType).buildHeaders(); assertEquals(3, headers.size());/*from www . java 2 s. c o m*/ assertThat(headers.get(0), instanceOf(BufferedHeader.class)); BufferedHeader basicHeader = (BufferedHeader) headers.get(1); assertEquals("header2", basicHeader.getName()); assertEquals("value2", basicHeader.getValue()); BasicHeader contentTypeHeader = (BasicHeader) headers.get(2); assertEquals("Content-Type", contentTypeHeader.getName()); assertEquals(CONTENT_TYPE, contentTypeHeader.getValue()); }
From source file:me.tfeng.play.avro.JsonIpcController.java
@BodyParser.Of(BodyParser.Raw.class) public static Promise<Result> post(String message, String protocol) throws Throwable { String contentTypeHeader = request().getHeader("content-type"); ContentType contentType = ContentType.parse(contentTypeHeader); if (!CONTENT_TYPE.equals(contentType.getMimeType())) { throw new RuntimeException( "Unable to handle content-type " + contentType + "; " + CONTENT_TYPE + " is expected"); }//from w w w . jav a2 s . c o m AvroPlugin plugin = AvroPlugin.getInstance(); Class<?> protocolClass = Play.application().classloader().loadClass(protocol); Object implementation = plugin.getProtocolImplementations().get(protocolClass); Protocol avroProtocol = AvroHelper.getProtocol(protocolClass); Message avroMessage = avroProtocol.getMessages().get(message); byte[] bytes = request().body().asRaw().asBytes(); SpecificResponder responder = new SpecificResponder(protocolClass, implementation); Object request = getRequest(responder, avroMessage, bytes); if (AvroHelper.isAvroClient(protocolClass)) { Promise<?> promise = (Promise<?>) responder.respond(avroMessage, request); return promise.<Result>map(result -> Results.ok(AvroHelper.toJson(avroMessage.getResponse(), result))) .recover(e -> { try { LOG.warn("Exception thrown while processing request; returning bad request", e); return Results.badRequest(AvroHelper.toJson(avroMessage.getErrors(), e)); } catch (Exception e2) { throw e; } }); } else { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); return Promise.promise(() -> { Authentication currentAuthentication = SecurityContextHolder.getContext().getAuthentication(); SecurityContextHolder.getContext().setAuthentication(authentication); try { Object result = responder.respond(avroMessage, request); return Results.ok(AvroHelper.toJson(avroMessage.getResponse(), result)); } catch (Exception e) { try { LOG.warn("Exception thrown while processing request; returning bad request", e); return Results.badRequest(AvroHelper.toJson(avroMessage.getErrors(), e)); } catch (Exception e2) { throw e; } } finally { SecurityContextHolder.getContext().setAuthentication(currentAuthentication); } }, AvroPlugin.getInstance().getExecutionContext()); } }
From source file:org.xwiki.test.escaping.framework.URLContent.java
public URLContent(String typeHeader, byte[] content) { this.type = typeHeader != null ? ContentType.parse(typeHeader) : null; this.content = content; }
From source file:io.cloudslang.content.httpclient.build.ContentTypeBuilder.java
public ContentType buildContentType() { ContentType parsedContentType = null; if (StringUtils.isNotBlank(contentType)) { try {/*from w w w . j a v a2s . c om*/ parsedContentType = ContentType.parse(contentType); } catch (ParseException | UnsupportedCharsetException e) { throw new IllegalArgumentException( "Could not parse input '" + HttpClientInputs.CONTENT_TYPE + "'. " + e.getMessage(), e); } if (!StringUtils.isEmpty(requestCharacterSet)) { try { parsedContentType = parsedContentType.withCharset(requestCharacterSet); } catch (UnsupportedCharsetException e) { throw new IllegalArgumentException("Could not parse input '" + HttpClientInputs.REQUEST_CHARACTER_SET + "'. " + e.getMessage(), e); } } } return parsedContentType; }
From source file:groovyx.net.http.ApacheEncoders.java
/** * Encodes multipart/form-data where the body content must be an instance of the {@link MultipartContent} class. Individual parts will be * encoded using the encoders available to the {@link ChainedHttpConfig} object. * * @param config the chained configuration object * @param ts the server adapter//from w w w . j a v a 2 s. c o m */ public static void multipart(final ChainedHttpConfig config, final ToServer ts) { try { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); if (!(body instanceof MultipartContent)) { throw new IllegalArgumentException("Multipart body content must be MultipartContent."); } final String contentType = request.actualContentType(); if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) { throw new IllegalArgumentException("Multipart body content must be multipart/form-data."); } final String boundary = randomString(10); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().setBoundary(boundary); final String boundaryContentType = "multipart/form-data; boundary=" + boundary; entityBuilder.setContentType(ContentType.parse(boundaryContentType)); for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { if (mpe.getFileName() == null) { entityBuilder.addTextBody(mpe.getFieldName(), (String) mpe.getContent()); } else { final byte[] encodedBytes = EmbeddedEncoder.encode(config, mpe.getContentType(), mpe.getContent()); entityBuilder.addBinaryBody(mpe.getFieldName(), encodedBytes, parse(mpe.getContentType()), mpe.getFileName()); } } request.setContentType(boundaryContentType); ts.toServer(entityBuilder.build().getContent()); } catch (IOException ioe) { ioe.printStackTrace(); } }
From source file:com.smartling.cms.gateway.client.upload.FileUpload.java
public void setContentType(String mimeType, String encoding) { Validate.notEmpty(mimeType);/*from ww w. j av a 2 s . c o m*/ contentType = ContentType.parse(mimeType); if (encoding != null) { contentType = contentType.withCharset(encoding); } }
From source file:org.apache.camel.component.olingo2.api.impl.AbstractFutureCallback.java
public static HttpStatusCodes checkStatus(HttpResponse response) throws ODataApplicationException { final StatusLine statusLine = response.getStatusLine(); HttpStatusCodes httpStatusCode = HttpStatusCodes.fromStatusCode(statusLine.getStatusCode()); if (400 <= httpStatusCode.getStatusCode() && httpStatusCode.getStatusCode() <= 599) { if (response.getEntity() != null) { try { final ContentType responseContentType = ContentType .parse(response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue()); final String mimeType = responseContentType.getMimeType(); if (ODATA_MIME_TYPE.matcher(mimeType).matches()) { final ODataErrorContext errorContext = EntityProvider.readErrorDocument( response.getEntity().getContent(), responseContentType.toString()); throw new ODataApplicationException(errorContext.getMessage(), errorContext.getLocale(), httpStatusCode, errorContext.getErrorCode(), errorContext.getException()); }//w ww .jav a 2s.c om } catch (EntityProviderException e) { throw new ODataApplicationException(e.getMessage(), response.getLocale(), httpStatusCode, e); } catch (IOException e) { throw new ODataApplicationException(e.getMessage(), response.getLocale(), httpStatusCode, e); } } throw new ODataApplicationException(statusLine.getReasonPhrase(), response.getLocale(), httpStatusCode); } return httpStatusCode; }
From source file:org.openscore.content.httpclient.build.ContentTypeBuilder.java
public ContentType buildContentType() { String contentType = this.contentType; String requestCharacterSet = this.requestCharacterSet; ContentType parsedContentType;/* w w w . ja v a2 s . c o m*/ if (StringUtils.isEmpty(contentType)) { parsedContentType = defaultContentType; } else { try { parsedContentType = ContentType.parse(contentType); } catch (ParseException | UnsupportedCharsetException e) { throw new IllegalArgumentException( "Could not parse input '" + HttpClientInputs.CONTENT_TYPE + "'. " + e.getMessage(), e); } } //do not override contentType provide by user if (!StringUtils.isEmpty(requestCharacterSet)) { try { parsedContentType = parsedContentType.withCharset(requestCharacterSet); } catch (UnsupportedCharsetException e) { throw new IllegalArgumentException( "Could not parse input '" + HttpClientInputs.REQUEST_CHARACTER_SET + "'. " + e.getMessage(), e); } } return parsedContentType; }