Package org.apache.tika.mime

Examples of org.apache.tika.mime.MediaType


            TikaInputStream tis = TikaInputStream.get(input, tmp);

            byte[] prefix = new byte[1024]; // enough for all known formats
            int length = tis.peek(prefix);

            MediaType type = detectArchiveFormat(prefix, length);
            if (PackageParser.isZipArchive(type)
                    && TikaInputStream.isTikaInputStream(input)) {
                return detectZipFormat(tis);
            } else if (!type.equals(MediaType.OCTET_STREAM)) {
                return type;
            } else {
                return detectCompressorFormat(prefix, length);
            }
        } finally {
View Full Code Here


    private static MediaType detectZipFormat(TikaInputStream tis) {
        try {
            ZipFile zip = new ZipFile(tis.getFile()); // TODO: hasFile()?
            try {
                MediaType type = detectOpenDocument(zip);
                if (type == null) {
                    type = detectOfficeOpenXML(zip, tis);
                }
                if (type == null) {
                    type = detectIWork(zip);
View Full Code Here

            TikaInputStream stream = TikaInputStream.get(
                    new DocumentInputStream((DocumentEntry) ooxml));
            try {
                ZipContainerDetector detector = new ZipContainerDetector();
                MediaType type = detector.detect(stream, new Metadata());
                handleEmbeddedResource(stream, null, dir.getName(), type.toString(), xhtml, true);
                return;
            } finally {
                stream.close();
            }
        }

        // It's regular OLE2:

        // What kind of document is it?
        Metadata metadata = new Metadata();
        metadata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, dir.getName());
        POIFSDocumentType type = POIFSDocumentType.detectType(dir);
        TikaInputStream embedded = null;

        try {
            if (type == POIFSDocumentType.OLE10_NATIVE) {
                try {
                    // Try to un-wrap the OLE10Native record:
                    Ole10Native ole = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)dir);
                    metadata.set(Metadata.RESOURCE_NAME_KEY, dir.getName() + '/' + ole.getLabel());
                   
                    byte[] data = ole.getDataBuffer();
                    embedded = TikaInputStream.get(data);
                } catch (Ole10NativeException ex) {
                    // Not a valid OLE10Native record, skip it
                }
            } else if (type == POIFSDocumentType.COMP_OBJ) {
                try {
                   // Grab the contents and process
                   DocumentEntry contentsEntry;
                   try {
                     contentsEntry = (DocumentEntry)dir.getEntry("CONTENTS");
                   } catch (FileNotFoundException ioe) {
                     contentsEntry = (DocumentEntry)dir.getEntry("Contents");
                   }
                   DocumentInputStream inp = new DocumentInputStream(contentsEntry);
                   byte[] contents = new byte[contentsEntry.getSize()];
                   inp.readFully(contents);
                   embedded = TikaInputStream.get(contents);
                  
                   // 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
                   metadata.set(Metadata.CONTENT_TYPE, mediaType.getType().toString());
                   metadata.set(Metadata.RESOURCE_NAME_KEY, dir.getName() + extension);
                } catch(Exception e) {
                   throw new TikaException("Invalid embedded resource", e);
                }
            } else {
View Full Code Here

                metadata.set(Metadata.LONGITUDE, m.group(2));
            } else {
                metadata.set("ICBM", value);
            }
        } else if (name.equalsIgnoreCase(Metadata.CONTENT_TYPE)){
            MediaType type = MediaType.parse(value);
            if (type != null) {
                metadata.set(Metadata.CONTENT_TYPE, type.toString());
            } else {
                metadata.set(Metadata.CONTENT_TYPE, value);
            }
        } else {
            metadata.set(name, value);
View Full Code Here

      String rtAndPath = cmd + CMD_SEPARATOR + realPath;
      String mimeType;
     
      // check for and use mimetype conf property if available
      if (cfg.getHandlerConf().containsKey(PROPERTY_MIMETYPE_ATTR)) {
        MediaType mediaType = MediaType.parse(cfg.getHandlerConf()
            .getProperty(PROPERTY_MIMETYPE_ATTR));
        if (mediaType == null) {
          LOG.log(Level.WARNING, "MIME type ["
              +cfg.getHandlerConf().getProperty(PROPERTY_MIMETYPE_ATTR)+"] specified "
              +"for handler ["+cfg.getClassName()+"] invalid. Defaulting to MIME type ["
              +MediaType.OCTET_STREAM.toString()+"]");
          mediaType = MediaType.OCTET_STREAM;
        }
        mimeType = mediaType.toString();
      } else { // use default mimetype of product on disk
        mimeType = MimeTypesFactory.create().getMimeType(new File(realPath)).getName();
      }
     
      xmlQuery.getResults().add(
View Full Code Here

      }
    }

    public String getSuperTypeForMimeType(String mimeType) {
      try {
        MediaType mediaType = this.mimeTypes.getMediaTypeRegistry().getSupertype(this.mimeTypes.forName(mimeType).getType());
        if (mediaType != null)
          return mediaType.getType() + "/" + mediaType.getSubtype();
        else
          return null;
      }catch (Exception e) {
        LOG.log(Level.WARNING, "Failed to get super-type for mimetype "
            + mimeType + " : " + e.getMessage());
View Full Code Here

      }
    }

    public String getSuperTypeForMimeType(String mimeType) {
      try {
        MediaType mediaType = this.mimeTypes.getMediaTypeRegistry().getSupertype(this.mimeTypes.forName(mimeType).getType());
        if (mediaType != null)
          return mediaType.getType() + "/" + mediaType.getSubtype();
        else
          return null;
      }catch (Exception e) {
        LOG.log(Level.WARNING, "Failed to get super-type for mimetype "
            + mimeType + " : " + e.getMessage());
View Full Code Here

     */
    public boolean canHandle(InputStream stream, Metadata metadata) throws IOException {
        if (!isEnabled()) {
            return false;
        }
        MediaType contentMediaType = null;
        if (parser instanceof AutoDetectParser) {
          contentMediaType = ((AutoDetectParser) parser).getDetector().detect(stream, metadata);
        }
        if (contentMediaType == null) {
          String contentType = metadata.get(Metadata.CONTENT_TYPE);
          contentMediaType = contentType != null ? new MediaType(StringUtils.substringBefore(contentType, "/"), StringUtils.substringAfter(contentType, "/")) : null;
        }

        return contentMediaType != null ? parser.getParsers().containsKey(contentMediaType) : false;
    }
View Full Code Here

      String rtAndPath = cmd + CMD_SEPARATOR + realPath;
      String mimeType;
     
      // check for and use mimetype conf property if available
      if (cfg.getHandlerConf().containsKey(PROPERTY_MIMETYPE_ATTR)) {
        MediaType mediaType = MediaType.parse(cfg.getHandlerConf()
            .getProperty(PROPERTY_MIMETYPE_ATTR));
        if (mediaType == null) {
          LOG.log(Level.WARNING, "MIME type ["
              +cfg.getHandlerConf().getProperty(PROPERTY_MIMETYPE_ATTR)+"] specified "
              +"for handler ["+cfg.getClassName()+"] invalid. Defaulting to MIME type ["
              +MediaType.OCTET_STREAM.toString()+"]");
          mediaType = MediaType.OCTET_STREAM;
        }
        mimeType = mediaType.toString();
      } else { // use default mimetype of product on disk
        mimeType = MimeTypesFactory.create().getMimeType(new File(realPath)).getName();
      }
     
      xmlQuery.getResults().add(
View Full Code Here

    private final MediaType type;

    private final double q;

    public static MediaRange parse(String range, MediaTypeRegistry registry) {
        MediaType type = MediaType.parse(range);
        if (type == null) {
            return null;
        }
        type = registry.normalize(type);

        Map<String, String> parameters =
                new HashMap<String, String>(type.getParameters());
        String q = parameters.remove("q");
        if (q != null) {
            try {
                return new MediaRange(
                        new MediaType(type.getBaseType(), parameters),
                        Double.parseDouble(q));
            } catch (NumberFormatException e) {
                return null;
            }
        }
View Full Code Here

TOP

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

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.