Skip to content

Package: OSGiResourceHandler

OSGiResourceHandler

nameinstructionbranchcomplexitylinemethod
OSGiResourceHandler(String, String, HttpContext, OSGiServletContext, Logger)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
authenticate(Request, Response, OSGiServletContext)
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getProcessingLock()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getRemovalLock()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
service(Request, Response)
M: 127 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 37 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.glassfish.grizzly.osgi.httpservice;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.OutputStream;
22: import java.net.URL;
23: import java.net.URLConnection;
24: import java.util.concurrent.locks.ReentrantReadWriteLock;
25:
26: import org.glassfish.grizzly.http.server.HttpHandler;
27: import org.glassfish.grizzly.http.server.Request;
28: import org.glassfish.grizzly.http.server.Response;
29: import org.glassfish.grizzly.http.util.MimeType;
30: import org.glassfish.grizzly.osgi.httpservice.util.Logger;
31: import org.glassfish.grizzly.servlet.HttpServletRequestImpl;
32: import org.glassfish.grizzly.servlet.HttpServletResponseImpl;
33: import org.osgi.service.http.HttpContext;
34:
35: /**
36: * OSGi Resource {@link HttpHandler}.
37: * <p/>
38: * OSGi Resource registration integration.
39: *
40: * @author Hubert Iwaniuk
41: */
42: public class OSGiResourceHandler extends HttpHandler implements OSGiHandler {
43: private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
44: private final String alias;
45: private final String prefix;
46: private final HttpContext httpContext;
47: private final OSGiServletContext servletContext;
48: private final Logger logger;
49:
50: /**
51: * Default constructor.
52: *
53: * @param alias Registered under this alias.
54: * @param prefix Internal prefix.
55: * @param httpContext Backing {@link org.osgi.service.http.HttpContext}.
56: * @param logger Logger utility.
57: */
58: public OSGiResourceHandler(String alias, String prefix, HttpContext httpContext, OSGiServletContext servletContext, Logger logger) {
59: super();
60: // noinspection AccessingNonPublicFieldOfAnotherObject
61: // super.commitErrorResponse = false;
62: this.alias = alias;
63: this.prefix = prefix;
64: this.httpContext = httpContext;
65: this.servletContext = servletContext;
66: this.logger = logger;
67: }
68:
69: /**
70: * {@inheritDoc}
71: */
72: @Override
73: public void service(Request request, Response response) throws Exception {
74: String requestURI = request.getDecodedRequestURI();
75: logger.debug("OSGiResourceHandler requestURI: " + requestURI);
76: String path = requestURI.replaceFirst(alias, prefix);
77: try {
78: // authentication
79:• if (!authenticate(request, response, servletContext)) {
80: logger.debug("OSGiResourceHandler Request not authenticated (" + requestURI + ").");
81: return;
82: }
83: } catch (IOException e) {
84: logger.warn("Error while authenticating request: " + request, e);
85: }
86:
87: // find resource
88: URL resource = httpContext.getResource(path);
89:• if (resource == null) {
90: logger.debug("OSGiResourceHandler \'" + alias + "\' Haven't found '" + path + "'.");
91: response.setStatus(404);
92: return;
93: } else {
94: response.setStatus(200);
95: }
96:
97: // MIME handling
98: String mime = httpContext.getMimeType(path);
99:• if (mime == null) {
100: mime = MimeType.getByFilename(path);
101: }
102:• if (mime != null) {
103: response.setContentType(mime);
104: }
105:
106: try {
107: final URLConnection urlConnection = resource.openConnection();
108: final int length = urlConnection.getContentLength();
109: final InputStream is = urlConnection.getInputStream();
110: final OutputStream os = response.getOutputStream();
111:
112: byte buff[] = new byte[1024 * 8];
113: int read, total = 0;
114:• while ((read = is.read(buff)) != -1) {
115: total += read;
116: os.write(buff, 0, read);
117: }
118: os.flush();
119: response.finish();
120:• if (total != length) {
121: logger.warn("Was supposed to send " + length + ", but sent " + total);
122: }
123: } catch (IOException e) {
124: logger.warn("", e);
125: }
126: }
127:
128: /**
129: * Checks authentication.
130: * <p/>
131: * Calls {@link HttpContext#handleSecurity} to authenticate.
132: *
133: * @param request Request to authenticate.
134: * @param response Response to populate if authentication not performed but needed.
135: * @param servletContext Context needed for proper HttpServletRequest creation.
136: * @return <code>true</code> if authenticated and can proceed with processing, else <code>false</code>.
137: * @throws IOException Propagate exception thrown by {@link HttpContext#handleSecurity}.
138: */
139: private boolean authenticate(Request request, Response response, OSGiServletContext servletContext) throws IOException {
140:
141: HttpServletRequestImpl servletRequest = new OSGiHttpServletRequest(servletContext);
142: HttpServletResponseImpl servletResponse = HttpServletResponseImpl.create();
143:
144: servletResponse.initialize(response, servletRequest);
145: servletRequest.initialize(request, servletResponse, servletContext);
146:
147: return httpContext.handleSecurity(servletRequest, servletResponse);
148: }
149:
150: /**
151: * {@inheritDoc}
152: */
153: @Override
154: public ReentrantReadWriteLock.ReadLock getProcessingLock() {
155: return lock.readLock();
156: }
157:
158: /**
159: * {@inheritDoc}
160: */
161: @Override
162: public ReentrantReadWriteLock.WriteLock getRemovalLock() {
163: return lock.writeLock();
164: }
165:
166: private static class OSGiHttpServletRequest extends HttpServletRequestImpl {
167:
168: public OSGiHttpServletRequest(OSGiServletContext context) throws IOException {
169: super();
170: setContextImpl(context);
171: }
172: }
173: }