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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations