Skip to content

Instantly share code, notes, and snippets.

Avatar

AbdulBasit Kabir abdulbasitkay

View GitHub Profile
@abdulbasitkay
abdulbasitkay / api.domain.tld.conf
Created Nov 6, 2018
port-forwarding nginx config file for glassfish using let's encrypt ssl certificate
View api.domain.tld.conf
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;
}
@abdulbasitkay
abdulbasitkay / build.gradle
Created Aug 12, 2017
Sample gradle build script with Checkstyle and FindBugs for ci build
View build.gradle
apply plugin: 'java'
apply plugin: 'findbugs'
apply plugin: 'checkstyle'
repositories {
mavenCentral()
}
dependencies {
@abdulbasitkay
abdulbasitkay / QuestionResource.java
Created May 4, 2017
Question Resource with error handling
View QuestionResource.java
@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");
@abdulbasitkay
abdulbasitkay / QuestionResource.java
Created May 4, 2017
Question Resource with null check.
View QuestionResource.java
@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
Last active May 4, 2017
Question Resource without error handling
View QuestionResource.java
@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();
}
View ErrorEntity.java
// .... 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));
View ApiExceptionMapper.java
@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());
View ApiException.java
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
View google checkstyle
<?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">