/
ArtificerDevServer.java
367 lines (330 loc) · 16.5 KB
/
ArtificerDevServer.java
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright 2013 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.artificer.devsvr;
import org.apache.commons.io.IOUtils;
import org.artificer.ui.server.filters.LocaleFilter;
import org.artificer.ui.server.servlets.ArtifactDownloadServlet;
import org.artificer.ui.server.servlets.ArtifactUploadServlet;
import org.artificer.ui.server.servlets.KeyCloakLogoutServlet;
import org.artificer.ui.server.servlets.OntologyDownloadServlet;
import org.artificer.ui.server.servlets.OntologyUploadServlet;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.SecurityHandler;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.server.UserIdentity;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.security.Credential;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
import org.oasis_open.docs.s_ramp.ns.s_ramp_v1.BaseArtifactType;
import org.overlord.commons.dev.server.DevServerEnvironment;
import org.overlord.commons.dev.server.ErraiDevServer;
import org.overlord.commons.dev.server.MultiDefaultServlet;
import org.overlord.commons.dev.server.discovery.ErraiWebAppModuleFromMavenDiscoveryStrategy;
import org.overlord.commons.dev.server.discovery.WebAppModuleFromIDEDiscoveryStrategy;
import org.overlord.commons.gwt.server.filters.GWTCacheControlFilter;
import org.overlord.commons.gwt.server.filters.ResourceCacheControlFilter;
import org.artificer.atom.err.ArtificerAtomException;
import org.artificer.client.ArtificerAtomApiClient;
import org.artificer.client.ArtificerClientException;
import org.artificer.common.ArtifactType;
import org.artificer.common.ArtificerModelUtils;
import org.artificer.repository.jcr.JCRRepository;
import org.artificer.repository.jcr.filter.ServletCredentialsFilter;
import org.artificer.server.ArtificerLifeCycle;
import org.artificer.server.atom.services.ArtificerApplication;
import org.artificer.server.filters.MavenRepositoryAuthFilter;
import org.artificer.server.mvn.services.MavenFacadeServlet;
import org.artificer.ui.client.shared.beans.ArtifactSummaryBean;
import org.artificer.ui.server.api.KeycloakBearerTokenAuthenticationProvider;
import javax.security.auth.Subject;
import javax.servlet.DispatcherType;
import java.io.InputStream;
import java.security.Principal;
import java.util.EnumSet;
/**
* A dev server for s-ramp.
* @author eric.wittmann@redhat.com
*/
public class ArtificerDevServer extends ErraiDevServer {
/**
* Main entry point.
* @param args
*/
public static void main(String [] args) throws Exception {
ArtificerDevServer devServer = new ArtificerDevServer(args);
devServer.enableDebug();
devServer.go();
}
/**
* Constructor.
* @param args
*/
public ArtificerDevServer(String[] args) {
super(args);
}
/**
* @see org.overlord.commons.dev.server.ErraiDevServer#getErraiModuleId()
*/
@Override
protected String getErraiModuleId() {
return "artificer-ui";
}
/**
* @see org.overlord.commons.dev.server.DevServer#preConfig()
*/
@Override
protected void preConfig() {
// Use an in-memory config for s-ramp
System.setProperty("artificer.modeshape.config.url", "classpath://" + JCRRepository.class.getName()
+ "/META-INF/modeshape-configs/inmemory-artificer-config.json");
// Authentication provider
System.setProperty("artificer-ui.atom-api.authentication.provider", KeycloakBearerTokenAuthenticationProvider.class.getName());
// Don't do any resource caching!
System.setProperty("overlord.resource-caching.disabled", "true");
System.setProperty("artificer.config.events.jms.topics", "artificer/events/topic");
}
/**
* @see org.overlord.commons.dev.server.DevServer#createDevEnvironment()
*/
@Override
protected DevServerEnvironment createDevEnvironment() {
return new ArtificerDevServerEnvironment(args);
}
/**
* @see org.overlord.commons.dev.server.DevServer#addModules(org.overlord.commons.dev.server.DevServerEnvironment)
*/
@Override
protected void addModules(DevServerEnvironment environment) {
environment.addModule("artificer-ui",
new WebAppModuleFromIDEDiscoveryStrategy(ArtifactSummaryBean.class),
new ErraiWebAppModuleFromMavenDiscoveryStrategy(ArtifactSummaryBean.class));
}
/**
* @see org.overlord.commons.dev.server.DevServer#addModulesToJetty(org.overlord.commons.dev.server.DevServerEnvironment, org.eclipse.jetty.server.handler.ContextHandlerCollection)
*/
@Override
protected void addModulesToJetty(DevServerEnvironment environment, ContextHandlerCollection handlers) throws Exception {
super.addModulesToJetty(environment, handlers);
/* *********
* S-RAMP UI
* ********* */
ServletContextHandler artificerUI = new ServletContextHandler(ServletContextHandler.SESSIONS);
artificerUI.setSecurityHandler(createSecurityHandler(true));
artificerUI.setContextPath("/artificer-ui");
artificerUI.setWelcomeFiles(new String[]{"index.html"});
artificerUI.setResourceBase(environment.getModuleDir("artificer-ui").getCanonicalPath());
artificerUI.addFilter(GWTCacheControlFilter.class, "/app/*", EnumSet.of(DispatcherType.REQUEST));
artificerUI.addFilter(GWTCacheControlFilter.class, "/rest/*", EnumSet.of(DispatcherType.REQUEST));
artificerUI.addFilter(GWTCacheControlFilter.class, "/", EnumSet.of(DispatcherType.REQUEST));
artificerUI.addFilter(GWTCacheControlFilter.class, "*.html", EnumSet.of(DispatcherType.REQUEST));
artificerUI.addFilter(ResourceCacheControlFilter.class, "/css/*", EnumSet.of(DispatcherType.REQUEST));
artificerUI.addFilter(ResourceCacheControlFilter.class, "/images/*", EnumSet.of(DispatcherType.REQUEST));
artificerUI.addFilter(ResourceCacheControlFilter.class, "/js/*", EnumSet.of(DispatcherType.REQUEST));
artificerUI.addFilter(LocaleFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
// Servlets
artificerUI.addServlet(new ServletHolder(ArtifactUploadServlet.class), "/app/services/artifactUpload");
artificerUI.addServlet(new ServletHolder(ArtifactDownloadServlet.class), "/app/services/artifactDownload");
artificerUI.addServlet(new ServletHolder(OntologyUploadServlet.class), "/app/services/ontologyUpload");
artificerUI.addServlet(new ServletHolder(OntologyDownloadServlet.class), "/app/services/ontologyDownload");
artificerUI.addServlet(new ServletHolder(KeyCloakLogoutServlet.class), "/app/services/logout");
ServletHolder resteasyUIServlet = new ServletHolder(new HttpServletDispatcher());
resteasyUIServlet.setInitParameter("javax.ws.rs.Application", JettyArtificerApplication.class.getName());
resteasyUIServlet.setInitParameter("resteasy.servlet.mapping.prefix", "/rest");
artificerUI.addServlet(resteasyUIServlet, "/rest/*");
// File resources
ServletHolder resources = new ServletHolder(new MultiDefaultServlet());
resources.setInitParameter("resourceBase", "/");
resources.setInitParameter("resourceBases", environment.getModuleDir("artificer-ui").getCanonicalPath());
resources.setInitParameter("dirAllowed", "true");
resources.setInitParameter("pathInfoOnly", "false");
String[] fileTypes = new String[] { "html", "js", "css", "png", "gif" };
for (String fileType : fileTypes) {
artificerUI.addServlet(resources, "*." + fileType);
}
/* *************
* S-RAMP server
* ************* */
ServletContextHandler artificerServer = new ServletContextHandler(ServletContextHandler.SESSIONS);
artificerServer.setSecurityHandler(createSecurityHandler(false));
artificerServer.setContextPath("/artificer-server");
artificerServer.addEventListener(new ArtificerLifeCycle());
ServletHolder resteasyServerServlet = new ServletHolder(new HttpServletDispatcher());
resteasyServerServlet.setInitParameter("javax.ws.rs.Application", ArtificerApplication.class.getName());
artificerServer.addServlet(resteasyServerServlet, "/s-ramp/*");
//maven repository servlet:
ServletHolder mvnServlet = new ServletHolder(new MavenFacadeServlet());
artificerServer.addServlet(mvnServlet, "/maven/repository/*");
artificerServer.addServlet(mvnServlet, "/maven/repository");
// TODO enable JSP support to test the repository listing
artificerServer.addFilter(BasicAuthFilter.class, "/s-ramp/*", EnumSet.of(DispatcherType.REQUEST))
.setInitParameter("allowedIssuers", "/artificer-ui,/dtgov,/dtgov-ui");
artificerServer.addFilter(MavenRepositoryAuthFilter.class, "/maven/repository/*", EnumSet.of(DispatcherType.REQUEST))
.setInitParameter("allowedIssuers", "/artificer-ui,/dtgov,/dtgov-ui");
artificerServer.addFilter(org.artificer.server.filters.LocaleFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
artificerServer.addFilter(ServletCredentialsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
// Add to handlers
handlers.addHandler(artificerUI);
handlers.addHandler(artificerServer);
}
/**
* @return a security handler
*/
private SecurityHandler createSecurityHandler(boolean forUI) {
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[]{"user"});
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
csh.setSessionRenewedOnAuthentication(false);
csh.setAuthenticator(new BasicAuthenticator());
csh.setRealmName("artificer");
if (forUI) {
csh.addConstraintMapping(cm);
}
csh.setLoginService(new HashLoginService() {
@Override
public UserIdentity login(String username, Object credentials) {
Credential credential = (credentials instanceof Credential) ? (Credential) credentials
: Credential.getCredential(credentials.toString());
Principal userPrincipal = new KnownUser(username, credential);
Subject subject = new Subject();
subject.getPrincipals().add(userPrincipal);
subject.getPrivateCredentials().add(credential);
String[] roles = new String[] { "user","readonly","readwrite","admin" };
for (String role : roles) {
subject.getPrincipals().add(new RolePrincipal(role));
}
subject.setReadOnly();
return _identityService.newUserIdentity(subject, userPrincipal, roles);
}
});
return csh;
}
/**
* @see org.overlord.commons.dev.server.DevServer#postStart(org.overlord.commons.dev.server.DevServerEnvironment)
*/
@Override
protected void postStart(DevServerEnvironment environment) throws Exception {
System.out.println("---------- Seeding the Repository ---------------");
ArtificerAtomApiClient client = new ArtificerAtomApiClient("http://localhost:"+serverPort()+"/artificer-server", "seeder", "seeder", true);
String seedType = System.getProperty("artificer-dev-server.seed-type", "none");
if ("switchyard".equals(seedType)) {
doSwitchYardSeed(client);
} else if ("standard".equals(seedType)) {
doStandardSeed(client);
} else {
// no seeding
}
System.out.println("---------- DONE ---------------");
System.out.println("Now try: \n http://localhost:"+serverPort()+"/artificer-ui/index.html");
System.out.println("---------------------------------");
}
/**
* @param client
* @throws org.artificer.atom.err.ArtificerAtomException
* @throws org.artificer.client.ArtificerClientException
*/
private void doStandardSeed(ArtificerAtomApiClient client) throws ArtificerClientException, ArtificerAtomException {
InputStream is = null;
// Ontology #1
try {
is = ArtificerDevServer.class.getResourceAsStream("colors.owl.xml");
client.uploadOntology(is);
System.out.println("Ontology 1 added");
} finally {
IOUtils.closeQuietly(is);
}
// Ontology #2
try {
is = ArtificerDevServer.class.getResourceAsStream("regional.owl.xml");
client.uploadOntology(is);
System.out.println("Ontology 2 added");
} finally {
IOUtils.closeQuietly(is);
}
// PDF Document
try {
is = ArtificerDevServer.class.getResourceAsStream("sample.pdf");
BaseArtifactType artifact = client.uploadArtifact(ArtifactType.Document(), is, "sample.pdf");
artifact.setDescription("This is just a sample PDF file that is included in the dev server so that we have some content when we start up.");
artifact.setVersion("1.0");
client.updateArtifactMetaData(artifact);
System.out.println("PDF added");
} finally {
IOUtils.closeQuietly(is);
}
// XML Document
try {
is = ArtificerDevServer.class.getResourceAsStream("order.xml");
BaseArtifactType artifact = client.uploadArtifact(ArtifactType.XmlDocument(), is, "order.xml");
artifact.getClassifiedBy().add("http://www.example.org/colors.owl#Blue");
ArtificerModelUtils.setCustomProperty(artifact, "foo", "bar");
ArtificerModelUtils.setCustomProperty(artifact, "angle", "obtuse");
client.updateArtifactMetaData(artifact);
System.out.println("XML file added");
} finally {
IOUtils.closeQuietly(is);
}
// WSDL Document
try {
is = ArtificerDevServer.class.getResourceAsStream("deriver.wsdl");
BaseArtifactType artifact = client.uploadArtifact(ArtifactType.WsdlDocument(), is, "deriver.wsdl");
artifact.getClassifiedBy().add("http://www.example.org/colors.owl#Red");
artifact.getClassifiedBy().add("http://www.example.org/regional.owl#Asia");
client.updateArtifactMetaData(artifact);
System.out.println("WSDL added");
} finally {
IOUtils.closeQuietly(is);
}
}
/**
* @param client
*/
private void doSwitchYardSeed(ArtificerAtomApiClient client) throws Exception {
// Upload the artifacts jar
InputStream artifactsIS = this.getClass().getResourceAsStream("artifacts.jar");
try {
client.uploadArtifact(artifactsIS, "artifacts.jar");
System.out.println("Added SwitchYard app (artifacts.jar)");
} finally {
IOUtils.closeQuietly(artifactsIS);
}
// Upload the order consumer jar
InputStream orderConsumerIS = this.getClass().getResourceAsStream("order-consumer.jar");
try {
client.uploadArtifact(orderConsumerIS, "order-consumer.jar");
System.out.println("Added SwitchYard app (order-consumer.jar)");
} finally {
IOUtils.closeQuietly(artifactsIS);
}
// Upload the order service jar
InputStream orderServiceIS = this.getClass().getResourceAsStream("order-service.jar");
try {
client.uploadArtifact(orderServiceIS, "order-service.jar");
System.out.println("Added SwitchYard app (order-service.jar)");
} finally {
IOUtils.closeQuietly(artifactsIS);
}
}
}