Example usage for javax.activation MimeType match

List of usage examples for javax.activation MimeType match

Introduction

In this page you can find the example usage for javax.activation MimeType match.

Prototype

public boolean match(String rawdata) throws MimeTypeParseException 

Source Link

Document

Determine if the primary and sub type of this object is the same as the content type described in rawdata.

Usage

From source file:eu.fusepool.p3.transformer.client.TransformerClientImpl.java

@Override
public boolean accepts(MimeType type) {
    for (MimeType m : supportedInputFormats) {
        if ((m.match(type)) || m.getPrimaryType().equals("*")
                || (m.getSubType().equals("*") && m.getPrimaryType().equals(type.getPrimaryType()))) {
            return true;
        }//  w  ww  . j ava  2s  .  c o  m
    }
    return false;
}

From source file:com.haulmont.cuba.restapi.LoginServiceController.java

@RequestMapping(value = "/api/logout", method = RequestMethod.POST)
public void logoutByPost(@RequestBody String requestBody,
        @RequestHeader(value = "Content-Type") MimeType contentType, HttpServletResponse response)
        throws IOException, JSONException {

    String sessionUUID;/*from  www.ja v a2 s  .  c om*/
    if (contentType.match(JSONConverter.MIME_TYPE_JSON)) {
        try {
            JSONObject json = new JSONObject(requestBody);
            sessionUUID = json.getString("session");
        } catch (JSONException e) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
    } else if (contentType.match(FORM_TYPE)) {
        String[] fields = requestBody.split("=");
        if (fields.length < 2) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        sessionUUID = URLEncodeUtils.decodeUtf8(fields[1]);
    } else {
        throw new IllegalStateException("Unsupported content type: " + contentType);
    }

    doLogout(sessionUUID, response);
}

From source file:com.haulmont.cuba.restapi.LoginServiceController.java

@RequestMapping(value = "/api/login", method = RequestMethod.POST)
public void loginByPost(@RequestBody String requestBody,
        @RequestHeader(value = "Content-Type") MimeType contentType, HttpServletRequest request,
        HttpServletResponse response) throws IOException, JSONException {

    String username;/*from   w w w  .ja  v  a2 s.  co  m*/
    String password;
    String localeStr;
    if (contentType.match(JSONConverter.MIME_TYPE_JSON)) {
        try {
            JSONObject json = new JSONObject(requestBody);
            username = json.getString("username");
            password = json.getString("password");
            localeStr = json.getString("locale");
        } catch (JSONException e) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
    } else if (contentType.match(FORM_TYPE)) {
        String[] pairs = requestBody.split("\\&");
        Map<String, String> name2value = new HashMap<>();
        for (String pair : pairs) {
            String[] fields = pair.split("=");
            if (fields.length < 2) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST);
                return;
            }
            String name = URLEncodeUtils.decodeUtf8(fields[0]);
            String value = URLEncodeUtils.decodeUtf8(fields[1]);
            name2value.put(name, value);
        }
        username = name2value.get("username");
        password = name2value.get("password");
        localeStr = name2value.get("locale");
    } else {
        throw new IllegalStateException("Unsupported content type: " + contentType);
    }

    doLogin(username, password, localeStr, request, response);
}

From source file:com.naryx.tagfusion.cfm.mail.cfMailMessageData.java

private void extractBody(Part Mess, cfArrayData AD, String attachURI, String attachDIR) throws Exception {

    if (Mess.isMimeType("multipart/*")) {

        Multipart mp = (Multipart) Mess.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            extractBody(mp.getBodyPart(i), AD, attachURI, attachDIR);

    } else {/*from  ww w.  ja v  a  2s . c  o  m*/

        cfStructData sd = new cfStructData();

        String tmp = Mess.getContentType();
        if (tmp.indexOf(";") != -1)
            tmp = tmp.substring(0, tmp.indexOf(";"));

        sd.setData("mimetype", new cfStringData(tmp));

        String filename = getFilename(Mess);
        String dispos = getDisposition(Mess);

        MimeType messtype = new MimeType(tmp);
        // Note that we can't use Mess.isMimeType() here due to bug #2080
        if ((dispos == null || dispos.equalsIgnoreCase(Part.INLINE)) && messtype.match("text/*")) {

            Object content;
            String contentType = Mess.getContentType().toLowerCase();
            // support aliases of UTF-7 - UTF7, UNICODE-1-1-UTF-7, csUnicode11UTF7, UNICODE-2-0-UTF-7
            if (contentType.indexOf("utf-7") != -1 || contentType.indexOf("utf7") != -1) {
                InputStream ins = Mess.getInputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                IOUtils.copy(ins, bos);
                content = new String(UTF7Converter.convert(bos.toByteArray()));
            } else {
                try {
                    content = Mess.getContent();
                } catch (UnsupportedEncodingException e) {
                    content = Mess.getInputStream();
                } catch (IOException ioe) {
                    // This will happen on BD/Java when the attachment has no content
                    // NOTE: this is the fix for bug NA#3198.
                    content = "";
                }
            }

            if (content instanceof InputStream) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                IOUtils.copy((InputStream) content, bos);
                sd.setData("content", new cfStringData(new String(bos.toByteArray())));
            } else {
                sd.setData("content", new cfStringData(content.toString()));
            }

            sd.setData("file", cfBooleanData.FALSE);
            sd.setData("filename", new cfStringData(filename == null ? "" : filename));

        } else if (attachDIR != null) {

            sd.setData("content", new cfStringData(""));

            if (filename == null || filename.length() == 0)
                filename = "unknownfile";

            filename = getAttachedFilename(attachDIR, filename);

            //--[ An attachment, save it out to disk
            try {
                BufferedInputStream in = new BufferedInputStream(Mess.getInputStream());
                BufferedOutputStream out = new BufferedOutputStream(
                        cfEngine.thisPlatform.getFileIO().getFileOutputStream(new File(attachDIR + filename)));
                IOUtils.copy(in, out);

                out.flush();
                out.close();
                in.close();

                sd.setData("file", cfBooleanData.TRUE);
                sd.setData("filename", new cfStringData(filename));
                if (attachURI.charAt(attachURI.length() - 1) != '/')
                    sd.setData("url", new cfStringData(attachURI + '/' + filename));
                else
                    sd.setData("url", new cfStringData(attachURI + filename));
                sd.setData("size", new cfNumberData((int) new File(attachDIR + filename).length()));

            } catch (Exception ignoreException) {
                // NOTE: this could happen when we don't have permission to write to the specified directory
                //       so let's log an error message to make this easier to debug.
                cfEngine.log("-] Failed to save attachment to " + attachDIR + filename + ", exception=["
                        + ignoreException.toString() + "]");
            }

        } else {
            sd.setData("file", cfBooleanData.FALSE);
            sd.setData("content", new cfStringData(""));
        }

        AD.addElement(sd);
    }
}

From source file:org.osaf.cosmo.dav.impl.StandardDavRequest.java

private Document getSafeRequestDocument(boolean requireDocument) throws DavException {
    try {/*  www . java 2  s .  c  o  m*/
        if (StringUtils.isBlank(getContentType())) {
            if (requireDocument)
                throw new BadRequestException("No Content-Type specified");
            return null;
        }
        MimeType mimeType = new MimeType(getContentType());
        if (!(mimeType.match(APPLICATION_XML) || mimeType.match(TEXT_XML)))
            throw new UnsupportedMediaTypeException(
                    "Expected Content-Type " + APPLICATION_XML + " or " + TEXT_XML);
        return getRequestDocument();
    } catch (MimeTypeParseException e) {
        throw new UnsupportedMediaTypeException(e.getMessage());
    } catch (IllegalArgumentException e) {
        Throwable cause = e.getCause();
        String msg = cause != null ? cause.getMessage() : null;
        if (msg == null)
            msg = "Unknown error parsing request document";
        throw new BadRequestException(msg);
    }
}

From source file:org.unitedinternet.cosmo.dav.impl.StandardDavRequest.java

/**
 * //  w  w w .j a v a  2  s. c o m
 * @param requireDocument boolean 
 * @return Document
 * @throws CosmoDavException 
 */
private Document getSafeRequestDocument(boolean requireDocument) throws CosmoDavException {
    try {
        if (StringUtils.isBlank(getContentType()) && requireDocument) {
            throw new BadRequestException("No Content-Type specified");
        }
        MimeType mimeType = new MimeType(getContentType());
        if (!(mimeType.match(APPLICATION_XML) || mimeType.match(TEXT_XML))) {
            throw new UnsupportedMediaTypeException(
                    "Expected Content-Type " + APPLICATION_XML + " or " + TEXT_XML);
        }

        return getRequestDocument();

    } catch (MimeTypeParseException e) {
        throw new UnsupportedMediaTypeException(e.getMessage());
    } catch (IllegalArgumentException e) {
        throwBadRequestExceptionFrom(e);
    } catch (DavException e) {
        throwBadRequestExceptionFrom(e);
    }

    return null;
}