Monday, June 23, 2014

Web Services error propagation

Case1:Throw from serviceImpl and Catch in aop if exception is checked
Case2:Throw from Aop and Catch in serviceimpl if exception is checked

Case3 if the caller is another web service.But,the caller must mention try and catch in their calling code  :Throw from serviceimpl and catch in aop and return from aop to client with error object Or rethrow exception
E.g
public void afterThrowing(Method method, Object[] args, Object target,
Exception exception) {
log.error(exception);
System.out.println("afterThrowing exception is called");
throw exception;
}
In this case set error message from aop and catch in controller and get the error message and return to client

In this case if the exception is thrown from service layer ,then it has to propagate to series of web service ,i.e TokenEndpointService1,PasswordEndpointservice2
,TokenEndpointService3 ...and so on

and there can be many services ,so rethrow the exception so that other services can catch it ..and process according to their needs


@Path("/passwordauthzorization")
public class TokenEndpointService1 {
try{
OAuthJSONAccessTokenResponse response = oAuthClient.accessToken(request);
} catch (Exception ex) {
// if any exception is thrown in call to OAuthJSONAccessTokenResponse response = oAuthClient.accessToken(request); here it is catched.Though the exception can be catched in multiple places

if(ex.getMessage().startsWith("invalid_grant")){
String errormessage=ex.getMessage().replaceAll("invalid_grant,"," ");
oauthresponse.setErrorMessage(errormessage);

}







}



// OAuthJSONAccessTokenResponse response = oAuthClient.accessToken(request); // call to web service PasswordEndpointservice2



@Path("/passwordauthzorization")
public class PasswordEndpointservice2 {

@POST
@Consumes("application/x-www-form-urlencoded")
@Produces("application/json")
public Response authorize(@Context HttpServletRequest request)
throws OAuthSystemException, URISyntaxException, JSONException {
try {
Test1 emp = new Test1();


emp.setName("a");
emp.setPassword(password);
emp.setUsername(username);


WebResource webResource = client.resource(Resource_Server_Url);
ClientResponse response = webResource.type(
MediaType.APPLICATION_JSON).post(ClientResponse.class,
mapJsonStr);


}
} catch (OAuthProblemException e) {
OAuthResponse res = OAuthASResponse
.errorResponse(HttpServletResponse.SC_BAD_REQUEST).error(e)
.buildJSONMessage();
return Response.status(res.getResponseStatus())
.entity(res.getBody()).build();
}
return null;

}

@Controller
@RequestMapping("/")
public class TokenEndpointService3 {

@Inject
private ClientService clientservice;
@Autowired
private ClientRepository clientRepository;

@RequestMapping(value = "/springimplict", method = RequestMethod.POST)
public @ResponseBody
ResponseDTO validate(@RequestBody Model emp) throws Exception {

ClientDto dto = new ClientDto();
dto.setPassword(emp.getPassword());


ResponseDTO param = new ResponseDTO();
Users client = null;
try {

client = clientservice.checkClientUserNamePassword(dto);
if(client==null){


param.setError(true);
param.setErrMsg("exception is catched ");

System.out.println("exception is catched ");
return param;


}
dto.setUserid(client.getId());
} catch (Exception ex) {

param.setError(true);
param.setErrMsg(ex.getMessage());
throw ex;

return param;

}

try {

} catch (Exception ex) {
param.setError(true);
System.out.println("exception is catched ");
throw ex; // rethrow he exception so that other web service can   catch it  .in their try catch block
return param;

}

return param;
}


}



No comments:

Post a Comment