BuaBook HTTP Common Library

HTTP access functionality (c) 2016 - 2017 Sport Trades Ltd

License

License

GroupId

GroupId

com.buabook
ArtifactId

ArtifactId

http-common
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

BuaBook HTTP Common Library
HTTP access functionality (c) 2016 - 2017 Sport Trades Ltd
Project URL

Project URL

http://buabook.github.io
Source Code Management

Source Code Management

https://github.com/BuaBook/http-common.git

Download http-common

How to add to project

<!-- https://jarcasting.com/artifacts/com.buabook/http-common/ -->
<dependency>
    <groupId>com.buabook</groupId>
    <artifactId>http-common</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.buabook/http-common/
implementation 'com.buabook:http-common:1.0.2'
// https://jarcasting.com/artifacts/com.buabook/http-common/
implementation ("com.buabook:http-common:1.0.2")
'com.buabook:http-common:jar:1.0.2'
<dependency org="com.buabook" name="http-common" rev="1.0.2">
  <artifact name="http-common" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.buabook', module='http-common', version='1.0.2')
)
libraryDependencies += "com.buabook" % "http-common" % "1.0.2"
[com.buabook/http-common "1.0.2"]

Dependencies

compile (4)

Group / Artifact Type Version
com.google.guava : guava jar 21.0
com.google.http-client : google-http-client jar 1.22.0
org.json : json jar 20160810
org.slf4j : slf4j-api jar 1.7.22

provided (2)

Group / Artifact Type Version
org.glassfish.jersey.ext : jersey-bean-validation jar 2.25.1
com.fasterxml.jackson.core : jackson-databind jar 2.8.6

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 2.7.11
org.hamcrest : java-hamcrest jar 2.0.0.0

Project Modules

There are no modules declared in this project.

HTTP Access and JSON Serialisation Library

This library provides the following features:

  • HTTP Interface

    • HTTP GET / POST / PUT (building on the Google HTTP Client library)
    • Support for Basic and Bearer authorisation schemes
    • Deserialisation to org.json.JSONObject
  • Jersey Server

    • Serialisation support for org.json.JSONObject
    • URL information logging
    • Constraint validation error logging
  • Jackson

    • Serialisation support for org.json.JSONObject
  • OAuth

    • Decoder for unencrypted JWT ID tokens
  • Standard JSON Response POJOs

    • Provide standardised JSON responses for all BuaBook APIs

Note that this library marks the Jersey and Jackson dependencies for the features above as provided only. Therefore you need to include the dependencies in your application for them to work.

Maven Central Build Status Coverage Status

HTTP Access

HttpClient and HttpHelpers provides the ability to perform HTTP requests building on Google's HTTP client.

HttpClient

Provides GET, POST and PUT functionality (see JavaDoc for full details).

Basic and Bearer authentication are supported:

  • Basic authentication with new HttpClient(String, String)
  • Bearer authentication with new HttpClient(String)

HTTP responses can be converted to String (HttpClient.getResponseAsString) or JSON (HttpClient.getResponseAsJson).

A single HttpClient can be instantiated for the entire application as a new request is generated each time doGet, doPost or doPut are called.

ApacheHttpClient

This behaves the same as HttpClient but uses Apache's HTTP connection libraries instead of the standard java.net libraries.

HttpHelpers

The function HttpHelpers.appendUrlParameters(String, Map<String, Object>) will take the map of parameters and append them to the end of the specified URL in query parameter form (e.g. ?param1=value). Useful for HTTP GET API calls.

Jersey Server Features

Message Body Writer for org.json.JSONObject

JsonMessageBodyWriter can be registered in a Jersey ResourceConfig to support serialisation of org.json.JSONObject when returned to the caller:

public class WebServiceResourceConfig extends ResourceConfig {

    public WebServiceResourceConfig() {
        register(new JsonMessageBodyWriter());
    }
}

URL Logger

The UrlPrinterFilter class provides logging when any URL within the Jersey application is queried. This can be registered in a Jersey ResourceConfig and logs the following information:

  • INFO: HTTP method, full URL path, content-length and content-type
  • DEBUG: Query parameters
  • TRACE: Request headers

Example log output:

2017.02.23 15:42:03.597 +0000 INFO  [qtp1005849716-31] com.buabook.http.common.jersey.UrlPrinterFilter : HTTP HEAD: http://127.0.0.1:8090/api/status

Constraint Exception Logger

ConstraintViolationExceptionConverter.asValidationErrors provides a way to log any validation errors that occur whilst parsing an inbound API request parameters.

This can be used with a Jersey ExceptionMapper for ConstraintViolationException:

public class ParameterConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
	
	private static final Logger log = LoggerFactory.getLogger(ParameterConstraintViolationExceptionMapper.class);
	
	@Override
	public Response toResponse(ConstraintViolationException e) {
		JSONObject errorResponse = new JSONObject()
						    .put("validationErrors", ConstraintViolationExceptionConverter.asValidationErrors(e));
		
		return Response
			.status(Status.BAD_REQUEST)
			.entity(errorResponse)
			.build();
	}

}

Jackson Features

ObjectMapperWithJsonObjectSupport provides a standard Jackson ObjectMapper with the added org.json.JSONObject serialisation.

This can then be configured to used by Jackson via a Jersey ResourceConfig:

public class WebServiceResourceConfig extends ResourceConfig {

    public WebServiceResourceConfig() {
        JacksonJaxbJsonProvider jacksonProvider = new JacksonJaxbJsonProvider();
    	jacksonProvider.setMapper(ObjectMapperWithJsonObjectSupport.newMapper());
    	
    	register(jacksonProvider);
    }
}

OAuth

JwtIdTokenDecoder.decodeIdToken will decode an unencrypted JWT ID token and return it in JSON form. We use this to decode ID tokens from Microsoft's Azure Active Directory.

Response POJOs

These POJOs are used to return a standardised JSON interface to clients. They ensure that the JSON returned to clients always take the following form:

{
    "success": Boolean,
    "response": Object
}
com.buabook

BuaBook

Versions

Version
1.0.2
1.0.1
1.0.0