コード例 #1
0
 protected void setRespondCommonHeaders(
     Response response, Resource resource, Response.Status status, Auth auth) {
   response.setStatus(status);
   response.setNonStandardHeader("Server", "milton.io-" + miltonVerson);
   response.setDateHeader(new Date());
   response.setNonStandardHeader("Accept-Ranges", "bytes");
   String etag = eTagGenerator.generateEtag(resource);
   if (etag != null) {
     response.setEtag(etag);
   }
 }
コード例 #2
0
    @Override
    public void sendContent(
        OutputStream out, Range range, Map<String, String> params, String contentType)
        throws IOException, NotAuthorizedException, BadRequestException, NotFoundException {
      MediaMetaData mmd = MediaMetaData.find(rPrimary.getHash(), SessionManager.session());
      if (mmd != null) {
        Integer durationSecs = mmd.getDurationSecs();
        if (durationSecs != null) {
          Response resp = HttpManager.response();
          if (resp != null) {
            System.out.println("set duration header: " + durationSecs);
            resp.setNonStandardHeader("X-Content-Duration", durationSecs.toString());
          }
        }
      } else {
        System.out.println("no metadata for: " + rPrimary.getHash());
      }
      try {
        boolean force = params.containsKey("force");
        if (altFormat == null || force) {
          // hack start
          if (params.containsKey("args")) {
            List<String> args = new ArrayList<>();
            for (String s : params.get("args").split(",")) {
              args.add(s);
            }
            String[] arr = new String[args.size()];
            args.toArray(arr);
            formatSpec.setConverterArgs(arr);
            System.out.println("set args: " + arr);
          }

          // hack end
          System.out.println("generate: " + getName());
          GenerateJob j =
              altFormatGenerator.getOrEnqueueJob(
                  rPrimary.getHash(), rPrimary.getName(), formatSpec);
          System.out.println("got job: " + j);

          // Wait until the file exists
          int cnt = 0;
          System.out.println("check if exists...");
          while (!j.getDestFile().exists() && !j.done()) {
            cnt++;
            System.out.println(
                "sleep..." + cnt + " .. " + j.getDestFile().exists() + " - " + j.done());
            doSleep(cnt++, 200, 70);
          }
          System.out.println("finished sleepy check");
          if (!j.getDestFile().exists()) {
            throw new RuntimeException(
                "Job did not create a destination file: " + j.getDestFile().getAbsolutePath());
          }
          System.out.println(
              "use dest file: "
                  + j.getDestFile().getAbsolutePath()
                  + " size: "
                  + j.getDestFile().length());

          FileInputStream fin = new FileInputStream(j.getDestFile());
          byte[] buf = new byte[1024];
          System.out.println("send file...");
          // Read the file until the job is done, or we run out of bytes
          int s = fin.read(buf);
          System.out.println("send file... " + s);
          long bytes = 0;
          while (!j.done() || s > 0) {
            if (s < 0) { // no bytes available, but job is not done, so wait
              System.out.println("sleep...");
              doSleep(100);
            } else {
              System.out.println("write bytes: " + s);
              bytes += s;
              out.write(buf, 0, s);
            }
            s = fin.read(buf);
          }
          System.out.println("finished sending file: " + bytes);
        } else {
          System.out.println("using pre-existing al-format");
          Combiner combiner = new Combiner();
          List<String> fanoutCrcs = getFanout().getHashes();
          combiner.combine(fanoutCrcs, hashStore, blobStore, out);
          out.flush();
        }
      } catch (Throwable e) {
        log.error("Exception sending content", e);
        throw new IOException("Exception sending content");
      }
    }