Search in sources :

Example 6 with MimeTypeException

use of org.apache.tika.mime.MimeTypeException in project cerberus-source by cerberustesting.

the class SikuliService method doSikuliAction.

@Override
public AnswerItem<JSONObject> doSikuliAction(Session session, String action, String locator, String text) {
    AnswerItem<JSONObject> answer = new AnswerItem<JSONObject>();
    MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS);
    HttpURLConnection connection = null;
    BufferedReader in = null;
    PrintStream os = null;
    URL url;
    String urlToConnect = "http://" + session.getHost() + ":" + session.getPort() + "/extra/ExecuteSikuliAction";
    try {
        /**
         * Connect to ExecuteSikuliAction Servlet Through SeleniumServer
         */
        url = new URL(urlToConnect);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        JSONObject postParameters = generatePostParameters(action, locator, text, session.getCerberus_selenium_wait_element());
        connection.setDoOutput(true);
        // Send post request
        os = new PrintStream(connection.getOutputStream());
        os.println(postParameters.toString());
        os.println("|ENDS|");
        if (connection == null || connection.getResponseCode() != 200) {
            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_SERVER_NOT_REACHABLE);
        }
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        /**
         * Wait here until receiving |ENDR| String
         */
        while (!(inputLine = in.readLine()).equals("|ENDR|")) {
            response.append(inputLine);
        }
        /**
         * Convert received string into JSONObject
         */
        JSONObject objReceived = new JSONObject(response.toString());
        answer.setItem(objReceived);
        if (objReceived.has("status")) {
            if ("Failed".equals(objReceived.getString("status"))) {
                msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
            } else {
                msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS);
            }
        } else {
            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
        }
        in.close();
    } catch (MalformedURLException ex) {
        LOG.warn(ex);
        MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_SERVER_NOT_REACHABLE);
        mes.setDescription(mes.getDescription().replace("%URL%", urlToConnect));
        msg = mes;
    } catch (FileNotFoundException ex) {
        LOG.warn(ex);
        MessageEvent mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SIKULI_FILE_NOT_FOUND);
        mes.setDescription(mes.getDescription().replace("%FILE%", locator));
        msg = mes;
    } catch (IOException ex) {
        LOG.warn(ex);
        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
    } catch (JSONException ex) {
        LOG.warn(ex);
        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
    } catch (MimeTypeException ex) {
        LOG.warn(ex);
        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
    } finally {
        if (os != null) {
            os.close();
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
    answer.setResultMessage(msg);
    return answer;
}
Also used : PrintStream(java.io.PrintStream) MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) MessageEvent(org.cerberus.engine.entity.MessageEvent) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) IOException(java.io.IOException) AnswerItem(org.cerberus.util.answer.AnswerItem) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.JSONObject) MimeTypeException(org.apache.tika.mime.MimeTypeException) BufferedReader(java.io.BufferedReader)

Example 7 with MimeTypeException

use of org.apache.tika.mime.MimeTypeException in project jackrabbit by apache.

the class SimpleWebdavServlet method getDetector.

/**
 * Reads and returns the configured &lt;mime-info/&gt; database.
 *
 * @see #INIT_PARAM_MIME_INFO
 * @return MIME media type database
 * @throws ServletException if the database is invalid or can not be read
 */
private Detector getDetector() throws ServletException {
    URL url;
    String mimeInfo = getInitParameter(INIT_PARAM_MIME_INFO);
    if (mimeInfo != null) {
        try {
            url = getServletContext().getResource(mimeInfo);
        } catch (MalformedURLException e) {
            throw new ServletException("Invalid " + INIT_PARAM_MIME_INFO + " configuration setting: " + mimeInfo, e);
        }
    } else {
        url = MimeTypesFactory.class.getResource("tika-mimetypes.xml");
    }
    try {
        return MimeTypesFactory.create(url);
    } catch (MimeTypeException e) {
        throw new ServletException("Invalid MIME media type database: " + url, e);
    } catch (IOException e) {
        throw new ServletException("Unable to read MIME media type database: " + url, e);
    }
}
Also used : ServletException(javax.servlet.ServletException) MalformedURLException(java.net.MalformedURLException) MimeTypeException(org.apache.tika.mime.MimeTypeException) MimeTypesFactory(org.apache.tika.mime.MimeTypesFactory) IOException(java.io.IOException) URL(java.net.URL)

Example 8 with MimeTypeException

use of org.apache.tika.mime.MimeTypeException in project Xponents by OpenSextant.

the class HyperLink method setMIMEType.

/**
     * Set the MIME type of a found link, i.e., once you'ved downloaded the content you then know the ContentType
     * possibly.
     * Which may differ from your perception of the URL path
     * 
     * - reset the file extension,
     * - reset the path
     * - folder vs. file
     * 
     * Set the MIME Type, file type, path, etc... prior to saving content to disk.
     *
     * @param t
     *            the new MIME type
     */
public void setMIMEType(String t) {
    mimeType = t;
    if (mimeType == null) {
        return;
    }
    try {
        MimeType mt;
        /* Isolate the MIME type without parameters.
             * 
             */
        mt = defaultMIME.forName(t.split(";", 2)[0]);
        if (mt != null) {
            fixPathExtension(mt.getExtension());
        }
    } catch (MimeTypeException ignore) {
    // Hmm.
    }
}
Also used : MimeTypeException(org.apache.tika.mime.MimeTypeException) MimeType(org.apache.tika.mime.MimeType)

Example 9 with MimeTypeException

use of org.apache.tika.mime.MimeTypeException in project tika by apache.

the class AbstractPOIFSExtractor method handleEmbeddedOfficeDoc.

/**
     * Handle an office document that's embedded at the POIFS level
     */
protected void handleEmbeddedOfficeDoc(DirectoryEntry dir, String resourceName, XHTMLContentHandler xhtml) throws IOException, SAXException, TikaException {
    if (dir.hasEntry("Package")) {
        // It's OOXML (has a ZipFile):
        Entry ooxml = dir.getEntry("Package");
        try (TikaInputStream stream = TikaInputStream.get(new DocumentInputStream((DocumentEntry) ooxml))) {
            ZipContainerDetector detector = new ZipContainerDetector();
            MediaType type = null;
            try {
                //if there's a stream error while detecting...
                type = detector.detect(stream, new Metadata());
            } catch (Exception e) {
                EmbeddedDocumentUtil.recordEmbeddedStreamException(e, parentMetadata);
                return;
            }
            handleEmbeddedResource(stream, null, dir.getName(), dir.getStorageClsid(), type.toString(), xhtml, true);
            return;
        }
    }
    // It's regular OLE2:
    // What kind of document is it?
    Metadata metadata = new Metadata();
    metadata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, dir.getName());
    if (dir.getStorageClsid() != null) {
        metadata.set(Metadata.EMBEDDED_STORAGE_CLASS_ID, dir.getStorageClsid().toString());
    }
    POIFSDocumentType type = POIFSDocumentType.detectType(dir);
    TikaInputStream embedded = null;
    String rName = (resourceName == null) ? dir.getName() : resourceName;
    try {
        if (type == POIFSDocumentType.OLE10_NATIVE) {
            try {
                // Try to un-wrap the OLE10Native record:
                Ole10Native ole = Ole10Native.createFromEmbeddedOleObject((DirectoryNode) dir);
                if (ole.getLabel() != null) {
                    metadata.set(Metadata.RESOURCE_NAME_KEY, rName + '/' + ole.getLabel());
                }
                if (ole.getCommand() != null) {
                    metadata.add(TikaCoreProperties.ORIGINAL_RESOURCE_NAME, ole.getCommand());
                }
                if (ole.getFileName() != null) {
                    metadata.add(TikaCoreProperties.ORIGINAL_RESOURCE_NAME, ole.getFileName());
                }
                byte[] data = ole.getDataBuffer();
                embedded = TikaInputStream.get(data);
            } catch (Ole10NativeException ex) {
            // Not a valid OLE10Native record, skip it
            } catch (Exception e) {
                EmbeddedDocumentUtil.recordEmbeddedStreamException(e, parentMetadata);
                return;
            }
        } else if (type == POIFSDocumentType.COMP_OBJ) {
            try {
                //TODO: figure out if the equivalent of OLE 1.0's
                //getCommand() and getFileName() exist for OLE 2.0 to populate
                //TikaCoreProperties.ORIGINAL_RESOURCE_NAME
                // 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, rName + extension);
            } catch (Exception e) {
                EmbeddedDocumentUtil.recordEmbeddedStreamException(e, parentMetadata);
                return;
            }
        } else {
            metadata.set(Metadata.CONTENT_TYPE, type.getType().toString());
            metadata.set(Metadata.RESOURCE_NAME_KEY, rName + '.' + type.getExtension());
        }
        // Should we parse it?
        if (embeddedDocumentUtil.shouldParseEmbedded(metadata)) {
            if (embedded == null) {
                // Make a TikaInputStream that just
                // passes the root directory of the
                // embedded document, and is otherwise
                // empty (byte[0]):
                embedded = TikaInputStream.get(new byte[0]);
                embedded.setOpenContainer(dir);
            }
            embeddedDocumentUtil.parseEmbedded(embedded, xhtml, metadata, true);
        }
    } catch (IOException e) {
        EmbeddedDocumentUtil.recordEmbeddedStreamException(e, metadata);
    } finally {
        if (embedded != null) {
            embedded.close();
        }
    }
}
Also used : ZipContainerDetector(org.apache.tika.parser.pkg.ZipContainerDetector) Ole10Native(org.apache.poi.poifs.filesystem.Ole10Native) Metadata(org.apache.tika.metadata.Metadata) FileNotFoundException(java.io.FileNotFoundException) TikaInputStream(org.apache.tika.io.TikaInputStream) POIFSDocumentType(org.apache.tika.parser.microsoft.OfficeParser.POIFSDocumentType) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) Ole10NativeException(org.apache.poi.poifs.filesystem.Ole10NativeException) TikaException(org.apache.tika.exception.TikaException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) MimeTypeException(org.apache.tika.mime.MimeTypeException) MimeType(org.apache.tika.mime.MimeType) Entry(org.apache.poi.poifs.filesystem.Entry) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) Ole10NativeException(org.apache.poi.poifs.filesystem.Ole10NativeException) MimeTypeException(org.apache.tika.mime.MimeTypeException) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) MediaType(org.apache.tika.mime.MediaType)

Example 10 with MimeTypeException

use of org.apache.tika.mime.MimeTypeException in project tika by apache.

the class EmbeddedDocumentUtil method getExtension.

public String getExtension(TikaInputStream is, Metadata metadata) {
    String mimeString = metadata.get(Metadata.CONTENT_TYPE);
    TikaConfig config = getConfig();
    MimeType mimeType = null;
    MimeTypes types = config.getMimeRepository();
    boolean detected = false;
    if (mimeString != null) {
        try {
            mimeType = types.forName(mimeString);
        } catch (MimeTypeException e) {
        //swallow
        }
    }
    if (mimeType == null) {
        Detector detector = config.getDetector();
        try {
            MediaType mediaType = detector.detect(is, metadata);
            mimeType = types.forName(mediaType.toString());
            detected = true;
            is.reset();
        } catch (IOException e) {
        //swallow
        } catch (MimeTypeException e) {
        //swallow
        }
    }
    if (mimeType != null) {
        if (detected) {
            //set or correct the mime type
            metadata.set(Metadata.CONTENT_TYPE, mimeType.toString());
        }
        return mimeType.getExtension();
    }
    return ".bin";
}
Also used : Detector(org.apache.tika.detect.Detector) TikaConfig(org.apache.tika.config.TikaConfig) MimeTypeException(org.apache.tika.mime.MimeTypeException) MediaType(org.apache.tika.mime.MediaType) IOException(java.io.IOException) MimeTypes(org.apache.tika.mime.MimeTypes) MimeType(org.apache.tika.mime.MimeType)

Aggregations

MimeTypeException (org.apache.tika.mime.MimeTypeException)13 IOException (java.io.IOException)8 MimeType (org.apache.tika.mime.MimeType)8 TikaConfig (org.apache.tika.config.TikaConfig)4 MediaType (org.apache.tika.mime.MediaType)4 TikaInputStream (org.apache.tika.io.TikaInputStream)3 Metadata (org.apache.tika.metadata.Metadata)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 MimeTypes (org.apache.tika.mime.MimeTypes)2 DataUri (com.github.ooxi.jdatauri.DataUri)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1