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.apache.sling.engine.impl.parameters.ParameterSupport.java

private static final boolean isWWWFormEncodedContent(HttpServletRequest request) {
    String contentType = request.getContentType();
    if (contentType == null) {
        return false;
    }/*w  ww .j  a v  a2s  .  co  m*/

    // This check assumes the content type ends after the WWW_FORM_URL_ENC
    // or continues with blank or semicolon. It will probably break if
    // the content type is some string extension of WWW_FORM_URL_ENC
    // such as "application/x-www-form-urlencoded-bla"
    if (contentType.toLowerCase(Locale.ENGLISH).startsWith(WWW_FORM_URL_ENC)) {
        return true;
    }

    return false;
}

From source file:org.apache.beehive.netui.pageflow.MultipartRequestUtils.java

/**
 * <p>// w w w  .  j av a  2s.co  m
 * Handle a multipart request.  The return value of this method will be <code>null</code>
 * if the following conditions are not satisfied:
 * <ol>
 * <li>request.getContentType() != null</li>
 * <li>content type is "multipart/form-data"</li>
 * <li>request method is "POST"</li>
 * </ol>
 * If these are satisfied, a Struts {@link MultipartRequestHandler} is created.  This object is used
 * to provide a mapping over both the regular {@link HttpServletRequest} parameters and the 
 * paramters that Struts creates to represent the file(s) that was uploaded.  If file(s) were 
 * uploaded, the {@link java.util.Map} returned has key / value pairs of these two structures:
 * <ul>
 * <li><code>String / String[]</code></li>
 * <li><code>String / {@link org.apache.commons.fileupload.FileItem}</code></li>
 * </ul>
 * <br/>
 * Invokers of this method should be aware that in this case, not all types returned from 
 * what looks like <code>request.getParameterValues(String key)</code> will be <code>String[]</code>.
 * </p>
 *
 * @param request the request object
 * @param bean the current action's associated {@link ActionForm}
 * @return <code>null</code> if the request is <i>not</i> multipart.  Otherwise, a {@link java.util.Map} 
 * is returned that contains the key / value pairs of the parameters in the request and the uploaded
 * files.
 * @throws ServletException if an error occurs loading this file.  These exception messages
 * are not internationalized as Struts does not internationalize them either.
 */
/* package */ static final Map handleMultipartRequest(HttpServletRequest request, ActionForm bean)
        throws ServletException {
    String contentType = request.getContentType();
    String method = request.getMethod();
    boolean isMultipart = false;

    Map multipartParameters = null;

    if (contentType != null && contentType.startsWith("multipart/form-data")
            && method.equalsIgnoreCase("POST")) {
        if (!InternalUtils.isMultipartHandlingEnabled(request)) {
            throw new ServletException("Received a multipart request, but multipart handling is not enabled.");
        }

        ActionServletWrapper servlet;

        if (bean != null) {
            servlet = bean.getServletWrapper();
        } else {
            ServletContext servletContext = InternalUtils.getServletContext(request);
            servlet = new ActionServletWrapper(InternalUtils.getActionServlet(servletContext));
        }

        // @struts: does this -- but we can't rely on the bean not being null.
        // if(bean instanceof ActionForm)
        //     servlet = bean.getServletWrapper();
        // else if ( ! ( bean instanceof ActionForm ) )
        //     throw new ServletException
        //         ("bean that's supposed to be populated from a multipart request is not of type " + 
        //          "\"org.apache.struts.action.ActionForm\", but type \"" + bean.getClass().getName() + "\"");

        MultipartRequestHandler multipartHandler = getCachedMultipartHandler(request);
        boolean preHandled = false;

        if (multipartHandler == null) {
            multipartHandler = getMultipartHandler(request);
        } else {
            preHandled = true;
        }

        if (bean != null) {
            bean.setMultipartRequestHandler(multipartHandler);
        }

        if (multipartHandler != null) {
            isMultipart = true;

            servlet.setServletFor(multipartHandler);
            multipartHandler.setMapping((ActionMapping) request.getAttribute(Globals.MAPPING_KEY));

            if (!preHandled) {
                multipartHandler.handleRequest(request);
            }

            Boolean maxLengthExceeded = (Boolean) request
                    .getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
            if (maxLengthExceeded != null && maxLengthExceeded.booleanValue()) {
                // bail from RequestUtils.processPopulate
                // @struts: semantics -- if this check fails, no values are updated into the form
                return null;
            }
            multipartParameters = MultipartRequestUtils.getAllParametersForMultipartRequest(request,
                    multipartHandler);

            // @struts: does this
            //names = Collections.enumeration(multipartParameters.keySet());
        }
    }

    if (!isMultipart) {
        // @struts: does this -- NetUI does not so that the ProcessPopulate method can be made faster; this way,
        // ProcessPopulate doesn't care whether this is a MultipartRequest or not; it can just talk to a Map
        // to get key / value pairs.
        //names = request.getParameterNames();
        return null;
    } else {
        ScopedRequest scopedRequest = ScopedServletUtils.unwrapRequest(request);

        if (scopedRequest != null) {
            multipartParameters = scopedRequest.filterParameterMap(multipartParameters);
        }
        return multipartParameters;
    }
}

From source file:org.deegree.services.config.actions.Upload.java

public static void upload(String path, HttpServletRequest req, HttpServletResponse resp) throws IOException {

    Pair<DeegreeWorkspace, String> p = getWorkspaceAndPath(path);

    resp.setContentType("text/plain");

    if (p.second == null) {
        IOUtils.write("No file name given.\n", resp.getOutputStream());
        return;//w ww.  j  a  v  a 2 s .  c  o m
    }

    boolean isZip = p.second.endsWith(".zip")
            || req.getContentType() != null && req.getContentType().equals("application/zip");

    ServletInputStream in = null;
    try {
        in = req.getInputStream();
        if (isZip) {
            // unzip a workspace
            String wsName = p.second.substring(0, p.second.length() - 4);
            String dirName = p.second.endsWith(".zip") ? wsName : p.second;
            File dir = new File(getWorkspaceRoot(), dirName);
            if (isWorkspace(dirName)) {
                IOUtils.write("Workspace " + wsName + " exists.\n", resp.getOutputStream());
                return;
            }
            unzip(in, dir);
            IOUtils.write("Workspace " + wsName + " uploaded.\n", resp.getOutputStream());
        } else {
            File dest = new File(p.first.getLocation(), p.second);
            if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) {
                IOUtils.write("Unable to create parent directory for upload.\n", resp.getOutputStream());
                return;
            }
            copyInputStreamToFile(in, dest);
            IOUtils.write(dest.getName() + " uploaded.\n", resp.getOutputStream());
        }
    } finally {
        closeQuietly(in);
    }
}

From source file:cltestgrid.Upload2.java

private static final boolean isMultipartContent(HttpServletRequest request) {
    if (!"post".equals(request.getMethod().toLowerCase())) {
        return false;
    }//from w  ww  . j av  a  2s  .  c  o m
    String contentType = request.getContentType();
    return contentType != null && contentType.toLowerCase().startsWith("multipart");
}

From source file:org.opendatakit.odktables.util.ServiceUtils.java

@SuppressWarnings("unused")
public static void examineRequest(ServletContext sc, HttpServletRequest req, HttpHeaders httpHeaders) {
    MultivaluedMap<String, String> headers = httpHeaders.getRequestHeaders();
    StringBuilder b = new StringBuilder();
    for (String headerName : headers.keySet()) {
        List<String> fieldValues = headers.get(headerName);
        for (String fieldValue : fieldValues) {
            b.append(headerName).append(": ").append(fieldValue).append("\n");
        }//from ww  w .j av  a 2 s . c  o m
    }
    String contentType = req.getContentType();
    String charEncoding = req.getCharacterEncoding();
    String headerSet = b.toString();
    Cookie[] cookies = req.getCookies();
    String method = req.getMethod();
    String ctxtPath = req.getContextPath();
    String pathInfo = req.getPathInfo();
    String query = req.getQueryString();
    boolean sessionId = req.isRequestedSessionIdValid();
}

From source file:org.codehaus.groovy.grails.web.binding.DataBindingUtils.java

public static MimeType resolveMimeType(Object bindingSource, MimeTypeResolver mimeTypeResolver) {
    final MimeType mimeType;
    if (mimeTypeResolver != null) {
        MimeType resolvedMimeType = mimeTypeResolver.resolveRequestMimeType();
        mimeType = resolvedMimeType != null ? resolvedMimeType : MimeType.ALL;
    } else if (bindingSource instanceof HttpServletRequest) {
        HttpServletRequest req = (HttpServletRequest) bindingSource;
        String contentType = req.getContentType();
        if (contentType != null) {
            mimeType = new MimeType(contentType);
        } else {//from   www .  j  av a2  s . co m
            mimeType = MimeType.ALL;
        }
    } else {
        mimeType = MimeType.ALL;
    }
    return mimeType;
}

From source file:com.jims.oauth2.common.utils.OAuthUtils.java

public static boolean isMultipart(HttpServletRequest request) {

    if (!"post".equals(request.getMethod().toLowerCase())) {
        return false;
    }//from w  w w.  j  a va  2  s . c o m
    String contentType = request.getContentType();
    if (contentType == null) {
        return false;
    }
    if (contentType.toLowerCase().startsWith(MULTIPART)) {
        return true;
    }
    return false;
}

From source file:org.alfresco.module.vti.web.actions.VtiSoapAction.java

/**
 * Returns the SOAP Action that was requested, or NULL if the request isn't a SOAP action.
 * This method handles any de-quoting that is required.
 *//* www . j  a  v a 2 s . c o  m*/
public static String getSOAPAction(HttpServletRequest request) {
    // Fetch, and de-quote if needed the action
    String soapAction = request.getHeader("SOAPAction");
    if (soapAction != null) {
        // SOAP 1.1, de-quote
        if (soapAction.startsWith("\"") && soapAction.endsWith("\"")) {
            soapAction = soapAction.substring(1, soapAction.length() - 1);
        }
    } else {
        // SOAP 1.2
        if (request.getContentType() != null) {
            Pattern p = Pattern.compile("action=\"(.*?)\"");
            Matcher m = p.matcher(request.getContentType());
            if (m.find()) {
                soapAction = m.group(1);
            }
        }
    }

    return soapAction;
}

From source file:org.synchronoss.cloud.nio.multipart.example.web.MultipartController.java

static MultipartContext getMultipartContext(final HttpServletRequest request) {
    String contentType = request.getContentType();
    int contentLength = request.getContentLength();
    String charEncoding = request.getCharacterEncoding();
    return new MultipartContext(contentType, contentLength, charEncoding);
}

From source file:org.synchronoss.cloud.nio.multipart.example.web.MultipartController.java

static void assertRequestIsMultipart(final HttpServletRequest request) {
    if (!MultipartUtils.isMultipart(request.getContentType())) {
        throw new IllegalStateException("Expected multipart request");
    }/*from w w  w . j a  v  a2 s.  c  om*/
}