Skip to main content
summaryrefslogtreecommitdiffstats
blob: dfaa2acfb784a6e810f3007e4a5d426fc72bea45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2014 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.jaxrs.server.internal.resources;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;
import javax.annotation.Priority;
import javax.servlet.ServletContext;
import javax.ws.rs.Priorities;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.jaxrs.server.internal.JaxRsResourceManager;
import org.eclipse.osee.jaxrs.server.internal.JaxRsResourceManager.Resource;

/**
 * @author Roberto E. Escobar
 */
@PreMatching
@Priority(Priorities.USER)
@Provider
public class JaxRsStaticResourceRequestFilter implements ContainerRequestFilter {

   private static final String GET_METHOD = "GET";
   private static final String HEAD_METHOD = "HEAD";

   private JaxRsResourceManager manager;

   @Context
   private ServletContext servletContext;

   public void setJaxRsResourceManager(JaxRsResourceManager manager) {
      this.manager = manager;
   }

   @Override
   public void filter(ContainerRequestContext requestContext) throws IOException {
      Request request = requestContext.getRequest();
      String method = request.getMethod();
      if (GET_METHOD.equals(method) || HEAD_METHOD.equals(method)) {
         UriInfo uriInfo = requestContext.getUriInfo();
         String path = uriInfo.getAbsolutePath().getPath();
         Resource resource = manager.getResource(path);
         if (resource != null) {
            MultivaluedMap<String, String> headers = requestContext.getHeaders();
            List<MediaType> acceptableMediaTypes = requestContext.getAcceptableMediaTypes();
            Response response = newResponse(servletContext, headers, acceptableMediaTypes, path, resource);
            requestContext.abortWith(response);
         }
      }
   }

   private Response newResponse(ServletContext servletContext, MultivaluedMap<String, String> headers, List<MediaType> acceptableMediaTypes, String toMatch, Resource resource) throws IOException {
      final URLConnection connection = resource.getUrl().openConnection();

      long lastModified = connection.getLastModified();
      int contentLength = connection.getContentLength();

      String etag = null;
      if (lastModified != -1 && contentLength != -1) {
         etag = String.format("W/\"%s-%s\"", contentLength, lastModified);
      }

      String ifNoneMatch = headers.getFirst(HttpHeaders.IF_NONE_MATCH);
      if (ifNoneMatch != null && etag != null && ifNoneMatch.indexOf(etag) != -1) {
         return Response.notModified().build();
      }

      ResponseBuilder builder = Response.ok();
      if (contentLength != -1) {
         builder.header(HttpHeaders.CONTENT_LENGTH, contentLength);
      }

      String contentType = null;
      if (servletContext != null) {
         contentType = servletContext.getMimeType(toMatch);
      }

      if (contentType != null) {
         builder.type(contentType);
      } else {
         if (!acceptableMediaTypes.isEmpty()) {
            builder.type(acceptableMediaTypes.get(0));
         }
      }

      if (lastModified > 0) {
         builder.lastModified(new Date(lastModified));
      }

      if (etag != null) {
         builder.tag(etag);
      }

      StreamingOutput output = new StreamingOutput() {

         @Override
         public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            InputStream inputStream = null;
            try {
               inputStream = connection.getInputStream();
               Lib.inputStreamToOutputStream(inputStream, outputStream);
            } finally {
               Lib.close(inputStream);
            }
         }
      };
      builder.entity(output);
      return builder.build();
   }

}

Back to the top