Example usage for javax.servlet.http HttpServletRequest getContentType

List of usage examples for javax.servlet.http HttpServletRequest getContentType

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getContentType.

Prototype

public String getContentType();

Source Link

Document

Returns the MIME type of the body of the request, or null if the type is not known.

Usage

From source file:org.flowable.common.rest.multipart.PutAwareStandardServletMultiPartResolver.java

/**
 * Utility method that determines whether the request contains multipart content.
 *
 * @param request/*from   www  .j  a v a  2 s . com*/
 *     The servlet request to be evaluated. Must be non-null.
 * @return <code>true</code> if the request is multipart; {@code false} otherwise.
 * @see org.apache.commons.fileupload.servlet.ServletFileUpload#isMultipartContent(HttpServletRequest)
 */
public static final boolean isMultipartContent(HttpServletRequest request) {
    final String method = request.getMethod().toLowerCase();
    if (!method.equalsIgnoreCase("post") && !method.equalsIgnoreCase("put")) {
        return false;
    }

    String contentType = request.getContentType();
    return StringUtils.startsWithIgnoreCase(contentType, "multipart/");
}

From source file:com.liusoft.dlog4j.util.RequestUtils.java

public static boolean isMultipart(HttpServletRequest req) {
    return ((req.getContentType() != null) && (req.getContentType().toLowerCase().startsWith("multipart")));
}

From source file:de.xwic.appkit.core.remote.server.ParameterProvider.java

/**
 * @param request//from  w w  w . ja va  2  s .c o  m
 * @return
 */
private static final boolean isMultipart(final HttpServletRequest request) {
    if (!"POST".equalsIgnoreCase(request.getMethod())) {
        return false;
    }
    String contentType = request.getContentType();
    if (contentType == null) {
        return false;
    }
    return contentType.toLowerCase().startsWith("multipart/");
}

From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtil.java

/**
 * Checks if the request is a multipart request
 *
 * @param request the HTTP servlet request
 * @return true if request is a Multipart request (file upload)
 *//*from w w w.  j a  v a  2  s .  c om*/
public static boolean isMultipart(HttpServletRequest request) {
    if (!"post".equals(request.getMethod().toLowerCase())) {
        return false;
    }
    String contentType = request.getContentType();
    return contentType != null && contentType.toLowerCase().startsWith("multipart/");
}

From source file:org.wso2.carbon.bpmn.rest.common.utils.Utils.java

/**
 * This function will check whether the HTTP Request is with Content-Type with application/json
 * @param httpServletRequest : httpServeletRequest
 * @return : true if the Content-Type is application/json, false otherwise
 *///from www  .j  a  v a2s.  c om
public static boolean isApplicationJsonRequest(HttpServletRequest httpServletRequest) {
    return httpServletRequest.getContentType().trim().startsWith(MediaType.APPLICATION_JSON);
}

From source file:org.wso2.carbon.bpmn.rest.common.utils.Utils.java

/**
 * This function will check whether the HTTP Request is with Content-Type with application/xml
 * @param httpServletRequest : httpServeletRequest
 * @return : true if the Content-Type is application/xml, false otherwise
 *//*  w ww .j  a  va2s . co  m*/
public static boolean isApplicationXmlRequest(HttpServletRequest httpServletRequest) {
    return httpServletRequest.getContentType().trim().startsWith(MediaType.APPLICATION_XML);
}

From source file:com.openmeap.util.ServletUtils.java

/**
 * //  w ww.  j a  v  a  2 s  .  c om
 * @param settings
 * @param request
 * @return
 */
static final public Map<Object, Object> cloneParameterMap(Integer maxFileUploadSize, String fileStoragePrefix,
        HttpServletRequest request) {

    // make a tidy package of parameters for the DocumentProcessor
    Map<Object, Object> map = new HashMap<Object, Object>();

    // check for file uploads
    String contentType = request.getContentType();

    if (contentType != null && contentType.startsWith(FormConstants.ENCTYPE_MULTIPART_FORMDATA)) {

        try {
            ServletUtils.handleFileUploads(maxFileUploadSize, fileStoragePrefix, request, map);
        } catch (FileUploadException fue) {
            // TODO: switch over to an error page and pass an event such that the exception is intelligently communicated
            throw new RuntimeException(fue);
        }
    } else {

        @SuppressWarnings(value = { "unchecked" })
        Map<String, String[]> params = (Map<String, String[]>) request.getParameterMap();
        for (Map.Entry<String, String[]> ent : params.entrySet()) {
            if (ent.getValue() != null) {

                String key = ent.getKey();
                String[] values = new String[ent.getValue().length];
                int i = 0;
                for (String val : ent.getValue()) {
                    values[i++] = val;
                }
                map.put(key, values);
            }
        }
    }

    return map;
}

From source file:org.apache.shindig.social.core.oauth.OAuthAuthenticationHandler.java

public static void verifyBodyHash(HttpServletRequest request, String oauthBodyHash)
        throws InvalidAuthenticationException {
    // we are doing body hash signing which is not permitted for form-encoded data
    if (request.getContentType() != null && request.getContentType().contains(OAuth.FORM_ENCODED)) {
        throw new AuthenticationHandler.InvalidAuthenticationException(
                "Cannot use oauth_body_hash with a Content-Type of application/x-www-form-urlencoded", null);
    } else {/*from  w  w w . j  av  a2s .  c  o  m*/
        try {
            byte[] rawBody = readBody(request);
            byte[] received = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(oauthBodyHash));
            byte[] expected = DigestUtils.sha(rawBody);
            if (!Arrays.equals(received, expected)) {
                throw new AuthenticationHandler.InvalidAuthenticationException(
                        "oauth_body_hash failed verification", null);
            }
        } catch (IOException ioe) {
            throw new AuthenticationHandler.InvalidAuthenticationException(
                    "Unable to read content body for oauth_body_hash verification", null);
        }
    }
}

From source file:org.wso2.carbon.bam.service.data.publisher.publish.ServiceAgentUtil.java

public static void extractInfoFromHttpHeaders(EventData eventData, Object requestProperty) {

    if (requestProperty instanceof HttpServletRequest) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) requestProperty;
        eventData.setRequestURL(httpServletRequest.getRequestURL().toString());
        eventData.setRemoteAddress(PublisherUtil.getHostAddress());
        eventData.setContentType(httpServletRequest.getContentType());
        eventData.setUserAgent(httpServletRequest.getHeader(BAMDataPublisherConstants.HTTP_HEADER_USER_AGENT));
        //            eventData.setHost(httpServletRequest.getHeader(
        //                    BAMDataPublisherConstants.HTTP_HEADER_HOST));
        eventData.setReferer(httpServletRequest.getHeader(BAMDataPublisherConstants.HTTP_HEADER_REFERER));
    }/*from  ww w. ja v a 2s .co m*/

}

From source file:org.acoustid.server.util.ParameterMap.java

public static ParameterMap parseRequest(HttpServletRequest request) throws IOException {
    String contentEncoding = request.getHeader("Content-Encoding");
    if (contentEncoding != null) {
        contentEncoding = contentEncoding.toLowerCase();
    }/* w  w  w  . j  a v a  2 s.  c om*/
    String contentType = request.getContentType();
    Map<String, String[]> map;
    if ("gzip".equals(contentEncoding) && "application/x-www-form-urlencoded".equals(contentType)) {
        InputStream inputStream = new GZIPInputStream(request.getInputStream());
        InputStreamEntity entity = new InputStreamEntity(inputStream, -1);
        entity.setContentType(contentType);
        map = new HashMap<String, String[]>();
        for (NameValuePair param : URLEncodedUtils.parse(entity)) {
            String name = param.getName();
            String value = param.getValue();
            String[] values = map.get(name);
            if (values == null) {
                values = new String[] { value };
            } else {
                values = (String[]) ArrayUtils.add(values, value);
            }
            map.put(name, values);
        }
    } else {
        map = request.getParameterMap();
    }
    return new ParameterMap(map);
}