fixed auth issue and error handling
This commit is contained in:
parent
a50a0b773f
commit
0e7571830c
@ -1,7 +1,10 @@
|
||||
package com.inteligr8.buxfer.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
|
||||
@JsonRootName("response")
|
||||
public class BaseResponse {
|
||||
|
||||
public enum Status {
|
||||
@ -13,7 +16,10 @@ public class BaseResponse {
|
||||
private Status status;
|
||||
|
||||
@JsonProperty(value = "error_description", required = false)
|
||||
private String error;
|
||||
private String errorDesc;
|
||||
|
||||
@JsonProperty(required = false)
|
||||
private Error error;
|
||||
|
||||
|
||||
|
||||
@ -25,12 +31,29 @@ public class BaseResponse {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
public String getErrorDescription() {
|
||||
return this.errorDesc;
|
||||
}
|
||||
|
||||
public void setErrorDescription(String errorDesc) {
|
||||
this.errorDesc = errorDesc;
|
||||
}
|
||||
|
||||
public Error getError() {
|
||||
return this.error;
|
||||
}
|
||||
|
||||
public void setError(String error) {
|
||||
public void setError(Error error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public String getErrorDescriptionOrMessage() {
|
||||
if (this.error != null && this.error.getMessage() != null) {
|
||||
return this.error.getMessage();
|
||||
} else {
|
||||
return this.errorDesc;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package com.inteligr8.buxfer.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Error {
|
||||
|
||||
@JsonProperty
|
||||
private String type;
|
||||
|
||||
@JsonProperty
|
||||
private String message;
|
||||
|
||||
|
||||
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
@ -9,15 +9,19 @@ import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.Form;
|
||||
import javax.ws.rs.core.GenericType;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.inteligr8.buxfer.model.Response;
|
||||
import com.inteligr8.buxfer.model.TokenResponse;
|
||||
import com.inteligr8.rs.AuthorizationFilter;
|
||||
|
||||
public class BuxferAuthorizationFilter implements AuthorizationFilter {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private String email;
|
||||
private String password;
|
||||
private String token;
|
||||
@ -43,11 +47,13 @@ public class BuxferAuthorizationFilter implements AuthorizationFilter {
|
||||
|
||||
protected void requestToken(ClientRequestContext requestContext) {
|
||||
UriBuilder loginUri = UriBuilder.fromUri(requestContext.getUri())
|
||||
.replacePath("/api/login");
|
||||
.replacePath("/api/login")
|
||||
.replaceQuery(null);
|
||||
|
||||
Form form = new Form();
|
||||
form.param("email", this.email);
|
||||
form.param("password", this.password);
|
||||
Entity<Form> formEntity = Entity.form(form);
|
||||
|
||||
GenericType<Response<TokenResponse>> responseType = new GenericType<Response<TokenResponse>>() {};
|
||||
|
||||
@ -56,16 +62,16 @@ public class BuxferAuthorizationFilter implements AuthorizationFilter {
|
||||
.target(loginUri)
|
||||
.request()
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.post(Entity.form(form), responseType)
|
||||
.getResponse();
|
||||
.post(formEntity, responseType).getResponse();
|
||||
|
||||
if (!com.inteligr8.buxfer.model.BaseResponse.Status.OK.equals(response.getStatus()))
|
||||
throw new WebApplicationException(response.getError(), Status.UNAUTHORIZED.getStatusCode());
|
||||
throw new NotAuthorizedException(response.getErrorDescriptionOrMessage(), response);
|
||||
this.token = response.getToken();
|
||||
this.logger.debug("received access token: {} = > {}", this.email, this.token);
|
||||
} catch (NotAuthorizedException nae) {
|
||||
throw nae;
|
||||
} catch (WebApplicationException wae) {
|
||||
throw new NotAuthorizedException("Indirect due to non-authorization failure: " + wae.getMessage(), wae);
|
||||
throw new NotAuthorizedException("Indirect due to non-authorization failure: [" + wae.getResponse().getStatus() + "]", wae);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user