use of javax.ws.rs.WebApplicationException in project OpenAM by OpenRock.
the class RequestTokenRequest method postReqTokenRequest.
/**
* POST method for creating a request for a Request Token
* @param content representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@POST
@Consumes("application/x-www-form-urlencoded")
@Produces("application/x-www-form-urlencoded")
public Response postReqTokenRequest(@Context HttpContext hc, String content) {
boolean sigIsOk = false;
OAuthResourceManager oauthResMgr = OAuthResourceManager.getInstance();
try {
OAuthServerRequest request = new OAuthServerRequest(hc.getRequest());
OAuthParameters params = new OAuthParameters();
params.readRequest(request);
String tok = params.getToken();
if ((tok != null) && (!tok.contentEquals("")))
throw new WebApplicationException(new Throwable(OAUTH_TOKEN + " MUST not be present."), BAD_REQUEST);
String conskey = params.getConsumerKey();
if (conskey == null) {
throw new WebApplicationException(new Throwable("Consumer key is missing."), BAD_REQUEST);
}
String signatureMethod = params.getSignatureMethod();
if (signatureMethod == null) {
throw new WebApplicationException(new Throwable("Signature Method is missing."), BAD_REQUEST);
}
String callback = params.get(OAUTH_CALLBACK);
if ((callback == null) || (callback.isEmpty())) {
throw new WebApplicationException(new Throwable("Callback URL is missing."), BAD_REQUEST);
}
if (!callback.equals(OAUTH_OOB)) {
try {
URL url = new URL(callback);
} catch (MalformedURLException me) {
throw new WebApplicationException(new Throwable("Callback URL is not valid."), BAD_REQUEST);
}
}
Map<String, String> searchMap = new HashMap<String, String>();
searchMap.put(CONSUMER_KEY, conskey);
List<Consumer> consumers = oauthResMgr.searchConsumers(searchMap);
if ((consumers != null) && (!consumers.isEmpty())) {
cons = consumers.get(0);
}
if (cons == null) {
throw new WebApplicationException(new Throwable("Consumer key invalid or service not registered"), BAD_REQUEST);
}
String secret = null;
if (signatureMethod.equalsIgnoreCase(RSA_SHA1.NAME)) {
secret = cons.getConsRsakey();
} else {
secret = cons.getConsSecret();
}
OAuthSecrets secrets = new OAuthSecrets().consumerSecret(secret).tokenSecret("");
try {
sigIsOk = OAuthSignature.verify(request, params, secrets);
} catch (OAuthSignatureException ex) {
Logger.getLogger(RequestTokenRequest.class.getName()).log(Level.SEVERE, null, ex);
}
if (!sigIsOk)
throw new WebApplicationException(new Throwable("Signature invalid."), BAD_REQUEST);
// We're good to go.
RequestToken rt = new RequestToken();
rt.setConsumerId(cons);
String baseUri = context.getBaseUri().toString();
if (baseUri.endsWith("/")) {
baseUri = baseUri.substring(0, baseUri.length() - 1);
}
URI loc = URI.create(baseUri + PathDefs.REQUEST_TOKENS_PATH + "/" + new UniqueRandomString().getString());
rt.setReqtUri(loc.toString());
rt.setReqtSecret(new UniqueRandomString().getString());
// Same value for now
rt.setReqtVal(loc.toString());
// Set the callback URL
rt.setCallback(callback);
//oauthResMgr.createConsumer(null, cons);
oauthResMgr.createRequestToken(null, rt);
String resp = OAUTH_TOKEN + "=" + rt.getReqtVal() + "&" + OAUTH_TOKEN_SECRET + "=" + rt.getReqtSecret() + "&" + OAUTH_CALLBACK_CONFIRMED + "=true";
return Response.created(loc).entity(resp).type(MediaType.APPLICATION_FORM_URLENCODED).build();
} catch (OAuthServiceException e) {
Logger.getLogger(RequestTokenRequest.class.getName()).log(Level.SEVERE, null, e);
throw new WebApplicationException(e);
}
}
use of javax.ws.rs.WebApplicationException in project OpenAM by OpenRock.
the class RequestTokenResource method deleteReqtoken.
@DELETE
@Consumes(MediaType.TEXT_PLAIN)
public Response deleteReqtoken() {
OAuthResourceManager oauthResMgr = OAuthResourceManager.getInstance();
try {
String tokenuri = context.getAbsolutePath().toString();
Map<String, String> searchMap = new HashMap<String, String>();
searchMap.put(REQUEST_TOKEN_URI, tokenuri);
List<RequestToken> reqTokens = oauthResMgr.searchRequestTokens(searchMap);
RequestToken token = null;
if ((reqTokens != null) && (!reqTokens.isEmpty())) {
token = reqTokens.get(0);
}
if (token == null) {
return Response.status(UNAUTHORIZED).build();
}
oauthResMgr.deleteRequestToken(token);
return Response.ok().build();
} catch (OAuthServiceException e) {
Logger.getLogger(RequestTokenResource.class.getName()).log(Level.SEVERE, null, e);
throw new WebApplicationException(e);
}
}
use of javax.ws.rs.WebApplicationException in project graylog2-server by Graylog2.
the class ClusterSystemResource method jvm.
@GET
@Timed
@ApiOperation(value = "Get JVM information of the given node")
@Path("{nodeId}/jvm")
public SystemJVMResponse jvm(@ApiParam(name = "nodeId", value = "The id of the node where processing will be paused.", required = true) @PathParam("nodeId") String nodeId) throws IOException, NodeNotFoundException {
final Node targetNode = nodeService.byNodeId(nodeId);
final RemoteSystemResource remoteSystemResource = remoteInterfaceProvider.get(targetNode, this.authenticationToken, RemoteSystemResource.class);
final Response<SystemJVMResponse> response = remoteSystemResource.jvm().execute();
if (response.isSuccessful()) {
return response.body();
} else {
LOG.warn("Unable to get jvm information on node {}: {}", nodeId, response.message());
throw new WebApplicationException(response.message(), BAD_GATEWAY);
}
}
use of javax.ws.rs.WebApplicationException in project graylog2-server by Graylog2.
the class ClusterSystemShutdownResource method shutdown.
@POST
@Timed
@ApiOperation(value = "Shutdown node gracefully.", notes = "Attempts to process all buffered and cached messages before exiting, " + "shuts down inputs first to make sure that no new messages are accepted.")
@AuditEvent(type = AuditEventTypes.NODE_SHUTDOWN_INITIATE)
public void shutdown(@ApiParam(name = "nodeId", value = "The id of the node to shutdown.", required = true) @PathParam("nodeId") String nodeId) throws IOException, NodeNotFoundException {
final Node targetNode = nodeService.byNodeId(nodeId);
RemoteSystemShutdownResource remoteSystemShutdownResource = remoteInterfaceProvider.get(targetNode, this.authenticationToken, RemoteSystemShutdownResource.class);
final Response response = remoteSystemShutdownResource.shutdown().execute();
if (response.code() != ACCEPTED.getCode()) {
LOG.warn("Unable send shut down signal to node {}: {}", nodeId, response.message());
throw new WebApplicationException(response.message(), BAD_GATEWAY);
}
}
use of javax.ws.rs.WebApplicationException in project graylog2-server by Graylog2.
the class ClusterSystemPluginResource method list.
@GET
@Timed
@ApiOperation(value = "List all installed plugins on the given node")
public PluginList list(@ApiParam(name = "nodeId", value = "The id of the node where processing will be paused.", required = true) @PathParam("nodeId") String nodeId) throws IOException, NodeNotFoundException {
final Node targetNode = nodeService.byNodeId(nodeId);
final RemoteSystemPluginResource remoteSystemPluginResource = remoteInterfaceProvider.get(targetNode, this.authenticationToken, RemoteSystemPluginResource.class);
final Response<PluginList> response = remoteSystemPluginResource.list().execute();
if (response.isSuccessful()) {
return response.body();
} else {
LOG.warn("Unable to get plugin list on node {}: {}", nodeId, response.message());
throw new WebApplicationException(response.message(), BAD_GATEWAY);
}
}
Aggregations