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.fcrepo.auth.webac.WebACFilter.java

private boolean isSparqlUpdate(final HttpServletRequest request) {
    return request.getMethod().equals("PATCH")
            && contentTypeSPARQLUpdate.equalsIgnoreCase(request.getContentType());
}

From source file:com.streamsets.pipeline.lib.sdcipc.SdcIpcRequestFragmenter.java

@Override
public boolean validate(HttpServletRequest req, HttpServletResponse res) throws IOException {
    boolean valid;
    if (!APPLICATION_BINARY.equalsIgnoreCase(req.getContentType())) {
        res.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
                "Unsupported content type: " + req.getContentType());
        valid = false;/*from www  . j a  va  2  s.c o  m*/
    } else if (!"true".equalsIgnoreCase(req.getHeader(X_SDC_JSON1_FRAGMENTABLE_HEADER))) {
        res.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
                "Missing '" + X_SDC_JSON1_FRAGMENTABLE_HEADER + "' header");
        valid = false;
    } else {
        valid = true;
    }
    return valid;
}

From source file:com.eternity.common.communication.servlet.SyncJSONDispatch.java

protected void doRequest(HttpServletRequest req, HttpServletResponse resp, InputStream data)
        throws ServletException, IOException {
    String contentType = req.getContentType();
    String charset = req.getCharacterEncoding();
    String acceptsHeader = req.getHeader("Accept");
    Encoder encoder = null;//ww w. j a  v a  2s .  c  o  m
    Decoder decoder = ProtocolHandlers.getHandlers().getDecoder(contentType);

    if (charset == null) {
        charset = "UTF-8";
    }

    if (decoder == null) {
        resp.setStatus(400);
        PrintWriter p = resp.getWriter();
        p.write("Unacceptable Content-Type!");
        return;
    }

    if (acceptsHeader != null) {
        String accepts[] = acceptsHeader.split(",");

        for (String accept : accepts) {
            encoder = ProtocolHandlers.getHandlers().getEncoder(accept.trim());
            if (encoder != null)
                break;
        }
    } else {
        encoder = ProtocolHandlers.getHandlers().getEncoder(contentType);
    }

    if (encoder == null) {
        resp.setStatus(400);
        PrintWriter p = resp.getWriter();
        p.write("Unacceptable or missing ACCEPT header!");
        return;
    }

    String jsonMessage = req.getParameter(Parameter.jsonMessage.toString());
    Message message = (Message) decoder.decode(ByteBuffer.wrap(jsonMessage.getBytes(charset)), Message.class);
    message.encoding = contentType;
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    if (data != null)
        IOUtils.copy(data, bytes);
    message.body = ByteBuffer.wrap(bytes.toByteArray());
    Object result = MessageConsumer.dispatchMessage(message, null, hostName);
    resp.getOutputStream().write(encoder.encode(result).array());
}

From source file:org.wahlzeit.servlets.MainServlet.java

/**
 *
 *//*from w ww  . j ava2  s  .  c om*/
protected Map getRequestArgs(HttpServletRequest request, UserSession us) throws IOException, ServletException {
    String contentType = request.getContentType();
    if ((contentType != null) && contentType.startsWith("multipart/form-data")) {
        return getMultiPartRequestArgs(request, us);
    } else {
        return request.getParameterMap();
    }
}

From source file:org.deegree.enterprise.servlet.ProxyServlet.java

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
    String url = req.getParameter("URL");
    try {//from w w w .  j a v a2 s  .c  o m
        HttpMethod result = HttpUtils.performHttpPost(url, req.getInputStream(), 0, null, null,
                req.getContentType(), req.getCharacterEncoding(), null);
        InputStream in = result.getResponseBodyAsStream();
        IOUtils.getInstance().copyStreams(in, resp.getOutputStream());
        in.close();
    } catch (HttpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.eternity.common.communication.servlet.AsyncDispatch.java

protected void doRequest(HttpServletRequest req, HttpServletResponse resp, InputStream data)
        throws ServletException, IOException {
    String contentType = req.getContentType();
    String charset = req.getCharacterEncoding();
    String acceptsHeader = req.getHeader("Accept");
    Encoder encoder = null;/*from  w w w . j a v  a  2s  .  c o  m*/
    Decoder decoder = ProtocolHandlers.getHandlers().getDecoder(contentType);

    if (charset == null) {
        charset = "UTF-8";
    }

    if (decoder == null) {
        resp.setStatus(400);
        PrintWriter p = resp.getWriter();
        p.write("Unacceptable Content-Type!");
        return;
    }

    if (acceptsHeader != null) {
        String accepts[] = acceptsHeader.split(",");

        for (String accept : accepts) {
            encoder = ProtocolHandlers.getHandlers().getEncoder(accept.trim());
            if (encoder != null)
                break;
        }
    } else {
        encoder = ProtocolHandlers.getHandlers().getEncoder(contentType);
    }

    if (encoder == null) {
        resp.setStatus(400);
        PrintWriter p = resp.getWriter();
        p.write("Unacceptable or missing ACCEPT header!");
        return;
    }

    String jsonMessage = req.getParameter(Parameter.jsonMessage.toString());
    Message message = (Message) decoder.decode(ByteBuffer.wrap(jsonMessage.getBytes(charset)), Message.class);
    message.encoding = contentType;
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    if (data != null)
        IOUtils.copy(data, bytes);
    message.body = ByteBuffer.wrap(bytes.toByteArray());
    MessageConsumer.dispatchAsyncMessage(message, null, hostName);
    Response result = new Response();
    result.setStatus(200);
    resp.getOutputStream().write(encoder.encode(result).array());
}

From source file:org.polymap.core.workbench.dnd.DndUploadServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    log.debug("POST-Request: " + request.getContentType() + ", length: " + request.getContentLength());
    OutputStream fout = null;/*  w w w  .j  a  v a2 s .  c  o  m*/
    try {
        // copy content into temp file
        final String contentType = request.getContentType();
        File dir = getUploadTempDir(request.getSession());
        final String filename = request.getHeader("X-Filename");
        assert filename != null;
        final File f = new File(dir, filename + "_" + System.currentTimeMillis());
        fout = new FileOutputStream(f);
        IOUtils.copy(request.getInputStream(), fout);
        fout.close();
        log.debug("    uploaded: " + f.getAbsolutePath());

        // create event
        final DesktopDropEvent event = new FileDropEvent() {
            public InputStream getInputStream() throws IOException {
                return new BufferedInputStream(new FileInputStream(f));
            }

            public String getContentType() {
                return contentType != null ? contentType : "";
            }

            public String getFileName() {
                return filename;
            }
        };
        synchronized (uploads) {
            uploads.put(request.getSession().getId(), event);
        }
    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);
        //throw new ServletException( e.getLocalizedMessage() );
        response.setStatus(409);
        response.getWriter().append(e.getLocalizedMessage()).flush();
        //sendError( 409, e.getLocalizedMessage() );
    } finally {
        IOUtils.closeQuietly(fout);
    }
}

From source file:org.mule.transport.servlet.ServletMuleMessageFactory.java

protected void setupContentType(HttpServletRequest request, DefaultMuleMessage message) {
    String contentType = request.getContentType();

    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(ServletConnector.CONTENT_TYPE_PROPERTY_KEY, contentType);

    message.addInboundProperties(properties);
}

From source file:org.mule.transport.servlet.ServletMuleMessageFactory.java

protected void setupEncoding(HttpServletRequest request, MuleMessage message) {
    String contentType = request.getContentType();
    if (contentType != null) {
        int index = contentType.indexOf("charset");
        if (index > -1) {
            int semicolonIndex = contentType.lastIndexOf(";");
            String encoding;//from   w w w . j av a 2s  . co m
            if (semicolonIndex > index) {
                encoding = contentType.substring(index + 8, semicolonIndex);
            } else {
                encoding = contentType.substring(index + 8);
            }
            // some stacks send quotes around the charset encoding
            message.setEncoding(encoding.replaceAll("\"", "").trim());
        }
    }
}

From source file:org.xmlactions.web.conceal.HtmlRequestMapper.java

/**
 * Checks if the HttpRequest is a post containing multipart upload data
 * //from   w  w  w  .  j a  v a 2s .  c  o  m
 * @param request
 *            the HttpServletRequest
 * @return true if contains upload data else false
 */
public boolean is_multipart_form_data(HttpServletRequest request) {

    try {
        if (StringUtils.isEmpty(request.getContentType())) {
            return (false);
        }
        if ("multipart/form-data".equalsIgnoreCase(
                request.getContentType().substring(0, "multipart/form-data".length())) == true) {
            return (true);
        }
        if ("application/octet-stream".equalsIgnoreCase(
                request.getContentType().substring(0, "application/octet-stream".length())) == true) {
            return (true);
        }
    } catch (Exception ex) {
        log.info(ex.getMessage());
    }
    return (false);
}