Search in sources :

Example 6 with AnnisBinaryMetaData

use of annis.service.objects.AnnisBinaryMetaData in project ANNIS by korpling.

the class VideoVisualizer method createComponent.

@Override
public MediaElementPlayer createComponent(VisualizerInput input, VisualizationToggle visToggle) {
    List<String> corpusPath = CommonHelper.getCorpusPath(input.getDocument().getGraph(), input.getDocument());
    String binaryServletPath = "";
    String corpusName = corpusPath.get(corpusPath.size() - 1);
    String documentName = corpusPath.get(0);
    corpusName = urlPathEscape.escape(corpusName);
    documentName = urlPathEscape.escape(documentName);
    WebResource resMeta = Helper.getAnnisWebResource().path("meta/binary").path(corpusName).path(documentName);
    List<AnnisBinaryMetaData> meta = resMeta.get(new GenericType<List<AnnisBinaryMetaData>>() {
    });
    // if there is no document at all don't fail
    String mimeType = meta.size() > 0 ? null : "video/webm";
    for (AnnisBinaryMetaData m : meta) {
        if (m.getMimeType().startsWith("video/")) {
            mimeType = m.getMimeType();
            break;
        }
    }
    Validate.notNull(mimeType, "There must be at least one binary file for the document with a video mime type");
    String mimeTypeEncoded = mimeType;
    mimeTypeEncoded = urlParamEscape.escape(mimeType);
    binaryServletPath = input.getContextPath() + "/Binary?" + "documentName=" + documentName + "&toplevelCorpusName=" + corpusName + "&mime=" + mimeTypeEncoded;
    MediaElementPlayer player = new MediaElementPlayer(MediaElement.video, binaryServletPath, mimeType);
    if (VaadinSession.getCurrent().getAttribute(MediaController.class) != null) {
        VaadinSession.getCurrent().getAttribute(MediaController.class).addMediaPlayer(player, input.getId(), visToggle);
    }
    return player;
}
Also used : MediaController(annis.libgui.media.MediaController) MediaElementPlayer(annis.gui.components.medialement.MediaElementPlayer) WebResource(com.sun.jersey.api.client.WebResource) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData) List(java.util.List)

Example 7 with AnnisBinaryMetaData

use of annis.service.objects.AnnisBinaryMetaData in project ANNIS by korpling.

the class AudioVisualizer method createComponent.

@Override
public MediaElementPlayer createComponent(VisualizerInput input, VisualizationToggle visToggle) {
    List<String> corpusPath = CommonHelper.getCorpusPath(input.getDocument().getGraph(), input.getDocument());
    String binaryServletPath = "";
    String corpusName = corpusPath.get(corpusPath.size() - 1);
    String documentName = corpusPath.get(0);
    corpusName = urlPathEscape.escape(corpusName);
    documentName = urlPathEscape.escape(documentName);
    WebResource resMeta = Helper.getAnnisWebResource().path("meta/binary").path(corpusName).path(documentName);
    List<AnnisBinaryMetaData> meta = resMeta.get(new GenericType<List<AnnisBinaryMetaData>>() {
    });
    // if there is no document at all don't fail
    String mimeType = meta.size() > 0 ? null : "audio/ogg";
    for (AnnisBinaryMetaData m : meta) {
        if (m.getMimeType().startsWith("audio/")) {
            mimeType = m.getMimeType();
            break;
        }
    }
    Validate.notNull(mimeType, "There must be at least one binary file for the document with a audio mime type");
    String mimeTypeEncoded = mimeType;
    mimeTypeEncoded = urlParamEscape.escape(mimeType);
    binaryServletPath = input.getContextPath() + "/Binary?" + "documentName=" + documentName + "&toplevelCorpusName=" + corpusName + "&mime=" + mimeTypeEncoded;
    MediaElementPlayer player = new MediaElementPlayer(MediaElement.audio, binaryServletPath, mimeType);
    if (VaadinSession.getCurrent().getAttribute(MediaController.class) != null) {
        VaadinSession.getCurrent().getAttribute(MediaController.class).addMediaPlayer(player, input.getId(), visToggle);
    }
    return player;
}
Also used : MediaController(annis.libgui.media.MediaController) MediaElementPlayer(annis.gui.components.medialement.MediaElementPlayer) WebResource(com.sun.jersey.api.client.WebResource) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData) List(java.util.List)

Example 8 with AnnisBinaryMetaData

use of annis.service.objects.AnnisBinaryMetaData in project ANNIS by korpling.

the class MetaByteHelper method extractData.

@Override
public List<AnnisBinaryMetaData> extractData(ResultSet rs) throws DataAccessException {
    List<AnnisBinaryMetaData> result = new LinkedList<>();
    try {
        while (rs.next()) {
            AnnisBinaryMetaData ab = new AnnisBinaryMetaData();
            ab.setLocalFileName(rs.getString("filename"));
            ab.setFileName(rs.getString("title"));
            ab.setCorpusName(rs.getString("corpus_name"));
            ab.setMimeType(rs.getString("mime_type"));
            result.add(ab);
        }
    } catch (SQLException ex) {
        log.error(null, ex);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData) LinkedList(java.util.LinkedList)

Example 9 with AnnisBinaryMetaData

use of annis.service.objects.AnnisBinaryMetaData in project ANNIS by korpling.

the class QueryServiceImpl method binary.

/**
 * Get an Annis Binary object identified by its id.
 *
 * @param id
 * @param rawOffset the part we want to start from, we start from 0
 * @param rawLength how many bytes we take
 * @return AnnisBinary
 */
@Override
public Response binary(String toplevelCorpusName, String corpusName, String rawOffset, String rawLength, String fileName) {
    Subject user = SecurityUtils.getSubject();
    user.checkPermission("query:binary:" + toplevelCorpusName);
    String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
    if (acceptHeader == null || acceptHeader.trim().isEmpty()) {
        acceptHeader = "*/*";
    }
    List<AnnisBinaryMetaData> meta = queryDao.getBinaryMeta(toplevelCorpusName, corpusName);
    HashMap<String, AnnisBinaryMetaData> matchedMetaByType = new LinkedHashMap<>();
    for (AnnisBinaryMetaData m : meta) {
        if (fileName == null) {
            // just add all available media types
            if (!matchedMetaByType.containsKey(m.getMimeType())) {
                matchedMetaByType.put(m.getMimeType(), m);
            }
        } else {
            // check if this binary has the right title/file name
            if (fileName.equals(m.getFileName())) {
                matchedMetaByType.put(m.getMimeType(), m);
            }
        }
    }
    if (matchedMetaByType.isEmpty()) {
        return Response.status(Response.Status.NOT_FOUND).entity("Requested binary not found").build();
    }
    // find the best matching mime type
    String bestMediaTypeMatch = MIMEParse.bestMatch(matchedMetaByType.keySet(), acceptHeader);
    if (bestMediaTypeMatch.isEmpty()) {
        return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Client must accept one of the following media types: " + StringUtils.join(matchedMetaByType.keySet(), ", ")).build();
    }
    MediaType mediaType = MediaType.valueOf(bestMediaTypeMatch);
    int offset = 0;
    int length = 0;
    if (rawLength == null || rawOffset == null) {
        // use matched binary meta data to get the complete file size
        AnnisBinaryMetaData matchedBinary = matchedMetaByType.get(mediaType.toString());
        if (matchedBinary != null) {
            length = matchedBinary.getLength();
        }
    } else {
        // use the provided information
        offset = Integer.parseInt(rawOffset);
        length = Integer.parseInt(rawLength);
    }
    log.debug("fetching  " + (length / 1024) + "kb (" + offset + "-" + (offset + length) + ") from binary " + toplevelCorpusName + "/" + corpusName + (fileName == null ? "" : fileName) + " " + mediaType.toString());
    final InputStream stream = queryDao.getBinary(toplevelCorpusName, corpusName, mediaType.toString(), fileName, offset, length);
    log.debug("fetch successfully");
    StreamingOutput result = new StreamingOutput() {

        @Override
        public void write(OutputStream output) throws IOException, WebApplicationException {
            try {
                ByteStreams.copy(stream, output);
                output.close();
            } finally {
                stream.close();
            }
        }
    };
    return Response.ok(result, mediaType).build();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData) MediaType(javax.ws.rs.core.MediaType) StreamingOutput(javax.ws.rs.core.StreamingOutput) Subject(org.apache.shiro.subject.Subject) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with AnnisBinaryMetaData

use of annis.service.objects.AnnisBinaryMetaData in project ANNIS by korpling.

the class QueryDaoImpl method getBinaryComplete.

@Override
public InputStream getBinaryComplete(String toplevelCorpusName, String mimeType, String title) {
    List<AnnisBinaryMetaData> binaryMetas = getBinaryMeta(toplevelCorpusName);
    InputStream input = null;
    if (binaryMetas != null) {
        for (AnnisBinaryMetaData metaData : binaryMetas) {
            if (mimeType.equals(metaData.getMimeType()) && title.equals(metaData.getFileName())) {
                String filePath = getRealDataDir().getPath() + "/" + metaData.getLocalFileName();
                try {
                    input = new FileInputStream(filePath);
                    return input;
                } catch (FileNotFoundException ex) {
                    log.error("could not found binary file {}", filePath, ex);
                }
            }
        }
    }
    return input;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData) FileInputStream(java.io.FileInputStream)

Aggregations

AnnisBinaryMetaData (annis.service.objects.AnnisBinaryMetaData)11 WebResource (com.sun.jersey.api.client.WebResource)4 List (java.util.List)3 MediaElementPlayer (annis.gui.components.medialement.MediaElementPlayer)2 MediaController (annis.libgui.media.MediaController)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 SQLException (java.sql.SQLException)2 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)1 VaadinServletResponse (com.vaadin.server.VaadinServletResponse)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 MediaType (javax.ws.rs.core.MediaType)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1