private void checkLevels(int levels) throws BadRequestException { if (levels < 1) { throw new BadRequestException(String.format("Invalid levels (%d), should be greater than 0.", levels)); } }
@Provider public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestException> { public Response toResponse(BadRequestException ex) { return Response.status(404). entity(ex.getMessage()). type("text/plain"). build(); } }
/************************************************ GET ************************************************************/ @GET @Path("metadata/{dataset}") public void getMetadata(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("dataset") String dataset) { Map<MetadataScope, Metadata> metadata = null; try { metadata = getContext().getMetadata(MetadataEntity.ofDataset(getContext().getNamespace(), dataset)); } catch (Exception e) { if (e.getCause() instanceof UnauthorizedException) { responder.sendStatus(((UnauthorizedException) e.getCause()).getStatusCode()); } else if (e.getCause() instanceof UnauthenticatedException) { responder.sendStatus(((UnauthenticatedException) e.getCause()).getStatusCode()); } else if (e.getCause() instanceof BadRequestException) { responder.sendStatus(((BadRequestException) e.getCause()).getStatusCode()); } else { responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()); } } responder.sendJson(HttpResponseStatus.OK.code(), metadata); }
private void checkLevels(int levels) throws BadRequestException { if (levels < 1) { throw new BadRequestException(String.format("Invalid levels (%d), should be greater than 0.", levels)); } }
import org.apache.log4j.Logger; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @Provider public class BadRequestExceptionMapper implements ExceptionMapper { private static final Logger log = Logger.getLogger(BadRequestExceptionMapper.class); @Override public Response toResponse(BadRequestException e) { log.info("Bad request: " + e.getMessage()); return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); } }
private void validateResponse(HttpURLConnection urlConn) throws Exception { int responseCode = urlConn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { return; } String errorMessage = new String(ByteStreams.toByteArray(urlConn.getErrorStream()), Charsets.UTF_8); if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) { throw new BadRequestException(errorMessage); } throw new Exception("Spark execution service request failed: " + errorMessage); } }
@Controller @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) public class SomeController { ... @ExceptionHandler(BadRequestException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public @ResponseBody Map<String,Object> handleIndexNotFoundException(BadRequestException bre, HttpServletRequest request, HttpServletResponse resp) { HashMap<String, Object> result = new HashMap<>(); result.put("error", true); result.put("error_message", bre.getMessage()); return result; } }
private void validateResponse(HttpURLConnection urlConn) throws Exception { int responseCode = urlConn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { return; } String errorMessage = new String(ByteStreams.toByteArray(urlConn.getErrorStream()), Charsets.UTF_8); if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) { throw new BadRequestException(errorMessage); } throw new Exception("Spark execution service request failed: " + errorMessage); } }
@POST @Path("/data/datasets/{name}/admin/drop") public void drop(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String instanceName) throws Exception { propagateUserId(request); InternalDatasetDropParams params = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), InternalDatasetDropParams.class); Preconditions.checkArgument(params.getInstanceSpec() != null, "Missing required 'instanceSpec' parameter."); Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter."); DatasetSpecification spec = params.getInstanceSpec(); DatasetTypeMeta typeMeta = params.getTypeMeta(); try { datasetAdminService.drop(new DatasetId(namespaceId, instanceName), typeMeta, spec); responder.sendJson(HttpResponseStatus.OK, GSON.toJson(spec)); } catch (BadRequestException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } }
static List<? extends EntityId> strings2ProgramIds(List<String> strings) throws BadRequestException { try { return Lists.transform(strings, new Function<String, EntityId>() { @Nullable @Override public EntityId apply(@Nullable String input) { if (input == null || input.isEmpty()) { return null; } return ProgramId.fromString(input); } }); } catch (IllegalArgumentException | NullPointerException e) { throw new BadRequestException(e.getMessage()); } }
@POST @Path("/data/datasets/{name}/admin/drop") public void drop(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String instanceName) throws Exception { propagateUserId(request); InternalDatasetDropParams params = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), InternalDatasetDropParams.class); Preconditions.checkArgument(params.getInstanceSpec() != null, "Missing required 'instanceSpec' parameter."); Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter."); DatasetSpecification spec = params.getInstanceSpec(); DatasetTypeMeta typeMeta = params.getTypeMeta(); try { datasetAdminService.drop(new DatasetId(namespaceId, instanceName), typeMeta, spec); responder.sendJson(HttpResponseStatus.OK, GSON.toJson(spec)); } catch (BadRequestException e) { responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } }
static List<? extends EntityId> strings2ProgramIds(List<String> strings) throws BadRequestException { try { return Lists.transform(strings, new Function<String, EntityId>() { @Nullable @Override public EntityId apply(@Nullable String input) { if (input == null || input.isEmpty()) { return null; } return ProgramId.fromString(input); } }); } catch (IllegalArgumentException | NullPointerException e) { throw new BadRequestException(e.getMessage()); } }
private static void verifyAlreadyExist(NamespaceMeta namespaceMeta, NamespaceId existingNamespace) throws Exception { try { namespaceAdmin.create(namespaceMeta); Assert.fail(String.format("Namespace '%s' should not have been created", namespaceMeta.getName())); } catch (BadRequestException e) { Assert.assertTrue(e.getMessage().contains(existingNamespace.getNamespace())); } }
private void validateResponse(HttpURLConnection urlConn) throws Exception { int responseCode = urlConn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { return; } String errorMessage = new String(ByteStreams.toByteArray(urlConn.getErrorStream()), Charsets.UTF_8); if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) { throw new BadRequestException(errorMessage); } throw new Exception("Spark execution service request failed: " + errorMessage); } }
private String getBundleVersion(File file) throws BadRequestException, IOException { try (JarFile jarFile = new JarFile(file)) { Manifest manifest = jarFile.getManifest(); if (manifest == null) { throw new BadRequestException( "Unable to derive version from artifact because it does not contain a manifest. " + "Please package the jar with a manifest, or explicitly specify the artifact version."); } Attributes attributes = manifest.getMainAttributes(); String version = attributes == null ? null : attributes.getValue(ManifestFields.BUNDLE_VERSION); if (version == null) { throw new BadRequestException( "Unable to derive version from artifact because manifest does not contain Bundle-Version attribute. " + "Please include Bundle-Version in the manifest, or explicitly specify the artifact version."); } return version; } catch (ZipException e) { throw new BadRequestException("Artifact is not in zip format. Please make sure it is a jar file."); } }
private String getBundleVersion(File file) throws BadRequestException, IOException { try (JarFile jarFile = new JarFile(file)) { Manifest manifest = jarFile.getManifest(); if (manifest == null) { throw new BadRequestException( "Unable to derive version from artifact because it does not contain a manifest. " + "Please package the jar with a manifest, or explicitly specify the artifact version."); } Attributes attributes = manifest.getMainAttributes(); String version = attributes == null ? null : attributes.getValue(ManifestFields.BUNDLE_VERSION); if (version == null) { throw new BadRequestException( "Unable to derive version from artifact because manifest does not contain Bundle-Version attribute. " + "Please include Bundle-Version in the manifest, or explicitly specify the artifact version."); } return version; } catch (ZipException e) { throw new BadRequestException("Artifact is not in zip format. Please make sure it is a jar file."); } }
private ArtifactScope validateScope(String scope) throws BadRequestException { try { return ArtifactScope.valueOf(scope.toUpperCase()); } catch (IllegalArgumentException e) { throw new BadRequestException("Invalid scope " + scope); } }
private ArtifactId validateAndGetArtifactId(NamespaceId namespace, String name, String version) throws BadRequestException { try { return namespace.artifact(name, version); } catch (Exception e) { throw new BadRequestException(e.getMessage()); } }
private ProfileId getValidatedProfile(NamespaceId namespaceId, String profileName) throws BadRequestException { try { return namespaceId.profile(profileName); } catch (IllegalArgumentException e) { throw new BadRequestException(e.getMessage(), e); } }
static DatasetTypeId toDatasetTypeId(String namespace, String typeName) throws BadRequestException { try { return new DatasetTypeId(namespace, typeName); } catch (IllegalArgumentException | NullPointerException e) { throw new BadRequestException(e.getMessage()); } }