示例#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;
  }
示例#2
0
  /**
   * Find the UI for the atmosphere resource, lock it and invoke the callback.
   *
   * @param resource the atmosphere resource for the current request
   * @param callback the push callback to call when a UI is found and locked
   */
  private void callWithUi(final AtmosphereResource resource, final PushEventCallback callback) {
    AtmosphereRequest req = resource.getRequest();
    VaadinServletRequest vaadinRequest = new VaadinServletRequest(req, service);
    VaadinSession session = null;

    service.requestStart(vaadinRequest, null);
    try {
      try {
        session = service.findVaadinSession(vaadinRequest);
      } catch (ServiceException e) {
        getLogger().log(Level.SEVERE, "Could not get session. This should never happen", e);
      } catch (SessionExpiredException e) {
        SystemMessages msg =
            service.getSystemMessages(
                ServletPortletHelper.findLocale(null, null, vaadinRequest), vaadinRequest);
        try {
          resource
              .getResponse()
              .getWriter()
              .write(
                  VaadinService.createCriticalNotificationJSON(
                      msg.getSessionExpiredCaption(),
                      msg.getSessionExpiredMessage(),
                      null,
                      msg.getSessionExpiredURL()));
        } catch (IOException e1) {
          getLogger().log(Level.WARNING, "Failed to notify client about unavailable session", e);
        }
        return;
      }

      session.lock();
      try {
        VaadinSession.setCurrent(session);
        // Sets UI.currentInstance
        final UI ui = service.findUI(vaadinRequest);
        if (ui == null) {
          // This a request through an already open push connection to
          // a UI which no longer exists.
          resource
              .getResponse()
              .getWriter()
              .write(UidlRequestHandler.getUINotFoundErrorJSON(service, vaadinRequest));
          // End the connection
          resource.resume();
          return;
        }

        callback.run(resource, ui);
      } catch (IOException e) {
        getLogger().log(Level.INFO, "An error occured while writing a push response", e);
      } finally {
        session.unlock();
      }
    } finally {
      service.requestEnd(vaadinRequest, null, session);
    }
  }
  @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;
  }