Skip to content
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

Feature/nta restapi #68

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ target
.project
.settings
.classpath
*.orig
10 changes: 10 additions & 0 deletions nta-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<artifactId>webapp</artifactId>
<type>war</type>
</dependency>
<dependency>
<groupId>io.narayana.nta</groupId>
<artifactId>restapi</artifactId>
<type>war</type>
</dependency>
<dependency>
<groupId>io.narayana.nta</groupId>
<artifactId>core</artifactId>
Expand Down Expand Up @@ -91,6 +96,11 @@
<webModule>
<groupId>io.narayana.nta</groupId>
<artifactId>webapp</artifactId>
<contextRoot>/ntaweb</contextRoot>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave the web-console on /nta? Maybe change the rest api to be on /nta/rest or something? Is there a defacto standard for that url?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will revert the url change made on web-console and update the rest url to nta/rest. There is no defacto standard but the preferred standard is "host/project/rest/api/version/endpoint". This can be seen in the following line: https://github.com/Amila17/transaction-analyser/blob/Feature/NTA_Restapi/restapi/src/main/java/io/narayana/nta/restapi/models/URIConstants.java#L12

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so switching the context root to "nta/rest" should fix the problem without breaking your URL? Assuming it works of course. Overlapping context roots might not be allowed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The over lapping context root url might be a problem, I will need to test it to confirm on that.

</webModule>
<webModule>
<groupId>io.narayana.nta</groupId>
<artifactId>restapi</artifactId>
<contextRoot>/nta</contextRoot>
</webModule>
</modules>
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<module>core</module>
<module>plugins</module>
<module>webapp</module>
<module>restapi</module>
<module>nta-dist</module>
<module>nta-remote-dist</module>
<module>demo</module>
Expand Down Expand Up @@ -112,6 +113,15 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>io.narayana.nta</groupId>
<artifactId>restapi</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>compile</scope>
</dependency>


<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
Expand Down
118 changes: 118 additions & 0 deletions restapi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nta-all</artifactId>
<groupId>io.narayana.nta</groupId>
<version>1.0.0.Alpha3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>restapi</artifactId>
<name>Narayana Transaction Analyser REST API</name>
<description>Contains the REST API component of the tool</description>
<packaging>war</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-7.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.narayana.nta</groupId>
<artifactId>core</artifactId>
<version>1.0.0.Alpha3-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.1_spec</artifactId>
<version>1.0.0.Final</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.2.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>2.3.2.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0-SP4</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.3.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-controller-client</artifactId>
<version>8.0.0.Final</version>
<scope>provided</scope>
</dependency>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of white space here...




</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>${version.war.plugin}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.compiler.plugin}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven.surefire}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
19 changes: 19 additions & 0 deletions restapi/src/main/java/io/narayana/nta/restapi/apis/Root.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.narayana.nta.restapi.apis;

import io.narayana.nta.restapi.models.URIConstants;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add the standard copyright and author details. See other files for examples.

* Created with IntelliJ IDEA.
* User: Amila
* Date: 04/05/14
* Time: 17:52
* To change this template use File | Settings | File Templates.
*/
@ApplicationPath(URIConstants.RootURI)
public class Root extends Application
{

}
50 changes: 50 additions & 0 deletions restapi/src/main/java/io/narayana/nta/restapi/apis/TracerAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.narayana.nta.restapi.apis;

import io.narayana.nta.restapi.models.Response.BaseResponse;
import io.narayana.nta.restapi.models.URIConstants;
import io.narayana.nta.restapi.services.TraceLoggingService;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
* Created with IntelliJ IDEA.
* User: Amila
* Date: 14/05/14
* Time: 23:01
* To change this template use File | Settings | File Templates.
*/
@Path(URIConstants.TracerURI)
public class TracerAPI
{
@Inject
private TraceLoggingService traceLoggingService;

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTraceStatus()
{
boolean tracingStatus = traceLoggingService.getTraceLoggingEnable();

BaseResponse baseResponse = new BaseResponse();
baseResponse.setMessage(String.valueOf(tracingStatus));
baseResponse.setStatus(Response.Status.OK);
return Response.ok(baseResponse).build();
}

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response setTransactionStatus(@QueryParam("enable") boolean enable)
{
traceLoggingService.setTraceLoggingEnable(enable);

String status = enable ? "Enabled" : "Disabled";
BaseResponse baseResponse = new BaseResponse();
baseResponse.setMessage("Trace logging has been " + status);
baseResponse.setStatus(Response.Status.OK);

return Response.ok(baseResponse).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.narayana.nta.restapi.apis;

import io.narayana.nta.persistence.enums.Status;
import io.narayana.nta.restapi.models.Response.PayloadResponse;
import io.narayana.nta.restapi.models.URIConstants;
import io.narayana.nta.restapi.services.TransactionService;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
* Created with IntelliJ IDEA.
* User: Amila
* Date: 04/05/14
* Time: 17:52
* To change this template use File | Settings | File Templates.
*/
@Path(URIConstants.TransactionURI)
public class TransactionAPI
{
@Inject
private TransactionService transactionService;

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTransactions(
@QueryParam("status")
String status)
{
PayloadResponse payloadResponse = new PayloadResponse();
payloadResponse.setStatus(Response.Status.OK);

if(status == null)
{
payloadResponse.setPayload(transactionService.getTransactions());
return Response.ok(payloadResponse).build();
}

Status requestedTransactionStatus = Status.valueOf(status.toUpperCase());
payloadResponse.setPayload(transactionService.getTransactions(requestedTransactionStatus));
return Response.ok(payloadResponse).build();
}

@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getTransactionById(
@PathParam("id")
@NotNull
Long id)
{
PayloadResponse payloadResponse = new PayloadResponse();
payloadResponse.setStatus(Response.Status.OK);
payloadResponse.setPayload(transactionService.getTransaction(id));
return Response.ok(payloadResponse).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.narayana.nta.restapi.handlers.exceptions;

import io.narayana.nta.restapi.models.Response.ErrorResponse;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.HashMap;
import java.util.Map;

/**
* Created with IntelliJ IDEA.
* User: Amila
* Date: 27/05/14
* Time: 22:51
* To change this template use File | Settings | File Templates.
*/
@Provider
public class ApplicationExceptionHandler implements ExceptionMapper<Exception>
{
@Override
public Response toResponse(Exception exception)
{
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setMessage(exception.getMessage());
errorResponse.setExceptionClass(exception.getClass());
errorResponse.setException(exception);
if(exception.getCause() != null)
{
errorResponse.setCause(exception.getCause().toString());
}

if(exception instanceof IllegalArgumentException)
{
errorResponse.setStatus(Response.Status.BAD_REQUEST);
return BadRequestResponse(errorResponse);
}

errorResponse.setStatus(Response.Status.INTERNAL_SERVER_ERROR);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorResponse).type(MediaType.APPLICATION_JSON).build();
}

private Response BadRequestResponse(ErrorResponse errorResponse)
{
return Response.status(Response.Status.BAD_REQUEST).entity(errorResponse).type(MediaType.APPLICATION_JSON).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.narayana.nta.restapi.handlers.exceptions;

import io.narayana.nta.restapi.models.Response.ErrorResponse;

import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

/**
* Created with IntelliJ IDEA.
* User: Amila
* Date: 27/05/14
* Time: 23:30
* To change this template use File | Settings | File Templates.
*/
public class ValidationExceptionHandler implements ExceptionMapper<ConstraintViolationException>
{
@Override
public Response toResponse(ConstraintViolationException exception)
{
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setMessage(exception.getMessage());
errorResponse.setExceptionClass(exception.getClass());
errorResponse.setException(exception);
errorResponse.setViolations(exception.getConstraintViolations().toString());
if(exception.getCause() != null)
{
errorResponse.setCause(exception.getCause().toString());
}
errorResponse.setStatus(Response.Status.BAD_REQUEST);

return Response.status(Response.Status.BAD_REQUEST).entity(errorResponse).type(MediaType.APPLICATION_JSON).build();
}
}
Loading