Beispiel #1
0
  @Override
  public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path)
      throws IOException {
    Matcher matcher = Pattern.compile("(\\d+)(/.*)?").matcher(path);
    if (!matcher.matches()) {
      return super.handleConnectorRequest(request, response, path);
    }

    DownloadStream stream;

    VaadinSession session = getSession();
    session.lock();
    try {
      List<URLReference> sources = getState().sources;

      int sourceIndex = Integer.parseInt(matcher.group(1));

      if (sourceIndex < 0 || sourceIndex >= sources.size()) {
        getLogger().log(Level.WARNING, "Requested source index {0} is out of bounds", sourceIndex);
        return false;
      }

      URLReference reference = sources.get(sourceIndex);
      ConnectorResource resource = (ConnectorResource) ResourceReference.getResource(reference);
      stream = resource.getStream();
    } finally {
      session.unlock();
    }

    stream.writeResponse(request, response);
    return true;
  }
  @Override
  public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path)
      throws IOException {

    if (!path.matches("dl(/.*)?")) {
      // Ignore if it isn't for us
      return false;
    }
    VaadinSession session = getSession();

    session.lock();
    AdvancedFileDownloader.this.fireEvent();

    DownloadStream stream;

    try {
      Resource resource = getFileDownloadResource();
      if (!(resource instanceof ConnectorResource)) {
        return false;
      }
      stream = ((ConnectorResource) resource).getStream();

      if (stream.getParameter("Content-Disposition") == null) {
        // Content-Disposition: attachment generally forces download
        stream.setParameter(
            "Content-Disposition", "attachment; filename=\"" + stream.getFileName() + "\"");
      }

      // Content-Type to block eager browser plug-ins from hijacking
      // the file
      if (isOverrideContentType()) {
        stream.setContentType("application/octet-stream;charset=UTF-8");
      }

    } finally {
      session.unlock();
    }
    stream.writeResponse(request, response);
    return true;
  }