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:com.du.order.dist.log.LoggingFilter.java

private boolean isBinaryContent(final HttpServletRequest request) {
    if (request.getContentType() == null) {
        return false;
    }//ww w  . j ava2  s  .  com
    return request.getContentType().startsWith("image") || request.getContentType().startsWith("video")
            || request.getContentType().startsWith("audio");
}

From source file:myPackage.UploadBean.java

public boolean doFilePost(HttpServletRequest request, ServletContext context) {
    if (request.getContentType() == null)
        return false;

    if (!request.getContentType().startsWith("multipart/formdata"))
        return false;

    String path = context.getRealPath(getDiretorio());

    try {//w  ww .j av a 2s . c  o m
        List list = sfu.parseRequest(request);
        Iterator iterator = list.iterator();

        while (iterator.hasNext()) {
            FileItem item = (FileItem) iterator.next();

            if (!item.isFormField()) {
                filename = item.getName();

                if ((filename != null) && (!filename.equals(""))) {
                    filename = (new File(filename)).getName();
                    item.write(new File(path + File.separator + filename));
                }
            }
        }
    }

    catch (FileUploadException e) {
        e.printStackTrace();
    }

    catch (Exception e) {
        e.printStackTrace();
    }

    return true;
}

From source file:nl.nn.adapterframework.pipes.IfMultipart.java

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    String forward;/*w  w  w.j  a v  a2  s .  c o m*/
    PipeForward pipeForward = null;

    if (!(input instanceof HttpServletRequest)) {
        throw new PipeRunException(this, getLogPrefix(null) + "expected HttpServletRequest as input, got ["
                + ClassUtils.nameOf(input) + "]");
    }

    HttpServletRequest request = (HttpServletRequest) input;
    String contentType = request.getContentType();
    if (StringUtils.isNotEmpty(contentType) && contentType.startsWith("multipart")) {
        forward = thenForwardName;
    } else {
        forward = elseForwardName;
    }

    log.debug(getLogPrefix(session) + "determined forward [" + forward + "]");

    pipeForward = findForward(forward);

    if (pipeForward == null) {
        throw new PipeRunException(this,
                getLogPrefix(null) + "cannot find forward or pipe named [" + forward + "]");
    }
    log.debug(getLogPrefix(session) + "resolved forward [" + forward + "] to path [" + pipeForward.getPath()
            + "]");
    return new PipeRunResult(pipeForward, input);
}

From source file:PostServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (!"text/xml".equals(request.getContentType())) {
        response.getWriter().println("Please post as text/xml.");
    } else {/*  w ww .j  av a2  s . c  o m*/
        try {
            Document doc = builder.build(request.getReader());
            StringBuffer buff = new StringBuffer();
            buff.append("You searched for name '" + nameXPath.valueOf(doc) + "'");
            String year = yearXPath.valueOf(doc);
            if (!"notselected".equals(year)) {
                buff.append(" and year '" + year + "'");
            }
            buff.append(".");
            response.getWriter().print(buff.toString());
        } catch (JDOMException e) {
            response.getWriter().print("Error getting search terms: " + e.getMessage());
        }
    }
}

From source file:platform.filter.HttpPutDeleteFormContentFilter.java

private boolean isFormContentType(HttpServletRequest request) {
    String contentType = request.getContentType();
    if (contentType != null) {
        try {//from  ww  w. ja  v  a 2 s.  c  om
            MediaType mediaType = MediaType.parseMediaType(contentType);
            return (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType));
        } catch (IllegalArgumentException ex) {
            return false;
        }
    } else {
        return false;
    }
}

From source file:be.solidx.hot.test.nio.http.EchoPOSTServlet.java

protected void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType(req.getContentType());
    resp.setContentLength(req.getContentLength());
    String body = IOUtils.toString(req.getInputStream());
    System.out.println(body);/*from  w w w. j av a  2  s  .c  o  m*/
    resp.getWriter().write(body);
}

From source file:com.thoughtworks.go.server.newsecurity.filters.helpers.ServerUnavailabilityResponse.java

private boolean requestIsOfType(String type, HttpServletRequest request) {
    String header = request.getHeader("Accept");
    String contentType = request.getContentType();
    String url = request.getRequestURI();
    return header != null && header.contains(type) || url != null && url.endsWith(type)
            || contentType != null && contentType.contains(type);
}

From source file:org.apache.shindig.protocol.multipart.DefaultMultipartFormParser.java

public boolean isMultipartContent(HttpServletRequest request) {
    if (!"POST".equals(request.getMethod())) {
        return false;
    }//from   www  . j  a va  2  s  . c  o m
    String contentType = request.getContentType();
    if (contentType == null) {
        return false;
    }
    return contentType.toLowerCase().startsWith(MULTIPART);
}

From source file:ch.sportchef.business.event.boundary.EventImageResource.java

@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadImage(@Context HttpServletRequest request) throws IOException, ServletException {
    final String contentType = request.getContentType();
    final byte[] boundary = contentType.substring(contentType.indexOf("boundary=") + 9).getBytes();

    try (final BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream(), 8192)) {
        final MultipartStream multipartStream = new MultipartStream(inputStream, boundary, 8192, null);
        boolean nextPart = multipartStream.skipPreamble();
        while (nextPart) {
            multipartStream.readHeaders(); // don't remove, strips headers off
            try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(8192)) {
                multipartStream.readBodyData(outputStream);
                nextPart = multipartStream.readBoundary();
                final byte[] image = outputStream.toByteArray();
                eventImageService.uploadImage(eventId, image);
                return Response.ok().build();
            }//  w ww  .j  av a2 s  .c  o m
        }
    }

    return Response.status(BAD_REQUEST).build();
}

From source file:org.tangram.components.servlet.ServletRequestParameterAccess.java

/**
 * Weak visibility to avoid direct instanciation.
 *//* w  ww  .  j  a va2s  .com*/
@SuppressWarnings("unchecked")
ServletRequestParameterAccess(HttpServletRequest request, long uploadFileMaxSize) {
    final String reqContentType = request.getContentType();
    LOG.debug("() uploadFileMaxSize={} request.contentType={}", uploadFileMaxSize, reqContentType);
    if (StringUtils.isNotBlank(reqContentType) && reqContentType.startsWith("multipart/form-data")) {
        ServletFileUpload upload = new ServletFileUpload();
        upload.setFileSizeMax(uploadFileMaxSize);
        try {
            for (FileItemIterator itemIterator = upload.getItemIterator(request); itemIterator.hasNext();) {
                FileItemStream item = itemIterator.next();
                String fieldName = item.getFieldName();
                InputStream stream = item.openStream();
                if (item.isFormField()) {
                    String[] value = parameterMap.get(item.getFieldName());
                    int i = 0;
                    if (value == null) {
                        value = new String[1];
                    } else {
                        String[] newValue = new String[value.length + 1];
                        System.arraycopy(value, 0, newValue, 0, value.length);
                        i = value.length;
                        value = newValue;
                    } // if
                    value[i] = Streams.asString(stream, "UTF-8");
                    LOG.debug("() request parameter {}='{}'", fieldName, value[0]);
                    parameterMap.put(item.getFieldName(), value);
                } else {
                    try {
                        LOG.debug("() item {} :{}", item.getName(), item.getContentType());
                        final byte[] bytes = IOUtils.toByteArray(stream);
                        if (bytes.length > 0) {
                            originalNames.put(fieldName, item.getName());
                            blobs.put(fieldName, bytes);
                        } // if
                    } catch (IOException ex) {
                        LOG.error("()", ex);
                        if (ex.getCause() instanceof FileUploadBase.FileSizeLimitExceededException) {
                            throw new RuntimeException(ex.getCause().getMessage()); // NOPMD we want to lose parts of our stack trace!
                        } // if
                    } // try/catch
                } // if
            } // for
        } catch (FileUploadException | IOException ex) {
            LOG.error("()", ex);
        } // try/catch
    } else {
        parameterMap = request.getParameterMap();
    } // if
}