We were using RESTEasy 2.0 in on of our earlier projects. RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. The 2.0 version was quite simple to use. I decided to use version 3.0 in one of recent projects and the migration was not that tough. A lot of chages have been done in the newer version. I am going to mention the ones which I found out.
RESTEasy Client
In version 2.0
String url = "http://resetservice.com/api/testService";
ClientRequest request = new ClientRequest(url); request.body("application/json", json); response = request.post(); int status = response.getStatus(); if (status == 200) { String data = (String) response.getEntity(String.class); Boolean value = new Gson().fromJson(data, Boolean.class); return value; } return false;
The same thing in 3.0 now changes to -
ResteasyClient client = new ResteasyClientBuilder().build(); String result = getResponseString("http://resetservice.com/api/testService"); Boolean returnVal = clientUtil.getObjectFromJson(Boolean.class, result); return returnVal;
Authentication
I also had to use some in-house authentication technic. I used it in very basic way. I checked for a particular request header (authorizationString) for authenticating the request. If that header is not present the request gets aborted. I used a very basic Below is the code snippet for the same.
@Provider @ServerInterceptor public class AuthorizationRequestFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) throws IOException { final SecurityContext securityContext =requestContext.getSecurityContext(); String header = requestContext.getHeaderString("authorizationString"); try { if (securityContext == null || header == null || header == "" || (header != null && !header.equals( ConfigReader.getInstance().getAuthToken()))) { requestContext.abortWith( Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build()); } } catch (ConfigurationException e) { e.printStackTrace(); } } }
0 comments:
Post a Comment