Commit 60dde982 authored by Roland Schuller's avatar Roland Schuller
Browse files

Subdomains

parent 4ce4c1a8
Pipeline #578 passed with stage
in 20 seconds
......@@ -25,7 +25,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.38.Final</version>
<version>4.1.42.Final</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
......@@ -45,22 +45,22 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9.2</version>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.9</version>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.13.3</version>
<version>3.13.4</version>
</dependency>
<dependency>
......@@ -80,5 +80,35 @@
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>at.itopen.arbeitszeit.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
......@@ -14,7 +14,6 @@ import at.itopen.simplerest.endpoints.staticfile.StaticFileEndpoint;
import at.itopen.simplerest.path.RestEndpoint;
import at.itopen.simplerest.path.RestPath;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
......@@ -32,29 +31,34 @@ public class Example {
* @param args the command line arguments
*/
public static void main(String[] args) {
RestHttpServer server=RestHttpServer.Start(3000);
RestHttpServer server = RestHttpServer.Start(3000);
server.enableIndex("TestProg", "1.0", "IT-Open", "office@it-open.at");
server.enableExceptionHandling();
server.enableNotFoundHandling();
server.enableStructure("structure",null);
server.enableRestUrlList("urls",null);
server.enableRestDoc("doc",null);
server.enableStructure("structure", null);
server.enableRestUrlList("urls", null);
server.enableRestDoc("doc", null);
try {
server.getRootEndpoint().addRestEndpoint(new RestEndpoint("test"){
server.getRootPathforSubdomain("test").addRestEndpoint(new RestEndpoint("test") {
@Override
public void Call(Conversion conversion, Map<String, String> UrlParameter) {
conversion.getResponse().setData("Super Subdomain");
}
});
server.getRootEndpoint().addRestEndpoint(new RestEndpoint("test") {
@Override
public void Call(Conversion conversion, Map<String,String> UrlParameter) {
public void Call(Conversion conversion, Map<String, String> UrlParameter) {
conversion.getResponse().setData("Super");
}
});
server.getRootEndpoint().addSubPath(new RestPath("html")).setCatchAllEndPoint(new StaticFileEndpoint(new File("/home/roland/src/bergland-amtstafel/web"), new NoCachePolicy()));
server.getRootEndpoint().addRestEndpoint(new GetEndpoint("image"){
server.getRootEndpoint().addRestEndpoint(new GetEndpoint("image") {
@Override
public void Call(Conversion conversion, Map<String,String> UrlParameter) {
public void Call(Conversion conversion, Map<String, String> UrlParameter) {
conversion.getResponse().setContentType(ContentType.JPEG);
File fis;
try {
......@@ -63,52 +67,50 @@ public class Example {
} catch (IOException ex) {
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
server.getRootEndpoint().addRestEndpoint(new RestEndpoint("upload"){
server.getRootEndpoint().addRestEndpoint(new RestEndpoint("upload") {
@Override
public void Call(Conversion conversion, Map<String,String> UrlParameter) {
public void Call(Conversion conversion, Map<String, String> UrlParameter) {
System.out.println(conversion.getRequest().getFiles().get("data").getName());
}
});
server.getRootEndpoint().addRestEndpoint(new JsonUserEndpoint("post"));
final List<String> data= new ArrayList<>();
data.add("Hallo");
data.add("Roland");
CRUDHelper helper=new CRUDHelper("data", server.getRootEndpoint()) {
final List<String> data = new ArrayList<>();
data.add("Hallo");
data.add("Roland");
CRUDHelper helper = new CRUDHelper("data", server.getRootEndpoint()) {
@Override
public void addNewItem(Conversion conversion, Map<String,String> UrlParameter) {
public void addNewItem(Conversion conversion, Map<String, String> UrlParameter) {
data.add("Hallo");
}
@Override
public void getSingeItem(Conversion conversion, Map<String,String> UrlParameter) {
String index=UrlParameter.get("id");
public void getSingeItem(Conversion conversion, Map<String, String> UrlParameter) {
String index = UrlParameter.get("id");
conversion.getResponse().setData(data.get(Integer.parseInt(index)));
}
@Override
public void getAllItem(Conversion conversion, Map<String,String> UrlParameter) {
public void getAllItem(Conversion conversion, Map<String, String> UrlParameter) {
conversion.getResponse().setData(data);
}
@Override
public void updateItem(Conversion conversion, Map<String,String> UrlParameter) {
public void updateItem(Conversion conversion, Map<String, String> UrlParameter) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void deleteItem(Conversion conversion, Map<String,String> UrlParameter) {
String index=UrlParameter.get("id");
public void deleteItem(Conversion conversion, Map<String, String> UrlParameter) {
String index = UrlParameter.get("id");
data.remove(Integer.parseInt(index));
}
};
......@@ -116,9 +118,7 @@ public class Example {
} catch (Exception ex) {
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
......@@ -69,7 +69,7 @@ public class Json {
*/
public static <T> T fromString(String data, TypeReference type) {
try {
return getJSON_CONVERTER().readValue(data, type);
return (T) getJSON_CONVERTER().readValue(data, type);
} catch (IOException ex) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex);
return null;
......
......@@ -123,11 +123,11 @@ public class RestHttpRequestDispatchHandler extends ChannelInboundHandlerAdapter
try {
if (conversion.getRequest().getUri().getPath().isEmpty()) {
if (conversion.getServer().getRootEndpoint().getINDEX() != null) {
worker = new EndpointWorker(conversion.getServer().getRootEndpoint().getINDEX(), null);
if (conversion.getServer().getRootEndpoint(conversion).getINDEX() != null) {
worker = new EndpointWorker(conversion.getServer().getRootEndpoint(conversion).getINDEX(), null);
}
} else {
worker = conversion.getServer().getRootEndpoint().findEndpoint(conversion, 0, new HashMap<>());
worker = conversion.getServer().getRootEndpoint(conversion).findEndpoint(conversion, 0, new HashMap<>());
}
if (worker != null) {
conversion.getResponse().setContentType(ContentType.JSON);
......@@ -135,9 +135,9 @@ public class RestHttpRequestDispatchHandler extends ChannelInboundHandlerAdapter
worker.work(conversion);
} else {
conversion.getResponse().setStatus(HttpStatus.NotFound);
if (conversion.getServer().getRootEndpoint().getNOT_FOUND() != null) {
if (conversion.getServer().getRootEndpoint(conversion).getNOT_FOUND() != null) {
conversion.getResponse().setContentType(ContentType.JSON);
conversion.getServer().getRootEndpoint().getNOT_FOUND().CallEndpoint(conversion, null);
conversion.getServer().getRootEndpoint(conversion).getNOT_FOUND().CallEndpoint(conversion, null);
}
}
} catch (Throwable e) {
......@@ -146,8 +146,8 @@ public class RestHttpRequestDispatchHandler extends ChannelInboundHandlerAdapter
if (conversion.getException() != null) {
conversion.getResponse().setStatus(HttpStatus.InternalServerError);
if (conversion.getServer().getRootEndpoint().getEXCEPTION() != null) {
worker = new EndpointWorker(conversion.getServer().getRootEndpoint().getEXCEPTION(), null);
if (conversion.getServer().getRootEndpoint(conversion).getEXCEPTION() != null) {
worker = new EndpointWorker(conversion.getServer().getRootEndpoint(conversion).getEXCEPTION(), null);
conversion.getResponse().setContentType(ContentType.JSON);
worker.work(conversion);
}
......
......@@ -5,6 +5,7 @@
*/
package at.itopen.simplerest;
import at.itopen.simplerest.conversion.Conversion;
import at.itopen.simplerest.endpoints.DocumentationEndpoint;
import at.itopen.simplerest.endpoints.ErrorEndpoint;
import at.itopen.simplerest.endpoints.IndexEndpoint;
......@@ -24,6 +25,8 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.internal.SystemPropertyUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -39,6 +42,7 @@ public class RestHttpServer {
private final EventLoopGroup worker;
private final RootPath root = new RootPath();
private LoadBalancer loadBalancer = null;
private final Map<String, RootPath> subdomainpath = new HashMap<String, RootPath>();
/**
* Constructor, starts the Rest Server
......@@ -61,6 +65,19 @@ public class RestHttpServer {
return restHttpServer;
}
public synchronized RootPath getRootPathforSubdomain(String subdomain) {
if (subdomain == null) {
return root;
}
if (subdomain.isEmpty()) {
return root;
}
if (!subdomainpath.containsKey(subdomain)) {
subdomainpath.put(subdomain, new RootPath());
}
return subdomainpath.get(subdomain);
}
/**
* Enable an index at the /.
*
......@@ -157,6 +174,39 @@ public class RestHttpServer {
path.addRestEndpoint(new DocumentationEndpoint(urlListPath));
}
/**
* Get the Startpoint of all Rest Calls '/' Make all Sub Path on this this
* Path.
*
* @return
*/
public RootPath getRootEndpoint(Conversion conversion) {
String host = conversion.getRequest().getHost();
for (Map.Entry<String, RootPath> e : subdomainpath.entrySet()) {
String sd = e.getKey() + ".";
if (host.startsWith(sd)) {
return e.getValue();
}
}
return root;
}
/**
* Get the Startpoint of all Rest Calls '/' Make all Sub Path on this this
* Path.
*
* @return
*/
public RootPath getRootEndpoint(String subdomain) {
for (Map.Entry<String, RootPath> e : subdomainpath.entrySet()) {
String sd = e.getKey() + ".";
if (subdomain.equals(sd)) {
return e.getValue();
}
}
return root;
}
/**
* Get the Startpoint of all Rest Calls '/' Make all Sub Path on this this
* Path.
......@@ -174,8 +224,8 @@ public class RestHttpServer {
* @param path
* @return
*/
public RestPath getPath(String path) {
return root.pathForLocation(path);
public RestPath getPath(Conversion conversion, String path) {
return getRootEndpoint(conversion).pathForLocation(path);
}
/**
......
......@@ -33,6 +33,7 @@ public class Request {
private String protocolName;
private int protocolMajorVersion, protocolMinorVersion;
private String method;
private String host;
private Uri uri;
private IpAdress sourceIp = null;
private final HttpHeaders headers;
......@@ -113,6 +114,13 @@ public class Request {
uri = new Uri(request.uri());
params.putAll(uri.queryParam);
headers.addHeaders(request.headers());
host = headers.get("host");
if (host != null) {
if (host.contains(":")) {
host = host.substring(0, host.indexOf(":"));
}
}
if (method.equals("POST")) {
httpDecoder = new HttpPostRequestDecoder((HttpRequest) msg);
}
......@@ -139,6 +147,10 @@ public class Request {
}
}
public String getHost() {
return host;
}
/**
*
* @return
......
......@@ -49,13 +49,13 @@ public class DocumentationEndpoint extends GetEndpoint {
public void Call(Conversion conversion, Map<String, String> UrlParameter) {
endpoints.clear();
StringBuilder out = new StringBuilder();
if (conversion.getServer().getRootEndpoint().getINDEX() instanceof IndexEndpoint) {
HTMLstart(out, (IndexEndpoint) conversion.getServer().getRootEndpoint().getINDEX());
if (conversion.getServer().getRootEndpoint(conversion).getINDEX() instanceof IndexEndpoint) {
HTMLstart(out, (IndexEndpoint) conversion.getServer().getRootEndpoint(conversion).getINDEX());
} else {
HTMLstart(out, null);
}
String path = "/";
subPath(conversion.getServer().getRootEndpoint(), path, false);
subPath(conversion.getServer().getRootEndpoint(conversion), path, false);
for (String erg : endpoints.values()) {
out.append(erg);
}
......
......@@ -14,15 +14,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* @author roland
*/
public class StructureEndpoint extends GetEndpoint {
private class Item{
private class Item {
String name;
boolean auth;
......@@ -38,42 +37,38 @@ public class StructureEndpoint extends GetEndpoint {
this.name = name;
this.auth = auth;
}
}
private class PathItem extends Item
{
private class PathItem extends Item {
public PathItem(String name, boolean auth) {
super(name, auth);
}
List<Item> subItems=new ArrayList<>();
List<Item> subItems = new ArrayList<>();
public List<Item> getSubItems() {
return subItems;
}
}
private class EndPointItem extends Item
{
private class EndPointItem extends Item {
public EndPointItem(String method, String name, boolean auth) {
super(name, auth);
this.method = method;
}
String method;
String method;
public String getMethod() {
return method;
}
}
/**
*
* @param endpointName
......@@ -88,39 +83,43 @@ public class StructureEndpoint extends GetEndpoint {
* @param UrlParameter
*/
@Override
public void Call(Conversion conversion, Map<String,String> UrlParameter) {
PathItem root=new PathItem("/", false);
subPath(conversion.getServer().getRootEndpoint(),root);
public void Call(Conversion conversion, Map<String, String> UrlParameter) {
PathItem root = new PathItem("/", false);
subPath(conversion.getServer().getRootEndpoint(conversion), root);
conversion.getResponse().setData(root);
}
private void subPath(RestPath path,PathItem item)
{
for (RestPath sub:path.getSubPaths())
{
boolean auth=item.auth || (sub instanceof AuthenticatedRestPath);
PathItem pi=new PathItem(sub.getPathName(), auth);
private void subPath(RestPath path, PathItem item) {
for (RestPath sub : path.getSubPaths()) {
boolean auth = item.auth || (sub instanceof AuthenticatedRestPath);
PathItem pi = new PathItem(sub.getPathName(), auth);
item.getSubItems().add(pi);
subPath(sub, pi);
}
for (RestEndpoint sub:path.getEndpoints())
{
boolean auth=item.auth || (sub instanceof AuthenticatedRestEndpoint);
String method="ALL";
if (sub instanceof GetEndpoint) method="GET";
if (sub instanceof PostEndpoint) method="POST";
if (sub instanceof PutEndpoint) method="PUT";
if (sub instanceof DeleteEndpoint) method="DELETE";
if (sub instanceof PutOrPostEndpoint) method="PUT,POST";
EndPointItem pi=new EndPointItem(method,sub.getEndpointName(), auth);
for (RestEndpoint sub : path.getEndpoints()) {
boolean auth = item.auth || (sub instanceof AuthenticatedRestEndpoint);
String method = "ALL";
if (sub instanceof GetEndpoint) {
method = "GET";
}
if (sub instanceof PostEndpoint) {
method = "POST";
}
if (sub instanceof PutEndpoint) {
method = "PUT";
}
if (sub instanceof DeleteEndpoint) {
method = "DELETE";
}
if (sub instanceof PutOrPostEndpoint) {
method = "PUT,POST";
}
EndPointItem pi = new EndPointItem(method, sub.getEndpointName(), auth);
item.getSubItems().add(pi);
}
}
}
......@@ -14,7 +14,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* @author roland
......@@ -35,42 +34,47 @@ public class UrlListEndpoint extends GetEndpoint {
* @param UrlParameter
*/
@Override
public void Call(Conversion conversion, Map<String,String> UrlParameter) {
String path="/";
List<String> endpoints=new ArrayList<>();
subPath(conversion.getServer().getRootEndpoint(),endpoints,path,false);
public void Call(Conversion conversion, Map<String, String> UrlParameter) {
String path = "/";
List<String> endpoints = new ArrayList<>();
subPath(conversion.getServer().getRootEndpoint(conversion), endpoints, path, false);
conversion.getResponse().setData(endpoints);
}
private void subPath(RestPath path,List<String> endpoints,String pathname,boolean isauth)
{
for (RestPath sub:path.getSubPaths())
{
boolean auth=isauth || (sub instanceof AuthenticatedRestPath);
String newPathName=pathname+sub.getPathName()+"/";
subPath(sub, endpoints,newPathName,auth);
private void subPath(RestPath path, List<String> endpoints, String pathname, boolean isauth) {
for (RestPath sub : path.getSubPaths()) {
boolean auth = isauth || (sub instanceof AuthenticatedRestPath);
String newPathName = pathname + sub.getPathName() + "/";
subPath(sub, endpoints, newPathName, auth);
}
for (RestEndpoint sub:path.getEndpoints())
{