Example usage for javax.activation MimeType toString

List of usage examples for javax.activation MimeType toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Return the String representation of this object.

Usage

From source file:org.apache.abdera.protocol.server.servlet.AbderaServlet.java

private void output(HttpServletRequest request, HttpServletResponse response, ResponseContext context)
        throws IOException {
    if (context != null) {
        response.setStatus(context.getStatus());
        long cl = context.getContentLength();
        String cc = context.getCacheControl();
        if (cl > -1)
            response.setHeader("Content-Length", Long.toString(cl));
        if (cc != null && cc.length() > 0)
            response.setHeader("Cache-Control", cc);
        try {//  w w  w . j ava2 s  .co  m
            MimeType ct = context.getContentType();
            if (ct != null)
                response.setContentType(ct.toString());
        } catch (Exception e) {
        }
        String[] names = context.getHeaderNames();
        for (String name : names) {
            Object[] headers = context.getHeaders(name);
            for (Object value : headers) {
                if (value instanceof Date)
                    response.setDateHeader(name, ((Date) value).getTime());
                else
                    response.setHeader(name, value.toString());
            }
        }
        if (!request.getMethod().equals("HEAD") && context.hasEntity()) {
            context.writeTo(response.getOutputStream());
        }
    } else {
        error("Internal Server Error", null, response);
    }
}

From source file:org.codice.ddf.spatial.ogc.catalog.resource.impl.OgcUrlResourceReader.java

/**
 * Retrieves a {@link ddf.catalog.resource.Resource} based on a {@link URI} and provided
 * arguments. A connection is made to the {@link URI} to obtain the
 * {@link ddf.catalog.resource.Resource}'s {@link InputStream} and build a
 * {@link ResourceResponse} from that. The {@link ddf.catalog.resource.Resource}'s name gets set
 * to the {@link URI} passed in. Calls {@link URLResourceReader}, if the mime-type is
 * "text/html" it will inject a simple script to redirect to the resourceURI instead of
 * attempting to download it.//from ww w .j  a va2 s . c om
 *
 * @param resourceURI
 *            A {@link URI} that defines what {@link Resource} to retrieve and how to do it.
 * @param properties
 *            Any additional arguments that should be passed to the
 *            {@link ddf.catalog.resource.ResourceReader}.
 * @return A {@link ResourceResponse} containing the retrieved {@link Resource}.
 * @throws ResourceNotSupportedException
 */
public ResourceResponse retrieveResource(URI resourceURI, Map<String, Serializable> properties)
        throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    LOGGER.debug("Calling URLResourceReader.retrieveResource()");
    ResourceResponse response = urlResourceReader.retrieveResource(resourceURI, properties);
    Resource resource = response.getResource();
    MimeType mimeType = resource.getMimeType();
    LOGGER.debug("mimeType: {}", mimeType);
    if (mimeType != null) {
        String mimeTypeStr = mimeType.toString();
        String detectedMimeType = "";
        if (UNKNOWN_MIME_TYPES.contains(mimeTypeStr)) {
            detectedMimeType = tika.detect(resourceURI.toURL());
        }
        if (StringUtils.contains(detectedMimeType, MediaType.TEXT_HTML)
                || StringUtils.contains(mimeTypeStr, MediaType.TEXT_HTML)) {
            LOGGER.debug("Detected \"text\\html\". Building redirect script");
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.append("<html><script type=\"text/javascript\">window.location.replace(\"");
            strBuilder.append(resourceURI);
            strBuilder.append("\");</script></html>");
            return new ResourceResponseImpl(new ResourceImpl(
                    new ByteArrayInputStream(strBuilder.toString().getBytes(StandardCharsets.UTF_8)),
                    detectedMimeType, resource.getName()));
        }
    }
    return response;
}

From source file:org.kalypso.model.wspm.pdb.ui.internal.admin.attachments.documents.DocumentsAttachmentsDocumentsData.java

public Document addDocument(final File file) {
    /* Create the new document. */
    final Document document = new Document();
    document.setName(file.getName());//from w  ww.j  av  a 2s. c o  m
    document.setDescription(StringUtils.EMPTY);
    document.setMeasurementDate(new Date(file.lastModified()));
    document.setFilename(getFilePath(m_documentContainer, file.getName()));

    /* Mime type. */
    final MimeType mimeType = getMimeType(file);
    if (mimeType != null)
        document.setMimetype(mimeType.toString());

    /* Apply the image metadata. */
    final TiffImageMetadata exif = readImageMetadata(document, file);
    if (exif != null)
        applyImageMetadata(document, exif);

    /* Set the document container reference. */
    document.setState(null);
    document.setWaterBody(null);
    document.setCrossSection(null);

    if (m_documentContainer instanceof WaterBody)
        document.setWaterBody((WaterBody) m_documentContainer);

    if (m_documentContainer instanceof State)
        document.setState((State) m_documentContainer);

    if (m_documentContainer instanceof CrossSection)
        document.setCrossSection((CrossSection) m_documentContainer);

    /* Create and hash the import document info. */
    addInfo(new DocumentsDocumentInfo(document, file, m_documentContainer));

    return document;
}

From source file:org.kalypso.model.wspm.pdb.ui.internal.admin.attachments.profiles.ProfilesAttachmentsDocumentsData.java

public Document addDocument(final BigDecimal station, final File file) {
    /* Create the new document */
    final Document document = new Document();
    document.setName(file.getName());// w ww .j  a  v  a 2s  .co  m
    document.setDescription(StringUtils.EMPTY);
    document.setMeasurementDate(new Date(file.lastModified()));
    document.setFilename(getFilePath(findCrossSection(station), file.getName()));

    /* Mime type. */
    final MimeType mimeType = getMimeType(file);
    if (mimeType != null)
        document.setMimetype(mimeType.toString());

    /* Apply the image metadata. */
    final TiffImageMetadata exif = readImageMetadata(document, file);
    if (exif != null)
        applyImageMetadata(document, exif);

    /* Set the document container reference. */
    // REMARK: we set state + water body to null here: this is a profile document!
    // I.e. if the profile is removed, also this document will be destroyed which is ok.
    document.setState(null);
    document.setWaterBody(null);
    document.setCrossSection(findCrossSection(station));

    /* Create and hash the import document info. */
    final Document[] dbDocuments = findDbDocuments(station);
    addInfo(new ProfilesDocumentInfo(document, file, station, dbDocuments));

    return document;
}

From source file:org.kalypso.model.wspm.pdb.wspm.CheckinDocumentWorker.java

public void createDocuments(final Session session, final State state, final TuhhReach reach) {
    if (state == null || reach == null)
        return;// www .  j av a 2  s.c o  m

    final IFeatureBindingCollection<Image> images = reach.getImages();
    for (final Image image : images) {
        final MimeType mimeType = image.getMimeType();
        final URI uri = image.getUri();

        final Document document = new Document();
        document.setCreationDate(state.getCreationDate());
        document.setDescription(image.getDescription());
        document.setEditingDate(state.getEditingDate());
        document.setEditingUser(state.getEditingUser());
        document.setFilename(asFilename(uri));
        document.setLocation(asPoint(image.getLocation()));
        document.setMeasurementDate(state.getMeasurementDate()); // bad, should come from image
        document.setMimetype(mimeType == null ? null : mimeType.toString());
        document.setName(asName(uri));
        // document.setShotdirection( null );
        // document.setViewangle( null );

        // REMARK: we set cross section + water body to null here: this is a state document!
        // I.e. if the state is removed, also this document will be destroyed which is ok.
        document.setCrossSection(null);
        document.setState(state);
        document.setWaterBody(null);

        if (session != null)
            session.save(document);
    }
}

From source file:org.kalypso.model.wspm.pdb.wspm.CheckinDocumentWorker.java

public void createDocuments(final Session session, final CrossSection section, final IProfileFeature profile) {
    if (section == null || profile == null)
        return;/*from   ww  w.  j  a v a 2 s . c  o  m*/

    final IFeatureBindingCollection<Image> images = profile.getImages();
    for (final Image image : images) {
        final MimeType mimeType = image.getMimeType();
        final URI uri = image.getUri();

        final Document document = new Document();
        document.setCreationDate(section.getCreationDate());
        document.setDescription(image.getDescription());
        document.setEditingDate(section.getEditingDate());
        document.setEditingUser(section.getEditingUser());
        document.setFilename(asFilename(uri));
        document.setLocation(asPoint(image.getLocation()));
        document.setMeasurementDate(section.getMeasurementDate()); // bad, should come from image
        document.setMimetype(mimeType == null ? null : mimeType.toString());
        document.setName(asName(uri));
        // document.setShotdirection( null );
        // document.setViewangle( null );

        // REMARK: we set state + water body to null here: this is a profile document!
        // I.e. if the profile is removed, also this document will be destroyed which is ok.
        document.setCrossSection(section);
        document.setState(null);
        document.setWaterBody(null);

        if (session != null)
            session.save(document);
    }
}

From source file:org.kalypsodeegree_impl.gml.binding.commons.Image.java

public void setMimeType(final MimeType mimeType) {
    if (mimeType == null)
        setProperty(PROPERTY_MIME_TYPE, null);
    else//w w w  . j a v  a2s  .c  o m
        setProperty(PROPERTY_MIME_TYPE, mimeType.toString());
}

From source file:org.osaf.cosmo.atom.AtomServlet.java

private void output(HttpServletRequest request, HttpServletResponse response, ResponseContext context)
        throws IOException {
    if (context != null) {
        response.setStatus(context.getStatus());
        long cl = context.getContentLength();
        String cc = context.getCacheControl();
        if (cl > -1)
            response.setHeader("Content-Length", Long.toString(cl));
        if (cc != null && cc.length() > 0)
            response.setHeader("Cache-Control", cc);
        try {// w  w  w .  j a  va2s.c o  m
            MimeType ct = context.getContentType();
            if (ct != null)
                response.setContentType(ct.toString());
        } catch (Exception e) {
        }
        String[] names = context.getHeaderNames();
        for (String name : names) {
            Object[] headers = context.getHeaders(name);
            for (Object value : headers) {
                if (value instanceof Date)
                    response.setDateHeader(name, ((Date) value).getTime());
                else
                    response.setHeader(name, value.toString());
            }
        }

        if (!request.getMethod().equals("HEAD") && context.hasEntity()) {
            context.writeTo(response.getOutputStream());
        }
    } else {
        error("Internal Server Error", null, response);
    }
}

From source file:org.osaf.cosmo.atom.provider.BaseCollectionAdapter.java

protected ResponseContext checkCollectionWritePreconditions(RequestContext request) {
    int contentLength = Integer.valueOf(request.getProperty(RequestContext.Property.CONTENTLENGTH).toString());
    if (contentLength <= 0)
        return lengthrequired(request);

    MimeType ct = request.getContentType();
    if (ct == null || !MimeTypeHelper.isMatch(MEDIA_TYPE_XHTML, ct.toString()))
        return ProviderHelper.notsupported(request, "Content-Type must be " + MEDIA_TYPE_XHTML);

    return null;/*from   w ww  . j a va2s. c om*/
}

From source file:org.osaf.cosmo.atom.provider.BaseCollectionAdapter.java

protected ResponseContext checkEntryWritePreconditions(RequestContext request, boolean requireAtomContent) {
    int contentLength = Integer.valueOf(request.getProperty(RequestContext.Property.CONTENTLENGTH).toString());
    if (contentLength <= 0)
        return lengthrequired(request);

    MimeType ct = request.getContentType();
    if (ct == null)
        return ProviderHelper.badrequest(request, "Content-Type required");

    if (!requireAtomContent)
        return null;

    if (!MimeTypeHelper.isMatch(Constants.ATOM_MEDIA_TYPE, ct.toString()))
        return ProviderHelper.notsupported(request, "Content-Type must be " + Constants.ATOM_MEDIA_TYPE);

    try {/*from  ww  w.  j av  a  2 s.  c  o  m*/
        if (!(request.getDocument().getRoot() instanceof Entry))
            return ProviderHelper.badrequest(request, "Entity-body must be an Atom entry");
    } catch (IOException e) {
        String reason = "Unable to read request content: " + e.getMessage();
        log.error(reason, e);
        return ProviderHelper.servererror(request, reason, e);
    }

    return null;
}