Skip to content

Server #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.intellimate.izou</groupId>
<artifactId>sdk</artifactId>
<version>0.18.2</version>
<version>0.19.0</version>
<name>IzouSDK</name>
<description>the sdk used to program for izou</description>
<url>https://github.com/intellimate/IzouSDK</url>
Expand Down Expand Up @@ -60,13 +60,18 @@
<dependency>
<groupId>org.intellimate.izou</groupId>
<artifactId>izou</artifactId>
<version>1.15.6</version>
<version>1.16.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.24-incubating</version>
</dependency>
</dependencies>

<build>
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/org/intellimate/izou/sdk/AddOnImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.intellimate.izou.sdk;


import org.intellimate.izou.activator.ActivatorModel;
import org.intellimate.izou.events.EventsControllerModel;
import org.intellimate.izou.output.OutputControllerModel;
import org.intellimate.izou.output.OutputExtensionModel;
import org.intellimate.izou.output.OutputPluginModel;
import org.intellimate.izou.sdk.addon.AddOn;
import org.intellimate.izou.sdk.contentgenerator.ContentGenerator;
import org.intellimate.izou.sdk.server.SDKRouter;
import ro.fortsoft.pf4j.Extension;

/**
* @author LeanderK
* @version 1.0
*/
@Extension
@SuppressWarnings({"WeakerAccess", "unused"})
public class AddOnImpl extends AddOn {
public AddOnImpl() {
super(AddOnImpl.class.getCanonicalName());
}

/**
* This method gets called before registering
*/
@Override
public void prepare() {
setRouter(new SDKRouter(getContext()));
}

@Override
public ActivatorModel[] registerActivator() {
return new ActivatorModel[0];
}

@Override
public ContentGenerator[] registerContentGenerator() {
return new ContentGenerator[0];
}

@Override
public EventsControllerModel[] registerEventController() {
return new EventsControllerModel[0];
}

@Override
public OutputPluginModel[] registerOutputPlugin() {
return new OutputPluginModel[0];
}

@Override
public OutputExtensionModel[] registerOutputExtension() {
return new OutputExtensionModel[0];
}

@Override
public OutputControllerModel[] registerOutputController() {
return new OutputControllerModel[0];
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/intellimate/izou/sdk/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ public AddOns getAddOns() {
return context.getAddOns();
}

/**
* Returns the API, which contains information bout to the server and the connection
*
* @return ServerInformation
*/
@Override
public ServerInformation getServerInformation() {
return context.getServerInformation();
}

private class ContentGeneratorsImpl implements ContentGenerators {

/**
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/intellimate/izou/sdk/addon/AddOn.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import org.intellimate.izou.sdk.contentgenerator.ContentGenerator;
import org.intellimate.izou.sdk.output.OutputController;
import org.intellimate.izou.sdk.output.OutputExtension;
import org.intellimate.izou.sdk.server.Router;
import org.intellimate.izou.sdk.server.properties.PropertiesRouter;
import org.intellimate.izou.sdk.util.ContextProvider;
import org.intellimate.izou.sdk.util.Loggable;
import org.intellimate.izou.sdk.util.LoggedExceptionCallback;
import org.intellimate.izou.server.Request;
import ro.fortsoft.pf4j.PluginWrapper;

/**
Expand All @@ -26,6 +29,7 @@ public abstract class AddOn implements AddOnModel, ContextProvider, Loggable, Lo
private final String addOnID;
private Context context;
private PluginWrapper plugin;
private Router router;

/**
* The default constructor for AddOns
Expand All @@ -41,6 +45,7 @@ public AddOn(String addOnID) {
*/
@Override
public void register() {
this.router = new PropertiesRouter(getContext());
prepare();
ContentGenerator[] contentGenerators = registerContentGenerator();
if (contentGenerators != null) {
Expand Down Expand Up @@ -190,6 +195,29 @@ public PluginWrapper getPlugin() {
return plugin;
}

/**
* sets the router to use when handling requests
* @param router the router to use
*/
@SuppressWarnings("unused")
protected void setRouter(Router router) {
this.router = router;
}

/**
* this method handles the HTTP-Requests from the server.
* <p>
* this method should not be overridden, to change behaviour please user setRouter.
* <p>
*
* @param request the request to process
* @return the response
*/
@Override
public org.intellimate.izou.server.Response handleRequest(Request request) {
return router.handle(request);
}

/**
* Sets the Plugin IF it is not already set.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.intellimate.izou.sdk.server;

/**
* represents an BadRequest (400)
* @author LeanderK
* @version 1.0
*/
public class BadRequestException extends RuntimeException {
/**
* Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public BadRequestException(String message) {
super(message);
}

/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public BadRequestException(String message, Throwable cause) {
super(message, cause);
}
}
50 changes: 50 additions & 0 deletions src/main/java/org/intellimate/izou/sdk/server/DefaultHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.intellimate.izou.sdk.server;

import org.intellimate.izou.sdk.Context;
import org.intellimate.izou.sdk.util.AddOnModule;
import org.intellimate.izou.sdk.util.FireEvent;
import org.intellimate.izou.util.IzouModule;

import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

/**
* handles a request on a specific route
* @author LeanderK
* @version 1.0
*/
@SuppressWarnings("WeakerAccess")
public class DefaultHandler extends AddOnModule implements Handler, FireEvent {
private final Method method;
private final Function<Request, Response> function;
private final boolean internal;

/**
* initializes the Module
* @param context the current context
* @param method the registered method
* @param function the function to execute
*/
public DefaultHandler(Context context, String addOnPackageName, String route, Method method, boolean internal, Function<Request, Response> function) {
super(context, addOnPackageName+"."+route+method.name());
this.method = method;
this.function = function;
this.internal = internal;
}

@Override
public Optional<Response> handle(Request request) {
if (!request.getMethod().toLowerCase().equals(method.name().toLowerCase())) {
return Optional.empty();
}
if (internal && !request.getToken().isPresent()) {
return Optional.empty();
}
Response response = function.apply(request);
if (response == null || !response.isValidResponse()) {
throw new InternalServerErrorException("App returned illegal response");
}
return Optional.of(response);
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/intellimate/izou/sdk/server/Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.intellimate.izou.sdk.server;

import java.util.Optional;

/**
* handles a request on a specific route
* @author LeanderK
* @version 1.0
*/
public interface Handler extends HandlerHelper {
/**
* maybe handles a request
* @param request the request
* @return empty if not responsible for the request or response
*/
Optional<Response> handle(Request request);
}
Loading