Skip to content

Commit

Permalink
feat: Implement endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jamilraichouni committed Oct 23, 2024
1 parent dea63cb commit 4697b50
Show file tree
Hide file tree
Showing 39 changed files with 2,999 additions and 57 deletions.
19 changes: 0 additions & 19 deletions CONTRIBUTING.md

This file was deleted.

29 changes: 28 additions & 1 deletion rest-api/openapi/custom.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

openapi: 3.0.3
info:
title: Capella API
description: API to access live data and modify Capella projects
version: 0.0.1
servers:
- url: http://localhost:5007/api/v1
description: Embeded Capella REST API server
description: Embedded Capella REST API server
tags:
- name: Diagrams
description: >
Expand Down Expand Up @@ -181,6 +184,30 @@ paths:
description: Project saved successfully
'404':
description: Project not found
/projects/{project_name}/diagram-editors:
get:
tags:
- Diagrams
summary: Get a list of all open diagram editors for a project by name
operationId: getDiagramEditorsByProjectName
parameters:
- name: project_name
in: path
required: true
description: Unique name of the project
schema:
type: string
responses:
'200':
description: A list of open diagram editors
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/DiagramEditor'
'404':
description: Project not found
/projects/{project_name}/diagrams:
get:
tags:
Expand Down
3 changes: 1 addition & 2 deletions rest-api/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
<plugin>
<extension point="org.eclipse.ui.startup">
<startup
class="Main">
class="com.db.capella.Main">
</startup>
</extension>
</plugin>

35 changes: 0 additions & 35 deletions rest-api/src/Main.java

This file was deleted.

18 changes: 18 additions & 0 deletions rest-api/src/com/db/capella/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0

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);
}
}
45 changes: 45 additions & 0 deletions rest-api/src/com/db/capella/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0

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://0.0.0.0: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();
}
}
}
40 changes: 40 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,40 @@
// Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0

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 +
'}';
}
}
25 changes: 25 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,25 @@
// Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0

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 {}
}
69 changes: 69 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,69 @@
// Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0

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;
}
}
53 changes: 53 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,53 @@
// Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0

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);
}
}
}
Loading

0 comments on commit 4697b50

Please sign in to comment.