Example usage for javax.activation MimeType MimeType

List of usage examples for javax.activation MimeType MimeType

Introduction

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

Prototype

public MimeType(String rawdata) throws MimeTypeParseException 

Source Link

Document

Constructor that builds a MimeType from a String.

Usage

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/portfolios/portfolio/code/{code}")
@GET//  w  ww . ja  va 2 s.co  m
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

public String getPortfolioByCode(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @PathParam("code") String code, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("user") Integer userId, @QueryParam("group") Integer group,
        @QueryParam("resources") String resources) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider
                .getPortfolioByCode(new MimeType("text/xml"), code, ui.userId, groupId, resources, ui.subId)
                .toString();
        if (returnValue.equals("faux")) {

            throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires");
        }
        if (accept.equals(MediaType.APPLICATION_JSON))
            returnValue = XML.toJSONObject(returnValue).toString();

        logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

        return returnValue;
    } catch (RestWebApplicationException ex) {
        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (SQLException ex) {
        logRestRequest(httpServletRequest, null, "Portfolio code = " + code + " not found",
                Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Portfolio code = " + code + " not found");
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + ex.getStackTrace(),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/portfolios")
@GET/*from www.j  a  v  a 2  s .  co m*/
@Consumes(MediaType.APPLICATION_XML)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String getPortfolios(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept,
        @QueryParam("active") String active, @QueryParam("user") Integer userId,
        @QueryParam("code") String code, @QueryParam("portfolio") String portfolioUuid) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        if (portfolioUuid != null) {
            String returnValue = dataProvider.getPortfolio(new MimeType("text/xml"), portfolioUuid, ui.userId,
                    groupId, this.label, null, null, ui.subId).toString();
            if (accept.equals(MediaType.APPLICATION_JSON))
                returnValue = XML.toJSONObject(returnValue).toString();

            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

            return returnValue;

        } else {
            String portfolioCode = null;
            String returnValue = "";
            Boolean portfolioActive;
            try {
                if (active.equals("false") || active.equals("0"))
                    portfolioActive = false;
                else
                    portfolioActive = true;
            } catch (Exception ex) {
                portfolioActive = true;
            }
            ;

            try {
                portfolioCode = code;
            } catch (Exception ex) {
            }
            ;
            if (portfolioCode != null) {
                returnValue = dataProvider.getPortfolioByCode(new MimeType("text/xml"), portfolioCode,
                        ui.userId, groupId, null, ui.subId).toString();
            } else {
                if (userId != null && credential.isAdmin(ui.userId)) {
                    returnValue = dataProvider
                            .getPortfolios(new MimeType("text/xml"), userId, groupId, portfolioActive, ui.subId)
                            .toString();
                } else {
                    returnValue = dataProvider.getPortfolios(new MimeType("text/xml"), ui.userId, groupId,
                            portfolioActive, ui.subId).toString();
                }

                if (accept.equals(MediaType.APPLICATION_JSON))
                    returnValue = XML.toJSONObject(returnValue).toString();
            }
            logRestRequest(httpServletRequest, null, returnValue, Status.OK.getStatusCode());

            return returnValue;
        }
    } catch (RestWebApplicationException ex) {
        logRestRequest(httpServletRequest, null, null, Status.FORBIDDEN.getStatusCode());

        throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString());
    } catch (SQLException ex) {
        logRestRequest(httpServletRequest, null, null, Status.NOT_FOUND.getStatusCode());

        throw new RestWebApplicationException(Status.NOT_FOUND, "Portfolios  not found");
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:org.codice.ddf.rest.impl.CatalogServiceImpl.java

private Metacard parseMetadata(String transformerParam, Metacard metacard, Attachment attachment,
        InputStream inputStream) {
    String transformer = DEFAULT_METACARD_TRANSFORMER;
    if (transformerParam != null) {
        transformer = transformerParam;//w  w  w. java 2s  .com
    }
    try {
        MimeType mimeType = new MimeType(attachment.getContentType().toString());
        metacard = generateMetacard(mimeType, null, inputStream, transformer);
    } catch (MimeTypeParseException | MetacardCreationException e) {
        LOGGER.debug("Unable to parse metadata {}", attachment.getContentType());
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return metacard;
}

From source file:org.codice.ddf.endpoints.rest.RESTEndpoint.java

private MimeType getMimeType(HttpHeaders headers) {
    List<String> contentTypeList = headers.getRequestHeader(HttpHeaders.CONTENT_TYPE);

    String singleMimeType = null;

    if (contentTypeList != null && !contentTypeList.isEmpty()) {
        singleMimeType = contentTypeList.get(0);
        LOGGER.debug("Encountered [{}] {}", singleMimeType, HttpHeaders.CONTENT_TYPE);
    }/*from w ww. j av  a  2 s  . com*/

    MimeType mimeType = null;

    // Sending a null argument to MimeType causes NPE
    if (singleMimeType != null) {
        try {
            mimeType = new MimeType(singleMimeType);
        } catch (MimeTypeParseException e) {
            LOGGER.debug("Could not parse mime type from headers.", e);
        }
    }

    return mimeType;
}

From source file:org.codice.ddf.rest.impl.CatalogServiceImpl.java

private Metacard parseMetacard(String transformerParam, Metacard metacard, Part part, InputStream inputStream) {
    String transformer = "xml";
    if (transformerParam != null) {
        transformer = transformerParam;//from w  ww  .ja  v  a2s .c o  m
    }
    try {
        MimeType mimeType = new MimeType(part.getContentType());
        metacard = generateMetacard(mimeType, null, inputStream, transformer);
    } catch (MimeTypeParseException | MetacardCreationException e) {
        LOGGER.debug("Unable to parse metadata {}", part.getContentType());
    }
    return metacard;
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/portfolios/portfolio/{portfolio-id}")
@PUT//  ww  w. j  a va2  s .  c o  m
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public String putPortfolio(String xmlPortfolio, @CookieParam("user") String user,
        @CookieParam("credential") String token, @QueryParam("group") int groupId,
        @PathParam("portfolio-id") String portfolioUuid, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @QueryParam("active") String active,
        @QueryParam("user") Integer userId) {
    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        Boolean portfolioActive;
        try {
            if (active.equals("false") || active.equals("0"))
                portfolioActive = false;
            else
                portfolioActive = true;
        } catch (Exception ex) {
            portfolioActive = null;
        }
        ;

        dataProvider.putPortfolio(new MimeType("text/xml"), new MimeType("text/xml"), xmlPortfolio,
                portfolioUuid, ui.userId, portfolioActive, groupId, null);
        logRestRequest(httpServletRequest, xmlPortfolio, null, Status.OK.getStatusCode());

        return "";

    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, xmlPortfolio,
                ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:fi.foyt.fni.materials.MaterialController.java

public MimeType parseMimeType(String mimeType) throws MimeTypeParseException {
    MimeType type = new MimeType(mimeType);
    return type;/*from   www .  j  av  a2s.c  om*/
}

From source file:org.codice.ddf.rest.impl.CatalogServiceImpl.java

private MimeType getMimeType(List<String> contentTypeList) {
    String singleMimeType = null;

    if (contentTypeList != null && !contentTypeList.isEmpty()) {
        singleMimeType = contentTypeList.get(0);
        LOGGER.debug("Encountered [{}] {}", singleMimeType, HttpHeaders.CONTENT_TYPE);
    }/*  w  ww. jav  a2  s .  co  m*/

    MimeType mimeType = null;

    // Sending a null argument to MimeType causes NPE
    if (singleMimeType != null) {
        try {
            mimeType = new MimeType(singleMimeType);
        } catch (MimeTypeParseException e) {
            LOGGER.debug("Could not parse mime type from headers.", e);
        }
    }

    return mimeType;
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/portfolios/instanciate/{portfolio-id}")
@POST/*from  ww  w .ja  va 2  s .  c o m*/
public String postInstanciatePortfolio(@CookieParam("user") String user,
        @CookieParam("credential") String token, @QueryParam("group") int groupId, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @PathParam("portfolio-id") String portfolioId,
        @QueryParam("sourcecode") String srccode, @QueryParam("targetcode") String tgtcode,
        @QueryParam("copyshared") String copy, @QueryParam("groupname") String groupname) {
    String value = "Instanciate: " + portfolioId;

    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        boolean copyshared = false;
        if ("y".equalsIgnoreCase(copy))
            copyshared = true;

        String returnValue = dataProvider.postInstanciatePortfolio(new MimeType("text/xml"), portfolioId,
                srccode, tgtcode, ui.userId, groupId, copyshared, groupname).toString();
        logRestRequest(httpServletRequest, value + " to: " + returnValue, returnValue,
                Status.OK.getStatusCode());

        return returnValue;
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, value + " --> Error",
                ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}

From source file:com.portfolio.rest.RestServicePortfolio.java

@Path("/portfolios/copy/{portfolio-id}")
@POST/*from ww w. j av a 2 s. co  m*/
public String postCopyPortfolio(@CookieParam("user") String user, @CookieParam("credential") String token,
        @QueryParam("group") int groupId, @Context ServletConfig sc,
        @Context HttpServletRequest httpServletRequest, @PathParam("portfolio-id") String portfolioId,
        @QueryParam("sourcecode") String srccode, @QueryParam("targetcode") String tgtcode) {
    String value = "Instanciate: " + portfolioId;

    UserInfo ui = checkCredential(httpServletRequest, user, token, null);

    try {
        String returnValue = dataProvider
                .postCopyPortfolio(new MimeType("text/xml"), portfolioId, srccode, tgtcode, ui.userId)
                .toString();
        logRestRequest(httpServletRequest, value + " to: " + returnValue, returnValue,
                Status.OK.getStatusCode());

        return returnValue;
    } catch (Exception ex) {
        ex.printStackTrace();
        logRestRequest(httpServletRequest, value + " --> Error",
                ex.getMessage() + "\n\n" + javaUtils.getCompleteStackTrace(ex),
                Status.INTERNAL_SERVER_ERROR.getStatusCode());

        throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage());
    } finally {
        dataProvider.disconnect();
    }
}