示例#1
0
  private void handlePartialPlayback(
      HttpServletRequest request,
      HttpServletResponse response,
      Vertex artifactVertex,
      String fileName,
      User user)
      throws IOException {
    String type = getRequiredParameter(request, "type");

    InputStream in;
    Long totalLength = null;
    long partialStart = 0;
    Long partialEnd = null;
    String range = request.getHeader("Range");

    if (range != null) {
      response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

      Matcher m = RANGE_PATTERN.matcher(range);
      if (m.matches()) {
        partialStart = Long.parseLong(m.group(1));
        if (m.group(2).length() > 0) {
          partialEnd = Long.parseLong(m.group(2));
        }
      }
    }

    response.setCharacterEncoding(null);
    response.setContentType(type);
    response.addHeader("Content-Disposition", "attachment; filename=" + fileName);

    StreamingPropertyValue mediaPropertyValue = getStreamingPropertyValue(artifactVertex, type);

    totalLength = mediaPropertyValue.getLength();
    in = mediaPropertyValue.getInputStream();

    if (partialEnd == null) {
      partialEnd = totalLength;
    }

    // Ensure that the last byte position is less than the instance-length
    partialEnd = Math.min(partialEnd, totalLength - 1);
    long partialLength = totalLength;

    if (range != null) {
      partialLength = partialEnd - partialStart + 1;
      response.addHeader(
          "Content-Range", "bytes " + partialStart + "-" + partialEnd + "/" + totalLength);
      if (partialStart > 0) {
        in.skip(partialStart);
      }
    }

    response.addHeader("Content-Length", "" + partialLength);

    OutputStream out = response.getOutputStream();
    copy(in, out, partialLength);

    response.flushBuffer();
  }
示例#2
0
  @Override
  public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain)
      throws Exception {
    boolean download = getOptionalParameter(request, "download") != null;
    boolean playback = getOptionalParameter(request, "playback") != null;

    User user = getUser(request);
    Authorizations authorizations = getAuthorizations(request, user);

    String graphVertexId = UrlUtils.urlDecode(getAttributeString(request, "graphVertexId"));

    Vertex artifactVertex = graph.getVertex(graphVertexId, authorizations);
    if (artifactVertex == null) {
      respondWithNotFound(response);
      return;
    }

    String fileName = LumifyProperties.FILE_NAME.getPropertyValue(artifactVertex);
    if (fileName == null) {
      fileName = LumifyProperties.TITLE.getPropertyValue(artifactVertex);
    }

    if (playback) {
      handlePartialPlayback(request, response, artifactVertex, fileName, user);
    } else {
      String mimeType = getMimeType(artifactVertex);
      response.setContentType(mimeType);
      setMaxAge(response, EXPIRES_1_HOUR);
      if (download) {
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
      } else {
        response.addHeader("Content-Disposition", "inline; filename=" + fileName);
      }

      StreamingPropertyValue rawValue = LumifyProperties.RAW.getPropertyValue(artifactVertex);
      if (rawValue == null) {
        LOGGER.warn("Could not find raw on artifact: %s", artifactVertex.getId().toString());
        respondWithNotFound(response);
        return;
      }
      InputStream in = rawValue.getInputStream();
      try {
        IOUtils.copy(in, response.getOutputStream());
      } finally {
        in.close();
      }
    }

    chain.next(request, response);
  }
 private void setIconProperty(
     Concept concept,
     File inDir,
     String glyphIconFileName,
     String propertyKey,
     Authorizations authorizations)
     throws IOException {
   if (glyphIconFileName != null) {
     File iconFile = new File(inDir, glyphIconFileName);
     if (!iconFile.exists()) {
       throw new RuntimeException("Could not find icon file: " + iconFile.toString());
     }
     InputStream iconFileIn = new FileInputStream(iconFile);
     try {
       StreamingPropertyValue value = new StreamingPropertyValue(iconFileIn, byte[].class);
       value.searchIndex(false);
       value.store(true);
       concept.setProperty(
           propertyKey, value, OntologyRepository.VISIBILITY.getVisibility(), authorizations);
     } finally {
       iconFileIn.close();
     }
   }
 }