fixed auth issue and error handling
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
package com.inteligr8.buxfer.model;
|
package com.inteligr8.buxfer.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
|
||||||
|
@JsonRootName("response")
|
||||||
public class BaseResponse {
|
public class BaseResponse {
|
||||||
|
|
||||||
public enum Status {
|
public enum Status {
|
||||||
@@ -13,7 +16,10 @@ public class BaseResponse {
|
|||||||
private Status status;
|
private Status status;
|
||||||
|
|
||||||
@JsonProperty(value = "error_description", required = false)
|
@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;
|
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;
|
return this.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setError(String error) {
|
public void setError(Error error) {
|
||||||
this.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.Form;
|
||||||
import javax.ws.rs.core.GenericType;
|
import javax.ws.rs.core.GenericType;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response.Status;
|
|
||||||
import javax.ws.rs.core.UriBuilder;
|
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.Response;
|
||||||
import com.inteligr8.buxfer.model.TokenResponse;
|
import com.inteligr8.buxfer.model.TokenResponse;
|
||||||
import com.inteligr8.rs.AuthorizationFilter;
|
import com.inteligr8.rs.AuthorizationFilter;
|
||||||
|
|
||||||
public class BuxferAuthorizationFilter implements AuthorizationFilter {
|
public class BuxferAuthorizationFilter implements AuthorizationFilter {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
private String email;
|
private String email;
|
||||||
private String password;
|
private String password;
|
||||||
private String token;
|
private String token;
|
||||||
@@ -43,11 +47,13 @@ public class BuxferAuthorizationFilter implements AuthorizationFilter {
|
|||||||
|
|
||||||
protected void requestToken(ClientRequestContext requestContext) {
|
protected void requestToken(ClientRequestContext requestContext) {
|
||||||
UriBuilder loginUri = UriBuilder.fromUri(requestContext.getUri())
|
UriBuilder loginUri = UriBuilder.fromUri(requestContext.getUri())
|
||||||
.replacePath("/api/login");
|
.replacePath("/api/login")
|
||||||
|
.replaceQuery(null);
|
||||||
|
|
||||||
Form form = new Form();
|
Form form = new Form();
|
||||||
form.param("email", this.email);
|
form.param("email", this.email);
|
||||||
form.param("password", this.password);
|
form.param("password", this.password);
|
||||||
|
Entity<Form> formEntity = Entity.form(form);
|
||||||
|
|
||||||
GenericType<Response<TokenResponse>> responseType = new GenericType<Response<TokenResponse>>() {};
|
GenericType<Response<TokenResponse>> responseType = new GenericType<Response<TokenResponse>>() {};
|
||||||
|
|
||||||
@@ -56,16 +62,16 @@ public class BuxferAuthorizationFilter implements AuthorizationFilter {
|
|||||||
.target(loginUri)
|
.target(loginUri)
|
||||||
.request()
|
.request()
|
||||||
.accept(MediaType.APPLICATION_JSON)
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
.post(Entity.form(form), responseType)
|
.post(formEntity, responseType).getResponse();
|
||||||
.getResponse();
|
|
||||||
|
|
||||||
if (!com.inteligr8.buxfer.model.BaseResponse.Status.OK.equals(response.getStatus()))
|
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.token = response.getToken();
|
||||||
|
this.logger.debug("received access token: {} = > {}", this.email, this.token);
|
||||||
} catch (NotAuthorizedException nae) {
|
} catch (NotAuthorizedException nae) {
|
||||||
throw nae;
|
throw nae;
|
||||||
} catch (WebApplicationException wae) {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user