Finalising service
This commit is contained in:
parent
a958ddc834
commit
072bbacae3
|
@ -0,0 +1,38 @@
|
||||||
|
package org.gcube.common.authorization.rest;
|
||||||
|
|
||||||
|
import javax.ws.rs.WebApplicationException;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.Response.Status;
|
||||||
|
import javax.ws.rs.ext.ExceptionMapper;
|
||||||
|
import javax.ws.rs.ext.Provider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
@Provider
|
||||||
|
public class AuthorizationBridgeExceptionMapper implements ExceptionMapper<Exception> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response toResponse(Exception exception) {
|
||||||
|
|
||||||
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
|
String exceptionMessage = exception.getMessage();
|
||||||
|
try {
|
||||||
|
if(exception.getCause() != null) {
|
||||||
|
exceptionMessage = exception.getCause().getMessage();
|
||||||
|
}
|
||||||
|
} catch(Exception e) {
|
||||||
|
exceptionMessage = exception.getMessage();
|
||||||
|
}
|
||||||
|
MediaType mediaType = MediaType.TEXT_PLAIN_TYPE;
|
||||||
|
|
||||||
|
if(WebApplicationException.class.isAssignableFrom(exception.getClass())) {
|
||||||
|
Response gotResponse = ((WebApplicationException) exception).getResponse();
|
||||||
|
status = Status.fromStatusCode(gotResponse.getStatusInfo().getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.status(status).entity(exceptionMessage).type(mediaType).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package org.gcube.common.authorization.rest;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
||||||
|
import javax.ws.rs.core.UriInfo;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class BaseREST {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Context
|
||||||
|
protected UriInfo uriInfo;
|
||||||
|
|
||||||
|
protected static final String LOCATION_HEADER = "Location";
|
||||||
|
|
||||||
|
protected void setCalledMethod(String method) {
|
||||||
|
CalledMethodProvider.instance.set(method);
|
||||||
|
logger.info("{}", uriInfo.getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ResponseBuilder addLocation(ResponseBuilder responseBuilder, String id) {
|
||||||
|
return responseBuilder.header(LOCATION_HEADER,
|
||||||
|
String.format("%s/%s", uriInfo.getAbsolutePath().toString(), id));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,45 @@
|
||||||
package org.gcube.common.authorization.rest;
|
package org.gcube.common.authorization.rest;
|
||||||
|
|
||||||
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.InternalServerErrorException;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.WebApplicationException;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*/
|
*/
|
||||||
|
@Path("goat")
|
||||||
|
public class Goat extends BaseREST {
|
||||||
|
|
||||||
public class Goat {
|
@GET
|
||||||
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
|
public String getOldToken() {
|
||||||
|
setCalledMethod("GET /goat");
|
||||||
|
try {
|
||||||
|
String oldToken = SecurityTokenProvider.instance.get();
|
||||||
|
if(oldToken==null) {
|
||||||
|
String context = ScopeProvider.instance.get();
|
||||||
|
String username = AuthorizationProvider.instance.get().getClient().getId();
|
||||||
|
UserInfo userInfo = new UserInfo(username, new ArrayList<>());
|
||||||
|
oldToken = authorizationService().generateUserToken(userInfo, context);
|
||||||
|
}
|
||||||
|
return oldToken;
|
||||||
|
} catch(WebApplicationException e) {
|
||||||
|
throw e;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new InternalServerErrorException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue