Selaa lähdekoodia

Running embedded Jetty 10 with rest

Lukas Cerny 4 vuotta sitten
vanhempi
commit
036fa33d86

+ 106 - 20
pom.xml

@@ -2,7 +2,7 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>cz.hsrs.maplog</groupId>
     <artifactId>DBService</artifactId>
-    <packaging>war</packaging>
+    <packaging>jar</packaging>
     <version>1.3.5-SNAPSHOT</version>
     <name>dbservice Maven Webapp</name>
     <url>http://maven.apache.org</url>
@@ -24,6 +24,38 @@
         </testResources>
         <plugins>
             <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-assembly-plugin</artifactId>
+                <version>3.3.0</version>
+                <configuration>
+                    <descriptorRefs>
+                        <descriptorRef>jar-with-dependencies</descriptorRef>
+                    </descriptorRefs>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>assemble-all</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>single</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addClasspath>true</addClasspath>
+                            <mainClass>cz.hsrs.main.StartJetty</mainClass>
+                        </manifest>
+                    </archive>
+                </configuration>
+            </plugin>
+
+            <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>buildnumber-maven-plugin</artifactId>
                 <version>1.3</version>
@@ -138,36 +170,90 @@
     
 <!-- JETTY -->
         <dependency>
-            <groupId>org.mortbay.jetty</groupId>
-            <artifactId>jetty</artifactId>
-            <version>${jetty.version}</version>
-            <scope>provided</scope>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-server</artifactId>
+            <version>10.0.2</version>
         </dependency>
         <dependency>
-            <groupId>org.mortbay.jetty</groupId>
-            <artifactId>jetty-servlet-tester</artifactId>
-            <version>${jetty.version}</version>
-            <scope>test</scope>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-servlet</artifactId>
+            <version>10.0.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-webapp</artifactId>
+            <version>10.0.2</version>
         </dependency>
+
         <dependency>
             <groupId>org.mortbay.jetty</groupId>
             <artifactId>jetty-util</artifactId>
-            <version>${jetty.version}</version>
-            <scope>provided</scope>
+            <version>6.1.26</version>
         </dependency>
+
         <dependency>
-            <groupId>org.mortbay.jetty</groupId>
-            <artifactId>jetty-management</artifactId>
-            <version>${jetty.version}</version>
-            <scope>provided</scope>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>4.0.1</version>
         </dependency>
+
         <dependency>
-            <groupId>org.mortbay.jetty</groupId>
-            <artifactId>jsp-2.1-jetty</artifactId>
-            <version>${jetty.version}</version>
-            <scope>provided</scope>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
+            <version>2.4.0-b180830.0359</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>2.0.0-alpha1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-jdk14</artifactId>
+            <version>2.0.0-alpha1</version>
+            <scope>runtime</scope>
         </dependency>
-<!-- JETTY -->    
+
+
+
+
+
+
+        <!--        <dependency>-->
+<!--            <groupId>org.mortbay.jetty</groupId>-->
+<!--            <artifactId>jetty</artifactId>-->
+<!--            <version>${jetty.version}</version>-->
+<!--            <scope>provided</scope>-->
+<!--        </dependency>-->
+<!--        <dependency>-->
+<!--            <groupId>org.mortbay.jetty</groupId>-->
+<!--            <artifactId>jetty-servlet-tester</artifactId>-->
+<!--            <version>${jetty.version}</version>-->
+<!--            <scope>test</scope>-->
+<!--        </dependency>-->
+<!--        <dependency>-->
+<!--            <groupId>org.mortbay.jetty</groupId>-->
+<!--            <artifactId>jetty-util</artifactId>-->
+<!--            <version>${jetty.version}</version>-->
+<!--            <scope>provided</scope>-->
+<!--        </dependency>-->
+<!--        <dependency>-->
+<!--            <groupId>org.mortbay.jetty</groupId>-->
+<!--            <artifactId>jetty-management</artifactId>-->
+<!--            <version>${jetty.version}</version>-->
+<!--            <scope>provided</scope>-->
+<!--        </dependency>-->
+<!--        <dependency>-->
+<!--            <groupId>org.mortbay.jetty</groupId>-->
+<!--            <artifactId>jsp-2.1-jetty</artifactId>-->
+<!--            <version>${jetty.version}</version>-->
+<!--            <scope>provided</scope>-->
+<!--        </dependency>-->
+<!-- JETTY -->
+
+
+
         <!-- Provided dependencies -->
         <dependency>
             <groupId>net.sf.jasperreports</groupId>

+ 37 - 0
src/main/java/cz/hsrs/main/JettyServer.java

@@ -0,0 +1,37 @@
+package cz.hsrs.main;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.webapp.WebAppContext;
+
+public class JettyServer {
+
+    private static final int DEFAULT_PORT = 8080;
+    private static final String DEFAULT_PREFIX = "/DBService";
+
+    public Server server;
+
+    public void start() throws Exception {
+        server = new Server(DEFAULT_PORT);
+
+        WebAppContext context = new WebAppContext();
+        context.setServer(server);
+        context.setContextPath(DEFAULT_PREFIX);
+//        context.setWar("src/main/webapp");
+        context.setResourceBase("src/main/webapp");
+        context.setDefaultsDescriptor("src/main/webapp/WEB-INF/web.xml");
+        server.setHandler(context);
+
+        try {
+            server.start();
+            server.join();
+        } catch (Exception e) {
+            e.printStackTrace();
+            stop();
+        }
+    }
+    public void stop() throws Exception {
+        if (server != null) {
+            server.stop();
+        }
+    }
+}

+ 9 - 6
src/main/java/cz/hsrs/main/StartJetty.java

@@ -1,14 +1,14 @@
 package cz.hsrs.main;
 
-import org.mortbay.jetty.Connector;
-import org.mortbay.jetty.Server;
-import org.mortbay.jetty.bio.SocketConnector;
-import org.mortbay.jetty.webapp.WebAppContext;
+//import org.mortbay.jetty.Connector;
+//import org.mortbay.jetty.Server;
+//import org.mortbay.jetty.bio.SocketConnector;
+//import org.mortbay.jetty.webapp.WebAppContext;
 
 import cz.hsrs.db.pool.SQLExecutor;
 
 public class StartJetty {
-
+/*
     public static Server server = new Server();
 
     public static void start() throws Exception {
@@ -40,8 +40,11 @@ public class StartJetty {
     public static void stop() throws Exception {
         server.stop();
     }
+
+ */
     public static void main(String[] args) throws Exception {
     	//PropertyConfigurator.configure("/log4j.properties");
-        start();
+      //  start();
+        new JettyServer().start();
     }
 }

+ 8 - 8
src/main/java/cz/hsrs/rest/provider/UserRest.java

@@ -16,7 +16,7 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
-import org.mortbay.jetty.HttpStatus;
+import org.eclipse.jetty.http.HttpStatus;
 
 import cz.hsrs.db.model.NoItemFoundException;
 import cz.hsrs.db.util.UserUtil;
@@ -55,11 +55,11 @@ public class UserRest {
             return Response.ok().entity(userDetails)
                     .build();
         } catch (AuthenticationException e1) {
-            return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
+            return Response.status(HttpStatus.UNAUTHORIZED_401)
                     .entity(new ExceptionBean(e1.getClass().getName(), "Authentication failure for request!"))
                     .build();
         } catch (SQLException e) {
-            return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
+            return Response.status(HttpStatus.INTERNAL_SERVER_ERROR_500)
                     .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
                     .build();
         }
@@ -95,16 +95,16 @@ public class UserRest {
             
             }
             else {
-                return Response.status(HttpStatus.ORDINAL_403_Forbidden)
+                return Response.status(HttpStatus.FORBIDDEN_403)
                         .entity(new ExceptionBean("AuthenticationException", "Not enough rights!"))
                         .build();
             }
         } catch (AuthenticationException e1) {
-            return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
+            return Response.status(HttpStatus.UNAUTHORIZED_401)
                     .entity(new ExceptionBean(e1.getClass().getName(), "Authentication failure for request!"))
                     .build();
         } catch (SQLException e) {
-            return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
+            return Response.status(HttpStatus.INTERNAL_SERVER_ERROR_500)
             		.entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
                     .build();
         }
@@ -146,11 +146,11 @@ public class UserRest {
             return Response.ok(UserRestUtil.getAllRights())
                     .build();
         } catch (AuthenticationException e) {
-            return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
+            return Response.status(HttpStatus.UNAUTHORIZED_401)
                     .entity(new ExceptionBean(e.getClass().getName(), "Authentication failure for request!"))
                     .build();
         } catch (SQLException e) {
-            return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
+            return Response.status(HttpStatus.INTERNAL_SERVER_ERROR_500)
                     .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
                     .build();
         }