Skip to content

Commit

Permalink
feat: Implement endpoints for modification/ save
Browse files Browse the repository at this point in the history
  • Loading branch information
jamilraichouni committed Oct 23, 2024
1 parent dea63cb commit 5a526b2
Show file tree
Hide file tree
Showing 36 changed files with 2,543 additions and 36 deletions.
2 changes: 1 addition & 1 deletion rest-api/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plugin>
<extension point="org.eclipse.ui.startup">
<startup
class="Main">
class="com.db.capella.Main">
</startup>
</extension>
</plugin>
Expand Down
35 changes: 0 additions & 35 deletions rest-api/src/Main.java

This file was deleted.

15 changes: 15 additions & 0 deletions rest-api/src/com/db/capella/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.db.capella;

import org.glassfish.jersey.server.ResourceConfig;

import com.db.capella.api.JacksonJsonProvider;
import com.db.capella.api.ProjectsApi;
import com.db.capella.api.WorkspaceApi;

public class Application extends ResourceConfig {
public Application() {
register(ProjectsApi.class);
register(WorkspaceApi.class);
register(JacksonJsonProvider.class);
}
}
42 changes: 42 additions & 0 deletions rest-api/src/com/db/capella/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.db.capella;

import java.net.URI;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.IStartup;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

public class Main implements IStartup {
public static final String BASE_URI = "http://localhost:5007/api/v1";

public static void log(int severity, String message, Throwable exception) {
Bundle bundle = FrameworkUtil.getBundle(Main.class);
ILog log = Platform.getLog(bundle);
IStatus status = new Status(severity, bundle.getSymbolicName(),
message, exception);
log.log(status);
}

@Override
public void earlyStartup() {
final ResourceConfig resourceConfig = new Application();
try {
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
URI.create(BASE_URI),
resourceConfig, true);
log(IStatus.INFO, "Capella REST API server listens on " + BASE_URI + " ...", null);
} catch (Exception e) {
String msg = "There was an error while starting Capella REST API server.";
log(IStatus.ERROR, msg, null);
System.err.println(msg);
e.printStackTrace();
}
}
}
37 changes: 37 additions & 0 deletions rest-api/src/com/db/capella/api/ApiException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.db.capella.api;

/**
* The exception that can be used to store the HTTP status code returned by an API response.
*/
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJerseyServerCodegen", comments = "Generator version: 7.7.0")
public class ApiException extends Exception {
/** The HTTP status code. */
private int code;

/**
* Constructor.
*
* @param code The HTTP status code.
* @param msg The error message.
*/
public ApiException(int code, String msg) {
super(msg);
this.code = code;
}

/**
* Get the HTTP status code.
*
* @return The HTTP status code.
*/
public int getCode() {
return code;
}

@Override
public String toString() {
return "ApiException{" +
"code=" + code +
'}';
}
}
22 changes: 22 additions & 0 deletions rest-api/src/com/db/capella/api/ApiOriginFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.db.capella.api;

import java.io.IOException;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletResponse;

@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJerseyServerCodegen", comments = "Generator version: 7.7.0")
public class ApiOriginFilter implements jakarta.servlet.Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
chain.doFilter(request, response);
}

public void destroy() {}

public void init(FilterConfig filterConfig) throws ServletException {}
}
66 changes: 66 additions & 0 deletions rest-api/src/com/db/capella/api/ApiResponseMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.db.capella.api;


@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJerseyServerCodegen", comments = "Generator version: 7.7.0")
public class ApiResponseMessage {
public static final int ERROR = 1;
public static final int WARNING = 2;
public static final int INFO = 3;
public static final int OK = 4;
public static final int TOO_BUSY = 5;

int code;
String type;
String message;

public ApiResponseMessage(){}

public ApiResponseMessage(int code, String message){
this.code = code;
switch(code){
case ERROR:
setType("error");
break;
case WARNING:
setType("warning");
break;
case INFO:
setType("info");
break;
case OK:
setType("ok");
break;
case TOO_BUSY:
setType("too busy");
break;
default:
setType("unknown");
break;
}
this.message = message;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
50 changes: 50 additions & 0 deletions rest-api/src/com/db/capella/api/Bootstrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.db.capella.api;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder;
import io.swagger.v3.oas.integration.*;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.info.*;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;

public class Bootstrap extends HttpServlet {

private static final long serialVersionUID = 20230810;

@Override
public void init(ServletConfig config) throws ServletException {

Info info = new Info()
.title("OpenAPI Server")
.description("API to access live data and modify Capella projects")
.termsOfService("")
.contact(new Contact()
.email(""))
.license(new License()
.name("")
.url("http://unlicense.org"));

OpenAPI oas = new OpenAPI();
oas.info(info);

SwaggerConfiguration openApiConfig = new SwaggerConfiguration()
.openAPI(oas)
.prettyPrint(true)
.resourcePackages(Stream.of("io.swagger.sample.resource").collect(Collectors.toSet()));

try {
new JaxrsOpenApiContextBuilder()
.servletConfig(config)
.openApiConfiguration(openApiConfig)
.buildContext(true);

} catch (OpenApiConfigurationException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
31 changes: 31 additions & 0 deletions rest-api/src/com/db/capella/api/JacksonJsonProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.db.capella.api;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import com.fasterxml.jackson.datatype.jsr310.*;

import com.fasterxml.jackson.jakarta.rs.json.JacksonXmlBindJsonProvider;

import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.Provider;

@Provider
@Produces({MediaType.APPLICATION_JSON})
public class JacksonJsonProvider extends JacksonXmlBindJsonProvider {

public JacksonJsonProvider() {

ObjectMapper objectMapper = new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.enable(SerializationFeature.INDENT_OUTPUT)
.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
.registerModule(new JavaTimeModule())
.setDateFormat(new RFC3339DateFormat());

setMapper(objectMapper);
}
}
10 changes: 10 additions & 0 deletions rest-api/src/com/db/capella/api/NotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.db.capella.api;

@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJerseyServerCodegen", comments = "Generator version: 7.7.0")
public class NotFoundException extends ApiException {
private int code;
public NotFoundException (int code, String msg) {
super(code, msg);
this.code = code;
}
}
Loading

0 comments on commit 5a526b2

Please sign in to comment.