You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApplicationResourceHandler.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2011 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.io.IOException;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. import javax.servlet.http.HttpServletResponse;
  21. import com.vaadin.Application;
  22. public class ApplicationResourceHandler implements RequestHandler {
  23. private static final Pattern APP_RESOURCE_PATTERN = Pattern
  24. .compile("^/?APP/(\\d+)/.*");
  25. @Override
  26. public boolean handleRequest(Application application,
  27. WrappedRequest request, WrappedResponse response)
  28. throws IOException {
  29. // Check for application resources
  30. String requestPath = request.getRequestPathInfo();
  31. if (requestPath == null) {
  32. return false;
  33. }
  34. Matcher resourceMatcher = APP_RESOURCE_PATTERN.matcher(requestPath);
  35. if (resourceMatcher.matches()) {
  36. ApplicationResource resource = application
  37. .getResource(resourceMatcher.group(1));
  38. if (resource != null) {
  39. DownloadStream stream = resource.getStream();
  40. if (stream != null) {
  41. stream.setCacheTime(resource.getCacheTime());
  42. stream.writeTo(response);
  43. return true;
  44. }
  45. }
  46. // We get here if the url looks like an application resource but no
  47. // resource can be served
  48. response.sendError(HttpServletResponse.SC_NOT_FOUND,
  49. request.getRequestPathInfo() + " can not be found");
  50. return true;
  51. }
  52. return false;
  53. }
  54. }