Search in sources :

Example 1 with AnnisBinaryMetaData

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

the class PDFPanel method getBinaryPath.

private String getBinaryPath() {
    List<String> corpusPath = CommonHelper.getCorpusPath(input.getDocument().getGraph(), input.getDocument());
    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 : "application/pdf";
    for (AnnisBinaryMetaData m : meta) {
        if (m.getMimeType().equals("application/pdf")) {
            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);
    return input.getContextPath() + "/Binary?" + "documentName=" + documentName + "&toplevelCorpusName=" + corpusName + "&mime=" + mimeTypeEncoded;
}
Also used : WebResource(com.sun.jersey.api.client.WebResource) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData) List(java.util.List)

Example 2 with AnnisBinaryMetaData

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

the class QueryDaoImpl method getBinaryMeta.

@Override
public List<AnnisBinaryMetaData> getBinaryMeta(String toplevelCorpusName, String corpusName) {
    List<AnnisBinaryMetaData> metaData = getJdbcTemplate().query(MetaByteHelper.SQL, metaByteHelper.getArgs(toplevelCorpusName, corpusName), MetaByteHelper.getArgTypes(), metaByteHelper);
    // get the file size from the real file
    ListIterator<AnnisBinaryMetaData> it = metaData.listIterator();
    while (it.hasNext()) {
        AnnisBinaryMetaData singleEntry = it.next();
        File f = new File(getRealDataDir(), singleEntry.getLocalFileName());
        singleEntry.setLength((int) f.length());
    }
    return metaData;
}
Also used : AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData) File(java.io.File)

Example 3 with AnnisBinaryMetaData

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

the class ByteHelper method extractData.

@Override
public AnnisBinaryMetaData extractData(ResultSet rs) throws DataAccessException {
    AnnisBinaryMetaData ab = new AnnisBinaryMetaData();
    try {
        while (rs.next()) {
            ab.setLocalFileName(rs.getString("filename"));
            ab.setFileName(rs.getString("title"));
            ab.setCorpusName(rs.getString("corpus_name"));
            ab.setMimeType(rs.getString("mime_type"));
            // we only give one matching result back
            break;
        }
    } catch (SQLException ex) {
        log.error(null, ex);
    }
    return ab;
}
Also used : SQLException(java.sql.SQLException) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData)

Example 4 with AnnisBinaryMetaData

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

the class BinaryRequestHandler method getMatchingMetadataFromService.

private AnnisBinaryMetaData getMatchingMetadataFromService(WebResource metaBinaryRes, String mimeType) {
    List<AnnisBinaryMetaData> allMeta = metaBinaryRes.get(new AnnisBinaryMetaDataListType());
    AnnisBinaryMetaData bm = allMeta.get(0);
    for (AnnisBinaryMetaData m : allMeta) {
        if (mimeType != null && mimeType.equals(m.getMimeType())) {
            bm = m;
            break;
        }
    }
    return bm;
}
Also used : AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData)

Example 5 with AnnisBinaryMetaData

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

the class BinaryRequestHandler method sendResponse.

public void sendResponse(VaadinSession session, VaadinRequest request, VaadinResponse pureResponse, boolean sendContent) throws IOException {
    if (!(pureResponse instanceof VaadinServletResponse)) {
        pureResponse.sendError(500, "Binary requests only work with servlets");
    }
    VaadinServletResponse response = (VaadinServletResponse) pureResponse;
    Map<String, String[]> binaryParameter = request.getParameterMap();
    String toplevelCorpusName = binaryParameter.get("toplevelCorpusName")[0];
    String documentName = binaryParameter.get("documentName")[0];
    String mimeType = null;
    if (binaryParameter.containsKey("mime")) {
        mimeType = binaryParameter.get("mime")[0];
    }
    try {
        // always set the buffer size to the same one we will use for coyping,
        // otherwise we won't notice any client disconnection
        response.reset();
        response.setCacheTime(-1);
        response.resetBuffer();
        // 4K
        response.setBufferSize(BUFFER_SIZE);
        String requestedRangeRaw = request.getHeader("Range");
        WebResource binaryRes = Helper.getAnnisWebResource().path("query").path("corpora").path(urlPathEscape.escape(toplevelCorpusName)).path(urlPathEscape.escape(documentName)).path("binary");
        WebResource metaBinaryRes = Helper.getAnnisWebResource().path("meta").path("binary").path(urlPathEscape.escape(toplevelCorpusName)).path(urlPathEscape.escape(documentName));
        // tell client that we support byte ranges
        response.setHeader("Accept-Ranges", "bytes");
        Preconditions.checkNotNull(mimeType, "No mime type given (parameter \"mime\"");
        AnnisBinaryMetaData meta = getMatchingMetadataFromService(metaBinaryRes, mimeType);
        if (meta == null) {
            response.sendError(404, "Binary file not found");
            return;
        }
        ContentRange fullRange = new ContentRange(0, meta.getLength() - 1, meta.getLength());
        ContentRange r = fullRange;
        try {
            if (requestedRangeRaw != null) {
                List<ContentRange> requestedRanges = ContentRange.parseFromHeader(requestedRangeRaw, meta.getLength(), 1);
                if (!requestedRanges.isEmpty()) {
                    r = requestedRanges.get(0);
                }
            }
            long contentLength = (r.getEnd() - r.getStart() + 1);
            boolean useContentRange = !fullRange.equals(r);
            response.setContentType(meta.getMimeType());
            if (useContentRange) {
                response.setHeader("Content-Range", r.toString());
            }
            response.setContentLength((int) contentLength);
            response.setStatus(useContentRange ? 206 : 200);
            response.flushBuffer();
            if (sendContent) {
                try (OutputStream out = response.getOutputStream()) {
                    writeFromServiceToClient(r.getStart(), contentLength, binaryRes, out, mimeType);
                }
            }
        } catch (ContentRange.InvalidRangeException ex) {
            response.setHeader("Content-Range", "bytes */" + meta.getLength());
            response.sendError(416, "Requested range not satisfiable: " + ex.getMessage());
            return;
        }
    } catch (IOException ex) {
        log.warn("IOException in BinaryRequestHandler", ex);
        response.setStatus(500);
    } catch (ClientHandlerException | UniformInterfaceException ex) {
        log.error(null, ex);
        response.setStatus(500);
    }
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) OutputStream(java.io.OutputStream) WebResource(com.sun.jersey.api.client.WebResource) IOException(java.io.IOException) VaadinServletResponse(com.vaadin.server.VaadinServletResponse) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData)

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