use of javax.ws.rs.WebApplicationException in project presto by prestodb.
the class ThrowableMapper method toResponse.
@Override
public Response toResponse(Throwable throwable) {
if (throwable instanceof WebApplicationException) {
return ((WebApplicationException) throwable).getResponse();
}
log.warn(throwable, "Request failed for %s", request.getRequestURI());
ResponseBuilder responseBuilder = Response.serverError().header(CONTENT_TYPE, TEXT_PLAIN);
if (includeExceptionInResponse) {
responseBuilder.entity(Throwables.getStackTraceAsString(throwable));
} else {
responseBuilder.entity("Exception processing request");
}
return responseBuilder.build();
}
use of javax.ws.rs.WebApplicationException in project platformlayer by platformlayer.
the class ServiceAuthorizationResource method createService.
// We deliberately don't support this at the moment... it's quite restrictive on our data store
// @GET
// @Produces({ APPLICATION_XML, APPLICATION_JSON })
// public <T> ServiceAuthorizationCollection getAll() {
// List<ServiceAuthorization> items = authorizationRepository.getByAccountId(getAccountId());
// ServiceAuthorizationCollection collection = new ServiceAuthorizationCollection();
// collection.items = items;
// return collection;
// }
@POST
@Consumes({ XML, JSON })
@Produces({ XML, JSON })
public <T> ServiceAuthorization createService(final ServiceAuthorization authorization) throws OpsException, RepositoryException {
ServiceType serviceType = getServiceType();
authorization.serviceType = serviceType.getKey();
final ServiceProvider serviceProvider = opsSystem.getServiceProvider(serviceType);
if (serviceProvider == null) {
log.warn("Unknown serviceProvider: " + serviceType);
throw new WebApplicationException(404);
}
String data = authorization.data;
if (Strings.isNullOrEmpty(data)) {
throw new IllegalArgumentException("Data is required");
}
data = data.trim();
if (data.startsWith("{")) {
// Convert to XML
SettingCollection settings = new SettingCollection();
settings.items = Lists.newArrayList();
// We presume it's a simple map of keys and values
try {
JSONObject json = new JSONObject(data);
@SuppressWarnings("unchecked") Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
String value = json.getString(key);
Setting setting = new Setting();
setting.key = key;
setting.value = value;
settings.items.add(setting);
}
} catch (JSONException e) {
throw new IllegalArgumentException("Error parsing data", e);
}
JaxbHelper jaxbHelper = JaxbHelper.get(SettingCollection.class);
String xml;
try {
xml = jaxbHelper.marshal(settings, false);
} catch (JAXBException e) {
throw new IllegalArgumentException("Error converting JSON to XML", e);
}
authorization.data = xml;
}
// Authentication authentication = getAuthentication();
//
// OpsContextBuilder opsContextBuilder = opsSystem.getInjector().getInstance(OpsContextBuilder.class);
// final OpsContext opsContext = opsContextBuilder.buildOpsContext(serviceType, authentication, false);
//
// OpsContext.runInContext(opsContext, new CheckedCallable<Object, Exception>() {
// @Override
// public Object call() throws Exception {
// serviceProvider.validateAuthorization(authorization);
// return null;
// }
// });
// serviceProvider.validateAuthorization(authorization);
ServiceAuthorization created = authorizationRepository.createAuthorization(getProject(), authorization);
// For security, never return the data
created.data = null;
return created;
}
use of javax.ws.rs.WebApplicationException in project platformlayer by platformlayer.
the class ServiceResource method getSshPublicKey.
@GET
@Path("sshkey")
@Produces({ TEXT_PLAIN })
public String getSshPublicKey() throws RepositoryException, OpsException, IOException {
final ServiceProvider serviceProvider = getServiceProvider();
if (serviceProvider == null) {
raiseNotFound();
}
OpsContextBuilder opsContextBuilder = opsContextBuilderFactory.get();
final OpsContext opsContext = opsContextBuilder.buildTemporaryOpsContext(serviceProvider.getServiceType(), getProjectAuthorization());
PublicKey publicKey = OpsContext.runInContext(opsContext, new CheckedCallable<PublicKey, Exception>() {
@Override
public PublicKey call() throws Exception {
PublicKey publicKey = serviceProvider.getSshPublicKey();
return publicKey;
}
});
if (publicKey == null) {
throw new WebApplicationException(404);
}
String description = "platformlayer://" + getProject().getKey() + "/" + serviceProvider.getServiceType().getKey();
return OpenSshUtils.serialize(publicKey, description);
}
use of javax.ws.rs.WebApplicationException in project platformlayer by platformlayer.
the class BlobsResource method downloadBlob.
@GET
@Path("blob/{blobId}")
public Response downloadBlob(@PathParam("blobId") String blobId) {
Download download = oneTimeDownloads.get(blobId);
if (download == null) {
throw new WebApplicationException(Status.NOT_FOUND);
}
String responseData = download.getContent();
String contentType = download.getContentType();
ResponseBuilder rBuild = Response.ok(responseData, contentType);
return rBuild.build();
}
use of javax.ws.rs.WebApplicationException in project helios by spotify.
the class HostsResource method list.
/**
* Returns the list of hostnames of known hosts/agents.
* @return The list of hostnames.
*/
@GET
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public List<String> list(@QueryParam("namePattern") final String namePattern, @QueryParam("selector") final List<String> hostSelectors) {
List<String> hosts = model.listHosts();
if (namePattern != null) {
final Predicate<String> matchesPattern = Pattern.compile(namePattern).asPredicate();
hosts = hosts.stream().filter(matchesPattern).collect(Collectors.toList());
}
if (!hostSelectors.isEmpty()) {
// check that all supplied selectors are parseable/valid
final List<HostSelector> selectors = hostSelectors.stream().map(selectorStr -> {
final HostSelector parsed = HostSelector.parse(selectorStr);
if (parsed == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Invalid host selector: " + selectorStr).build());
}
return parsed;
}).collect(Collectors.toList());
final Map<String, Map<String, String>> hostsAndLabels = getLabels(hosts);
final HostMatcher matcher = new HostMatcher(hostsAndLabels);
hosts = matcher.getMatchingHosts(selectors);
}
return hosts;
}
Aggregations