Skip to content

Commit

Permalink
Enable winery REST and UI to be started in WineryUsingHttpServer
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Oct 17, 2017
1 parent 6768f14 commit b1f6e70
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ release.properties
**/rebel.xml
target/
node_modules/
winery-debug.log
12 changes: 12 additions & 0 deletions org.eclipse.winery.repository.rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
<version>9.4.2.v20170220</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.4.2.v20170220</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
Expand Down Expand Up @@ -309,6 +315,12 @@
<version>3.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<version>4.2</version>
<scope>test</scope>
</dependency>
<!-- end of test-only dependencies. Add runtime dependencies ABOVE the test depdendencies -->
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,99 @@
* and the Apache License 2.0 which both accompany this distribution,
* and are available at http://www.eclipse.org/legal/epl-v20.html
* and http://www.apache.org/licenses/LICENSE-2.0
*
* Contributors:
* Oliver Kopp - initial API and implementation
*******************************************************************************/
package org.eclipse.winery.repository.rest.resources;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EnumSet;

import javax.servlet.DispatcherType;

import org.eclipse.winery.repository.backend.IRepository;
import org.eclipse.winery.repository.backend.RepositoryFactory;
import org.eclipse.winery.repository.backend.filebased.FilebasedRepository;
import org.eclipse.winery.repository.rest.Prefs;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WineryUsingHttpServer {

public static Server createHttpServer(int port) throws IOException {
private static final Logger LOGGER = LoggerFactory.getLogger(WineryUsingHttpServer.class);

public static final int REPOSITORY_UI_PORT = 4200;


/**
* Creates a
*/
public static Server createHttpServer(int port) throws IOException {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/winery");
addServlet(context, "");

// TODO serve jsp - maybe https://www.eclipse.org/jetty/documentation/current/embedding-jetty.html could be of help
/*
String[] subs = {"imports", "servicetemplates", "nodetypes", "nodetypeimplementations", "relationshiptypes", "relationshiptypeimplementations", "requirementtypes", "capabilitytypes", "artifacttypes", "artifacttemplates", "policytypes", "policytempaltes", "admin", "API", "other", "test"};
Arrays.stream(subs).forEach(s -> {
});
*/

Server server = new Server(port);
server.setHandler(context);
return server;
}

/**
* Creates a server for the REST backend on URL localhost:8080/winery
*/
public static Server createHttpServer() throws IOException {
return createHttpServer(8080);
}

/**
* Starts the repository UI on port {@value #REPOSITORY_UI_PORT}
*/
public static Server createHttpServerForRepositoryUi() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(REPOSITORY_UI_PORT);
server.setConnectors(new Connector[]{connector});
ResourceHandler rh0 = new ResourceHandler();
ContextHandler context0 = new ContextHandler();
context0.setContextPath("/");
// Path indexHtmlPath = MavenTestingUtils.getProjectFilePath("../org.eclipse.winery.repository.ui/dist/index.html").getParent();
Path indexHtmlPath = MavenTestingUtils.getProjectFilePath("pom.xml").getParent().resolve("org.eclipse.winery.repository.ui").resolve("dist");
if (Files.exists(indexHtmlPath)) {
LOGGER.debug("Serving UI from " + indexHtmlPath.toString());
} else {
LOGGER.error("Path does not exist " + indexHtmlPath);
}
context0.setBaseResource(Resource.newResource(indexHtmlPath.toFile()));
context0.setHandler(rh0);
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[]{context0});

server.setHandler(contexts);

return server;
}

private static void addServlet(ServletContextHandler context, String s) {
// Add the filter, and then use the provided FilterHolder to configure it
FilterHolder cors = context.addFilter(CrossOriginFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
cors.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
cors.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,HEAD");
cors.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");

ServletHolder h = context.addServlet(com.sun.jersey.spi.container.servlet.ServletContainer.class, "/*");
h.setInitParameter("com.sun.jersey.config.property.packages", "org.eclipse.winery.repository.rest.resources");
h.setInitParameter("com.sun.jersey.config.feature.FilterForwardOn404", "false");
Expand All @@ -52,17 +106,31 @@ private static void addServlet(ServletContextHandler context, String s) {
h.setInitParameter("com.sun.jersey.config.feature.Redirect", "true");
h.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
h.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");

h.setInitOrder(1);
}

/**
* When in IntelliJ, /tmp/winery-repository is used. See /src/test/resources/winery.properties
*/
public static void main(String[] args) throws Exception {
// initialize repository
new Prefs(true);

Server server = createHttpServer();

server.start();

Server uiServer = createHttpServerForRepositoryUi();
uiServer.start();

IRepository repository = RepositoryFactory.getRepository();
if (repository instanceof FilebasedRepository) {
LOGGER.debug("Using path " + ((FilebasedRepository) repository).getRepositoryRoot());
} else {
LOGGER.debug("Repository is not filebased");
}

server.join();
uiServer.join();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class WineryRepositoryComponent implements OnInit {
}

ngOnInit() {
this.existService.check(backendBaseURL).subscribe(
this.existService.check(backendBaseURL + '/').subscribe(
data => {
this.isBackendAvailable = true;
this.loading = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ng-template #displayErrorMessage>
<br>
<div id="error-message">
<h2>Backend is not available!</h2>
<h3>Backend is not available</h3>
<button class="btn btn-primary" (click)="refresh();">Retry</button>
</div>
</ng-template>
Expand Down

0 comments on commit b1f6e70

Please sign in to comment.