Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7ee24a05fa5533801a18571c33ffe53fb7b55381 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
 * 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.ext;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.RuntimeDelegate;
import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.feature.Feature;
import org.apache.cxf.feature.LoggingFeature;
import org.apache.cxf.jaxrs.JAXRSBindingFactory;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
import org.apache.cxf.jaxrs.utils.ResourceUtils;
import org.apache.cxf.transport.common.gzip.GZIPFeature;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import org.eclipse.osee.authorization.admin.AuthorizationAdmin;
import org.eclipse.osee.jaxrs.JacksonFeature;
import org.eclipse.osee.jaxrs.OseeWebApplicationException;
import org.eclipse.osee.jaxrs.server.internal.JaxRsUtils;
import org.eclipse.osee.jaxrs.server.internal.JaxRsVisitable;
import org.eclipse.osee.jaxrs.server.internal.applications.AbstractJaxRsApplicationContainer;
import org.eclipse.osee.jaxrs.server.internal.applications.AbstractJaxRsContainer;
import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry.JaxRsContainer;
import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry.JaxRsContainerProvider;
import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsContainerProviderImpl;
import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsFactory;
import org.eclipse.osee.jaxrs.server.internal.exceptions.GenericExceptionMapper;
import org.eclipse.osee.jaxrs.server.internal.filters.SecurityContextFilter;
import org.eclipse.osee.jaxrs.server.internal.filters.SecurityContextProviderImpl;
import org.eclipse.osee.logger.Log;
import org.osgi.service.http.HttpService;

/**
 * @author Roberto E. Escobar
 */
public final class CxfJaxRsFactory implements JaxRsFactory {

   private static final String APACHE_CXF_LOGGER = "org.apache.cxf.Logger";
   private static final String DEFAULT_CXF_LOGGING_IMPL = "org.apache.cxf.common.logging.Slf4jLogger";

   private Log logger;
   private HttpService httpService;
   private AuthorizationAdmin authorizationAdmin;

   private List<Feature> features;
   private List<? extends Object> providers;
   private Map<String, Object> properties;
   private Map<Object, Object> extensionMappings;

   public void setLogger(Log logger) {
      this.logger = logger;
   }

   public void setHttpService(HttpService httpService) {
      this.httpService = httpService;
   }

   public void setAuthorizationAdmin(AuthorizationAdmin authorizationAdmin) {
      this.authorizationAdmin = authorizationAdmin;
   }

   public void start(Map<String, Object> props) {
      logger.debug("Starting [%s]...", getClass().getSimpleName());

      System.setProperty(APACHE_CXF_LOGGER, DEFAULT_CXF_LOGGING_IMPL);

      // Ensure CXF JAX-RS implementation is loaded 
      RuntimeDelegate runtimeDelegate = new org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl();
      RuntimeDelegate.setInstance(runtimeDelegate);

      SecurityContextProviderImpl provider = new SecurityContextProviderImpl(logger, authorizationAdmin);
      SecurityContextFilter filter = new SecurityContextFilter(provider);

      List<Object> providers = new ArrayList<Object>();
      WebApplicationExceptionMapper waem = new WebApplicationExceptionMapper();
      waem.setPrintStackTrace(true);
      waem.setAddMessageToResponse(true);

      providers.add(waem);
      providers.add(new GenericExceptionMapper(logger));
      providers.addAll(JacksonFeature.getProviders());
      providers.add(filter);
      this.providers = providers;

      List<Feature> features = new ArrayList<Feature>();
      LoggingFeature loggingFeature = new LoggingFeature();
      loggingFeature.setPrettyLogging(true);

      features.add(loggingFeature);
      features.add(new GZIPFeature());
      this.features = features;

      Map<Object, Object> extensionMappings = new HashMap<Object, Object>(4);
      extensionMappings.put("xml", MediaType.APPLICATION_XML);
      extensionMappings.put("json", MediaType.APPLICATION_JSON);
      extensionMappings.put("gzip", "application/gzip");
      extensionMappings.put("zip", "application/zip");
      this.extensionMappings = extensionMappings;
      this.properties = props;
   }

   public void stop() {
      RuntimeDelegate.setInstance(null);
      if (providers != null) {
         providers.clear();
         providers = null;
      }
      if (features != null) {
         features.clear();
         features = null;
      }
      if (extensionMappings != null) {
         extensionMappings.clear();
         extensionMappings = null;
      }
      properties = null;
   }

   private Map<Object, Object> getExtensionMappings() {
      return extensionMappings;
   }

   private List<? extends Object> getProviders() {
      return providers;
   }

   private List<Feature> getFeatures() {
      return features;
   }

   private Map<String, Object> getProperties() {
      return properties;
   }

   @Override
   public JaxRsContainerProvider newJaxRsContainerProvider(String contextName) {
      return new JaxRsContainerProviderImpl(this, contextName);
   }

   @Override
   public JaxRsContainer newJaxRsContainer(String contextName) {
      Dictionary<String, Object> props = new Hashtable<String, Object>();
      CxfJaxRsContainer container = new CxfJaxRsContainer(logger, httpService, props);
      container.setServletContext(contextName);
      logger.trace("Create - [%s]", container);
      return container;
   }

   private CxfJaxRsApplicationContainer newApplicationContainer(String applicationContext) {
      return new CxfJaxRsApplicationContainer(applicationContext);
   }

   private CXFNonSpringServlet newBaseJaxsRsServlet(JaxRsVisitable visitable) {
      return new CXFNonSpringServlet();
   }

   public Server newCxfServer(CXFNonSpringServlet servlet, String applicationPath, Application application) {
      String contextName = servlet.getServletName();
      Bus bus = servlet.getBus();
      if (bus == null) {
         throw new OseeWebApplicationException(Status.INTERNAL_SERVER_ERROR,
            "Error initializing [%s] for application [%s] - bus was null", contextName, application);
      }

      boolean ignoreApplicationPath = true;
      boolean staticSubresourceResolution = true;
      JAXRSServerFactoryBean bean =
         ResourceUtils.createApplication(application, ignoreApplicationPath, staticSubresourceResolution);

      if (JaxRsUtils.hasPath(applicationPath)) {
         String subAddress = JaxRsUtils.normalize(applicationPath);
         bean.setAddress(subAddress);
      }

      bean.setProviders(getProviders());
      bean.setFeatures(getFeatures());
      bean.setProperties(getProperties());
      bean.setExtensionMappings(getExtensionMappings());

      bean.setBindingId(JAXRSBindingFactory.JAXRS_BINDING_ID);
      bean.setTransportId("http://cxf.apache.org/transports/http");

      bean.setBus(bus);
      bean.setStart(false);

      Server server = bean.create();
      return server;
   }

   private final class CxfJaxRsContainer extends AbstractJaxRsContainer<CXFNonSpringServlet, CxfJaxRsApplicationContainer> {

      public CxfJaxRsContainer(Log logger, HttpService httpService, Dictionary<String, Object> props) {
         super(logger, httpService, props);
      }

      @Override
      protected CxfJaxRsApplicationContainer createApplicationContainer(String applicationContext) {
         return newApplicationContainer(applicationContext);
      }

      @Override
      protected CXFNonSpringServlet createBaseJaxsRsServlet(JaxRsVisitable visitable) {
         return newBaseJaxsRsServlet(visitable);
      }

      @Override
      protected void startContainer(CxfJaxRsApplicationContainer container) {
         CXFNonSpringServlet baseServlet = getBaseServlet();
         container.startContainer(baseServlet);
      }

      @Override
      protected void stopContainer(CxfJaxRsApplicationContainer container) {
         container.stopContainer();
      }

   };

   private final class CxfJaxRsApplicationContainer extends AbstractJaxRsApplicationContainer {

      private final AtomicBoolean isRegistered = new AtomicBoolean(false);
      private volatile Server server;

      public CxfJaxRsApplicationContainer(String applicationContext) {
         super(applicationContext);
      }

      public void startContainer(CXFNonSpringServlet servlet) {
         if (!isRegistered.getAndSet(true)) {
            Server newServer = newCxfServer(servlet, getApplicationContext(), getApplication());
            newServer.start();
            server = newServer;
         }
      }

      public void stopContainer() {
         if (isRegistered.getAndSet(false)) {
            if (server != null) {
               server.stop();
               server.destroy();
            }
         }
      }
   }

}

Back to the top