CHANGED: added first API resources with jetty and jersey

This commit is contained in:
Kc 2018-09-16 23:24:20 +02:00
parent 9fb434cdd6
commit d63ff02b97
5 changed files with 123 additions and 64 deletions

41
pom.xml
View File

@ -43,19 +43,42 @@
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.11.v20180605</version>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.11.v20180605</version>
<classifier>config</classifier>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.11.v20180605</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
</dependencies>
</project>

20
src/Start.java Normal file
View File

@ -0,0 +1,20 @@
import api.ApiService;
import repository.DataException;
import repository.RepositoryFactory;
import repository.RepositoryManager;
import repository.hsqldb.HSQLDBRepositoryFactory;
public class Start {
private static final String connectionUrl = "jdbc:hsqldb:mem:db/test;create=true;close_result=true;sql.strict_exec=true;sql.enforce_names=true;sql.syntax_mys=true";
public static void main(String args[]) throws DataException
{
RepositoryFactory repositoryFactory = new HSQLDBRepositoryFactory(connectionUrl);
RepositoryManager.setRepositoryFactory(repositoryFactory);
ApiService apiService = new ApiService();
apiService.start();
}
}

View File

@ -1,5 +1,7 @@
package api;
//import io.swagger.jaxrs.config.DefaultJaxrsConfig;
import java.util.HashSet;
import java.util.Set;
@ -10,62 +12,71 @@ import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import settings.Settings;
public class ApiService {
private Server server;
public Server server;
public ApiService()
{
//CREATE CONFIG
Set<Class<?>> s = new HashSet<Class<?>>();
public ApiService()
{
// resources to register
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(BlocksResource.class);
ResourceConfig config = new ResourceConfig(s);
//CREATE CONTAINER
ServletContainer container = new ServletContainer(config);
//CREATE CONTEXT
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(new ServletHolder(container),"/*");
ResourceConfig config = new ResourceConfig(s);
// create RPC server
this.server = new Server(Settings.getInstance().getRpcPort());
//CREATE WHITELIST
// whitelist
InetAccessHandler accessHandler = new InetAccessHandler();
for(String pattern : Settings.getInstance().getRpcAllowed())
accessHandler.include(pattern);
accessHandler.include(pattern);
this.server.setHandler(accessHandler);
// context
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath("/");
accessHandler.setHandler(context);
//CREATE RPC SERVER
this.server = new Server(Settings.getInstance().getRpcPort());
this.server.setHandler(accessHandler);
}
public void start()
{
try
// API servlet
ServletContainer container = new ServletContainer(config);
ServletHolder apiServlet = new ServletHolder(container);
apiServlet.setInitOrder(1);
context.addServlet(apiServlet, "/api/*");
/*
// Setup Swagger servlet
ServletHolder swaggerServlet = context.addServlet(DefaultJaxrsConfig.class, "/swagger-core");
swaggerServlet.setInitOrder(2);
swaggerServlet.setInitParameter("api.version", "1.0.0");
*/
}
public void start()
{
try
{
//START RPC
server.start();
}
//START RPC
server.start();
}
catch (Exception e)
{
//FAILED TO START RPC
}
}
public void stop()
{
try
{
//STOP RPC
server.stop();
}
//FAILED TO START RPC
}
}
public void stop()
{
try
{
//STOP RPC
server.stop();
}
catch (Exception e)
{
//FAILED TO STOP RPC
}
}
{
//FAILED TO STOP RPC
}
}
}

View File

@ -1,9 +1,12 @@
package api;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import repository.DataException;
@ -13,16 +16,18 @@ import repository.RepositoryManager;
@Path("blocks")
@Produces(MediaType.APPLICATION_JSON)
public class BlocksResource {
@Context
HttpServletRequest request;
@GET
@Path("/height")
public static String getHeight()
{
try (final Repository repository = RepositoryManager.getRepository()) {
return String.valueOf(repository.getBlockRepository().getBlockchainHeight());
} catch (Exception e) {
throw new WebApplicationException("What happened?");
}
}
@GET
@Path("/height")
public static String getHeight()
{
try (final Repository repository = RepositoryManager.getRepository()) {
return String.valueOf(repository.getBlockRepository().getBlockchainHeight());
} catch (Exception e) {
throw new WebApplicationException("What happened?");
}
}
}

View File

@ -26,7 +26,7 @@ public class Settings {
//RPC
private int rpcPort = 9085;
private List<String> rpcAllowed = new ArrayList<String>(Arrays.asList("127.0.0.1"));
private List<String> rpcAllowed = new ArrayList<String>(Arrays.asList("127.0.0.1", "::1")); // ipv4, ipv6
private boolean rpcEnabled = true;
// Constants