Example usage for org.apache.http.entity ContentType parse

List of usage examples for org.apache.http.entity ContentType parse

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType parse.

Prototype

public static ContentType parse(String str) throws ParseException, UnsupportedCharsetException 

Source Link

Usage

From source file:com.mirth.connect.server.userutil.HTTPUtil.java

private static ContentType getContentType(String contentType) {
    try {//from   w  ww .jav a2s.c  om
        return ContentType.parse(contentType);
    } catch (RuntimeException e) {
        return ContentType.TEXT_PLAIN;
    }
}

From source file:com.github.matthesrieke.jprox.JProxViaParameterServlet.java

private HttpUriRequest prepareRequest(HttpServletRequest req, String target) throws IOException {
    String method = req.getMethod();
    if (method.equals(HttpGet.METHOD_NAME)) {
        return new HttpGet(target);
    } else if (method.equals(HttpPost.METHOD_NAME)) {
        HttpPost post = new HttpPost(target);
        post.setEntity(new ByteArrayEntity(readInputStream(req.getInputStream(), req.getContentLength()),
                ContentType.parse(req.getContentType())));
        return post;
    }//from  w ww.  ja v a  2 s .co  m
    throw new UnsupportedOperationException("Only GET and POST are supported by this proxy.");
}

From source file:com.github.matthesrieke.simplebroker.SimpleBrokerServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (verifyRemoteHost(req.getRemoteHost())) {
        final String content;
        final ContentType type;
        final String remoteHost;
        try {/*from   w w w .j a va2s  . c  o m*/
            content = readContent(req);
            type = ContentType.parse(req.getContentType());
            remoteHost = req.getRemoteHost();
        } catch (IOException e) {
            logger.warn(e.getMessage());
            return;
        }

        this.executor.submit(new Runnable() {

            public void run() {
                for (Consumer c : consumers)
                    try {
                        c.consume(content, type, remoteHost);
                    } catch (RuntimeException e) {
                        logger.warn(e.getMessage());
                    } catch (IOException e) {
                        logger.warn(e.getMessage());
                    }
            }
        });
    } else {
        logger.info("Host {} is not whitelisted. Ignoring request.", req.getRemoteHost());
    }

    resp.setStatus(HttpStatus.SC_NO_CONTENT);
}

From source file:net.ychron.unirestinst.request.body.MultipartBody.java

public MultipartBody field(String name, Object value, boolean file, String contentType) {
    List<Object> list = parameters.get(name);
    if (list == null)
        list = new LinkedList<Object>();
    list.add(value);/*from  w w w .  j  av a  2 s.  co  m*/
    parameters.put(name, list);

    ContentType type = null;
    if (contentType != null && contentType.length() > 0) {
        type = ContentType.parse(contentType);
    } else if (file) {
        type = ContentType.APPLICATION_OCTET_STREAM;
    } else {
        type = ContentType.APPLICATION_FORM_URLENCODED.withCharset(UTF_8);
    }
    contentTypes.put(name, type);

    if (!hasFile && file) {
        hasFile = true;
    }

    return this;
}

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 filename filename that is being probed for content type
 * @param defaultContentType content type to default to
 * @return content type object//  w  w w  .  j  a v a  2  s.  c om
 */
public static ContentType findOrDefaultContentType(final MantaHttpHeaders headers, final String filename,
        final ContentType defaultContentType) {
    final String headerContentType;

    if (headers != null) {
        headerContentType = headers.getContentType();
    } else {
        headerContentType = null;
    }

    String type = ObjectUtils.firstNonNull(
            // Use explicitly set headers if available
            headerContentType,
            // Detect based on filename
            URLConnection.guessContentTypeFromName(filename));

    if (type == null) {
        return defaultContentType;
    }

    return ContentType.parse(type);
}

From source file:com.k42b3.aletheia.protocol.http.Response.java

private Charset detectCharset() {
    // try to read charset from the header
    String contentType = this.getHeader("Content-Type");

    if (contentType != null) {
        try {/*from   ww w. j av a 2 s  .  co  m*/
            Charset charset = ContentType.parse(contentType).getCharset();

            if (charset != null) {
                return charset;
            }
        } catch (ParseException e) {
        } catch (UnsupportedCharsetException e) {
        }

        // if the content type is text/* use default charset
        if (contentType.indexOf("text/") != -1) {
            return Charset.forName("UTF-8");
        }
    }

    // @todo try to parse meta tag therefor we need to convert the content
    // byte[] into an string 
    // <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

    return null;
}

From source file:org.craftercms.studio.impl.v1.web.http.MultiReadHttpServletRequestWrapper.java

private Iterable<NameValuePair> decodeParams(String body) {
    List<NameValuePair> params = new ArrayList<>(URLEncodedUtils.parse(body, UTF8_CHARSET));
    try {//  ww  w.  jav a 2s. com
        String cts = getContentType();
        if (cts != null) {
            ContentType ct = ContentType.parse(cts);
            if (ct.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
                List<NameValuePair> postParams = URLEncodedUtils.parse(IOUtils.toString(getReader()),
                        UTF8_CHARSET);
                CollectionUtils.addAll(params, postParams);
            }
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    return params;
}

From source file:ro.startx.ups.server.Sender.java

/**
 * Asynchronously send a message with a context to be passed in the future result.
 *
 * @param message The message to send.//from ww w . j  a  v  a2 s . co  m
 * @param requestContext An opaque context to include the future result.
 * @return The future.
 */
public ListenableFuture<Result> send(final Message message, final Object requestContext) {
    return executor.getFutureWithRetry(new RetryCallable<ListenableFuture<Result>>() {
        @Override
        public ListenableFuture<Result> call(RetryContext context) throws Exception {
            SettableFuture<Result> future = SettableFuture.create();
            HttpPost request = new HttpPost(url);

            request.setEntity(new StringEntity(message.serialize(), ContentType.parse("application/json")));

            client.execute(request, new ResponseHandler(future, requestContext));

            return future;
        }
    });
}

From source file:org.whispersystems.gcm.server.Sender.java

/**
 * Asynchronously send a message with a context to be passed in the future result.
 *
 * @param message The message to send.//from  ww  w  .  j a  va 2s  .  co  m
 * @param requestContext An opaque context to include the future result.
 * @return The future.
 */
public ListenableFuture<Result> send(final Message message, final Object requestContext) {
    return executor.getFutureWithRetry(new RetryCallable<ListenableFuture<Result>>() {
        @Override
        public ListenableFuture<Result> call(RetryContext context) throws Exception {
            SettableFuture<Result> future = SettableFuture.create();
            HttpPost request = new HttpPost(url);

            request.setHeader("Authorization", authorizationHeader);
            request.setEntity(new StringEntity(message.serialize(), ContentType.parse("application/json")));

            client.execute(request, new ResponseHandler(future, requestContext));

            return future;
        }
    });
}