Package org.apache.tika.mime

Examples of org.apache.tika.mime.MimeType


        return type;
    }

    public String getExtension(String mimeType) {
        try {
            MimeType type = types.forName(mimeType);
            String extension = type.getExtension();
            if (extension != null && extension.length() > 1) {
                return extension.substring(1); // skip leading "."
            }
        } catch (MimeTypeException e) {
            // ignore
View Full Code Here


    // set headers
    headers.set(Response.CONTENT_LENGTH, new Long(size).toString());
    headers.set(Response.LAST_MODIFIED, HttpDateFormat.toString(f
        .lastModified()));
   
    MimeType mimeType = MIME.getMimeType(f);
    String mimeTypeString = mimeType != null ? mimeType.getName() : "";
    headers.set(Response.CONTENT_TYPE, mimeTypeString);

    // response code
    this.code = 200; // http OK
  }
View Full Code Here

        if (!stream.markSupported()) {
            stream = new BufferedInputStream(stream);
        }

        // Automatically detect the MIME type of the document
        MimeType type = getMimeType(stream, metadata);
        metadata.set(Metadata.CONTENT_TYPE, type.getName());

        // Get the parser configured for the detected MIME type
        Parser parser = config.getParser(type.getName());
        if (parser == null) {
            parser = config.getParser(MimeTypes.DEFAULT);
        }
        if (parser == null) {
            throw new TikaException("No parsers available: " + type.getName());
        }

        // Parse the document
        parser.parse(stream, handler, metadata);
    }
View Full Code Here

        // Get type based on magic prefix
        stream.mark(types.getMinLength());
        try {
            byte[] prefix = getPrefix(stream, types.getMinLength());
            MimeType type = types.getMimeType(prefix);
            if (type != null) {
                return type;
            }
        } finally {
            stream.reset();
        }

        // Get type based on resourceName hint (if available)
        String resourceName = metadata.get(Metadata.RESOURCE_NAME_KEY);
        if (resourceName != null) {
            MimeType type = types.getMimeType(resourceName);
            if (type != null) {
                return type;
            }
        }
View Full Code Here

   *          The byte data, returned from the crawl, if any.
   * @return The correctly, automatically guessed {@link MimeType} name.
   */
  public String autoResolveContentType(String typeName, String url, byte[] data) {
    String retType = null;
    MimeType type = null;
    String cleanedMimeType = null;

    cleanedMimeType = MimeUtil.cleanMimeType(typeName);
    // first try to get the type from the cleaned type name
    if (cleanedMimeType != null) {
      try {
        type = mimeTypes.forName(cleanedMimeType);
        cleanedMimeType = type.getName();
      } catch (MimeTypeException mte) {
        // Seems to be a malformed mime type name...
        cleanedMimeType = null;
      }
    }

    // if returned null, or if it's the default type then try url resolution
    if (type == null
        || (type != null && type.getName().equals(MimeTypes.OCTET_STREAM))) {
      // If no mime-type header, or cannot find a corresponding registered
      // mime-type, then guess a mime-type from the url pattern
      try {
        retType = tika.detect(url) != null ? tika.detect(url) : null;
      } catch (Exception e) {
        String message = "Problem loading default Tika configuration";
        LOG.error(message, e);
        throw new RuntimeException(e);
      }
    } else {
        retType = type.getName();
    }

    // if magic is enabled use mime magic to guess if the mime type returned
    // from the magic guess is different than the one that's already set so far
    // if it is, and it's not the default mime type, then go with the mime type
View Full Code Here

        // Determines the MIMEType based on Content-Type hint if available.
        final String contentType = metadata.get(Metadata.CONTENT_TYPE);
        String candidateMIMEType = null;
        if (contentType != null) {
            try {
                MimeType type = types.forName(contentType);
                if (type != null) {
                    if( ! isPlainMIMEType(type.getName()) ) {
                        return type.getName();
                    } else {
                        candidateMIMEType = type.getName();
                    }
                }
            }
            catch (MimeTypeException mte) {
                // Malformed ocntent-type value, ignore.
            }
        }

        // Determines the MIMEType based on resource name hint if available.
        final String resourceName = metadata.get(Metadata.RESOURCE_NAME_KEY);
        if (resourceName != null) {
            MimeType type = types.getMimeType(resourceName);
            if (type != null) {
                return type.getName();
            }
        }

        // Finally, use the default type if no matches found
        if(candidateMIMEType != null) {
View Full Code Here

                  
                   // Try to work out what it is
                   MediaType mediaType = getDetector().detect(embedded, new Metadata());
                   String extension = type.getExtension();
                   try {
                      MimeType mimeType = getMimeTypes().forName(mediaType.toString());
                      extension = mimeType.getExtension();
                   } catch(MimeTypeException mte) {
                      // No details on this type are known
                   }
                  
                   // Record what we can do about it
View Full Code Here

    MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
      throws IOException, WebApplicationException
  {
    String contentType;
    String dataStoreReference = resource.getDataStoreReference();
    MimeType mimeType = resource.getMimeType();
    if (mimeType != null &&
        mimeType.getName() != null &&
       !mimeType.getName().equals(""))
    {
      contentType = mimeType.getName();
    }
    else
    {
      contentType = new Tika().detect(dataStoreReference);
    }
View Full Code Here

   * @return the name of the MIME type for the reference
   */
  @XmlElement(name = "mimeType")
  public String getMimeTypeName()
  {
    MimeType m = reference.getMimeType();
    if (m != null)
    {
      return m.getName();
    }
    return null;
  }
View Full Code Here

     *            The byte data, returned from the crawl, if any.
     * @return The correctly, automatically guessed {@link MimeType} name.
     */
    public String autoResolveContentType(String typeName, String url,
            byte[] data) {
        MimeType type = null;
        String cleanedMimeType = null;

        try {
            cleanedMimeType = MimeTypeUtils.cleanMimeType(typeName) != null ? this.mimeTypes
                    .forName(MimeTypeUtils.cleanMimeType(typeName)).getName()
                    : null;
        } catch (MimeTypeException mte) {
            // Seems to be a malformed mime type name...
        }

        // first try to get the type from the cleaned type name
        try {
            type = cleanedMimeType != null ? this.mimeTypes
                    .forName(cleanedMimeType) : null;
        } catch (MimeTypeException e) {
            type = null;
        }

        // if returned null, or if it's the default type then try url resolution
        if (type == null
                || (type != null && type.getName().equals(MimeTypes.OCTET_STREAM))) {
            // If no mime-type header, or cannot find a corresponding registered
            // mime-type, then guess a mime-type from the url pattern
            type = this.mimeTypes.getMimeType(url) != null ? this.mimeTypes
                    .getMimeType(url) : type;
        }

        // if magic is enabled use mime magic to guess if the mime type returned
        // from the magic guess is different than the one that's already set so
        // far
        // if it is, and it's not the default mime type, then go with the mime
        // type
        // returned by the magic
        if (this.mimeMagic) {
            MimeType magicType = this.mimeTypes.getMimeType(data);
            if (magicType != null
                    && !magicType.getName().equals(MimeTypes.OCTET_STREAM)
                    && type != null
                    && !type.getName().equals(magicType.getName())) {
                // If magic enabled and the current mime type differs from that
                // of the
                // one returned from the magic, take the magic mimeType
                type = magicType;
            }
View Full Code Here

TOP

Related Classes of org.apache.tika.mime.MimeType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.