Skip to content

Instantly share code, notes, and snippets.

View abdulbasitkay's full-sized avatar

AbdulBasit Kabir abdulbasitkay

View GitHub Profile
public class ApiException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* contains redundantly the HTTP status of the response sent back to the
* client in case of error, so that the developer does
* not have to look into the response headers.
*/
private int status;
private String developerMessage; //detailed error description for developers
@abdulbasitkay
abdulbasitkay / QuestionResource.java
Last active May 4, 2017 14:57
Question Resource without error handling
@Path("questions/")
public class QuestionResource {
@GET
@Path("{id}")
@Produces("application/json")
Response getQuestion(@PathParam("id") String questionId) {
QuestionDto question = questionService.getQuestionById(questionId);
return Response.status(OK).entity(question).build();
}
@abdulbasitkay
abdulbasitkay / QuestionResource.java
Created May 4, 2017 15:27
Question Resource with null check.
@Path("questions/")
public class QuestionResource {
@GET
@Path("{id}")
@Produces("application/json")
Response getQuestion(@PathParam("id") String questionId) {
QuestionDto question = questionService.getQuestionById(questionId);
if (question == null) {
return Response.status(NOT_FOUND).build();
@abdulbasitkay
abdulbasitkay / QuestionResource.java
Created May 4, 2017 16:52
Question Resource with error handling
@Path("questions/")
public class QuestionResource {
@GET
@Path("{id}")
@Produces("application/json")
Response getQuestion(@PathParam("id") String questionId) {
QuestionDto question = questionService.getQuestionById(questionId);
if (question == null)
throw new ApiException(404, "Question not found", "Make sure the Id is valid and a question has been created");
// .... imports ommited for brevity
public class ErrorEntity implements Serializable {
int status; // contains the same HTTP Status code returned by the server
String message; // message describing the error
String developerMessage; // extra information that might useful for developers
public ErrorEntity(int status, Exception ex) {
StringWriter errorStackTrace = new StringWriter();
ex.printStackTrace(new PrintWriter(errorStackTrace));
@Provider
public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
@Override
public Response toResponse(ApiException ex) {
ErrorEntity error = new ErrorEntity();
error.setStatus(ex.getStatus());
error.setMessage(ex.getMessage());
error.setDeveloperMessage(ex.getMessage());
@abdulbasitkay
abdulbasitkay / build.gradle
Created August 12, 2017 09:06
Sample gradle build script with Checkstyle and FindBugs for ci build
apply plugin: 'java'
apply plugin: 'findbugs'
apply plugin: 'checkstyle'
repositories {
mavenCentral()
}
dependencies {
@abdulbasitkay
abdulbasitkay / api.domain.tld.conf
Created November 6, 2018 15:21
port-forwarding nginx config file for glassfish using let's encrypt ssl certificate
upstream glassfish {
server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5;
}
server {
listen 80;
server_name servername.tdl;
return 301 https://$host$request_uri;
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<!-- This is a checkstyle configuration file. For descriptions of
what the following rules do, please see the checkstyle configuration
page at http://checkstyle.sourceforge.net/config.html -->
<module name="Checker">