Skip to content

Package: OSGiServletContext

OSGiServletContext

nameinstructionbranchcomplexitylinemethod
OSGiServletContext(HttpContext, Logger)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getEventListeners()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getFilterChainFactory()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMimeType(String)
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getResource(String)
M: 24 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getResourceAsStream(String)
M: 54 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
installAuthFilter(HttpContext)
M: 30 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
unregisterAllFilters()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
unregisterFilter(Filter)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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 static java.text.MessageFormat.format;
20:
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.net.MalformedURLException;
24: import java.net.URL;
25: import java.util.EventListener;
26:
27: import org.glassfish.grizzly.http.util.MimeType;
28: import org.glassfish.grizzly.osgi.httpservice.util.Logger;
29: import org.glassfish.grizzly.servlet.FilterChainFactory;
30: import org.glassfish.grizzly.servlet.FilterRegistration;
31: import org.glassfish.grizzly.servlet.WebappContext;
32: import org.osgi.service.http.HttpContext;
33:
34: import jakarta.servlet.Filter;
35:
36: /**
37: * OSGi {@link WebappContext} integration.
38: *
39: * @author Hubert Iwaniuk
40: */
41: public class OSGiServletContext extends WebappContext {
42: /**
43: * {@link HttpContext} providing OSGi integration.
44: */
45: private final HttpContext httpContext;
46: private final Logger logger;
47:
48: // ------------------------------------------------------------ Constructors
49:
50: /**
51: * Default constructor.
52: *
53: * @param httpContext {@link org.osgi.service.http.HttpContext} to provide integration with OSGi.
54: * @param logger Logger util.
55: */
56: public OSGiServletContext(HttpContext httpContext, Logger logger) {
57: this.httpContext = httpContext;
58: this.logger = logger;
59: installAuthFilter(httpContext);
60: }
61:
62: // ---------------------------------------------------------- Public Methods
63:
64: /**
65: * OSGi integration. Uses {@link HttpContext#getResource(String)}.
66: * <p/>
67: * {@inheritDoc}
68: */
69: @Override
70: public URL getResource(String path) throws MalformedURLException {
71:• if (path == null || !path.startsWith("/")) {
72: throw new MalformedURLException(path);
73: }
74:
75: path = normalize(path);
76:• if (path == null) {
77: return null;
78: }
79:
80: return httpContext.getResource(path);
81: }
82:
83: /**
84: * OSGi integration. Uses {@link HttpContext#getResource(String)}.
85: * <p/>
86: * {@inheritDoc}
87: */
88: @Override
89: public InputStream getResourceAsStream(String path) {
90: path = normalize(path);
91:• if (path == null) {
92: return null;
93: }
94:
95: URL resource = httpContext.getResource(path);
96:• if (resource == null) {
97: logger.warn(format("Error getting resource ''{0}''. Message: {1}", path, "Can't locate resource."));
98: return null;
99: }
100:
101: try {
102: return resource.openStream();
103: } catch (IOException e) {
104: logger.warn(format("Error getting resource ''{0}''. Message: {1}", path, e.getMessage()));
105: }
106: return null;
107: }
108:
109: /**
110: * OSGi integration. Uses {@link HttpContext#getMimeType(String)}.
111: * <p/>
112: * {@inheritDoc}
113: */
114: @Override
115: public String getMimeType(String file) {
116: String mime = httpContext.getMimeType(file);
117:• if (mime == null) {
118: // if returned null, try figuring out by ourselfs.
119: mime = MimeType.getByFilename(file);
120: }
121: return mime;
122: }
123:
124: // ------------------------------------------------------- Protected Methods
125:
126: @Override
127: protected EventListener[] getEventListeners() {
128: return super.getEventListeners();
129: }
130:
131: @Override
132: protected FilterChainFactory getFilterChainFactory() {
133: return super.getFilterChainFactory();
134: }
135:
136: @Override
137: protected void unregisterFilter(final Filter f) {
138: super.unregisterFilter(f);
139: }
140:
141: @Override
142: protected void unregisterAllFilters() {
143: super.unregisterAllFilters();
144: }
145:
146: // --------------------------------------------------------- Private Methods
147:
148: private void installAuthFilter(HttpContext httpContext) {
149: final Filter f = new OSGiAuthFilter(httpContext);
150: try {
151: f.init(new OSGiFilterConfig(this));
152: } catch (Exception ignored) {
153: // won't happen
154: }
155: FilterRegistration registration = addFilter(Integer.toString(f.hashCode()), f);
156: registration.addMappingForUrlPatterns(null, "/*");
157: }
158: }