Search in sources :

Example 11 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class StreamClient method setStreamProperties.

/**
   * Sets properties of a stream.
   *
   * @param stream ID of the stream
   * @param properties properties to set
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the client is unauthorized
   * @throws BadRequestException if the request is bad
   * @throws StreamNotFoundException if the stream was not found
   */
public void setStreamProperties(StreamId stream, StreamProperties properties) throws IOException, UnauthenticatedException, BadRequestException, StreamNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(stream.getParent(), String.format("streams/%s/properties", stream.getStream()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(properties)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException("Bad request: " + response.getResponseBodyAsString());
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new StreamNotFoundException(stream);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) URL(java.net.URL)

Example 12 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ConversionHelpers method getInstanceConfiguration.

static DatasetInstanceConfiguration getInstanceConfiguration(HttpRequest request) throws BadRequestException {
    Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()), Charsets.UTF_8);
    try {
        DatasetInstanceConfiguration config = GSON.fromJson(reader, DatasetInstanceConfiguration.class);
        Preconditions.checkNotNull(config.getTypeName(), "The typeName must be specified.");
        return config;
    } catch (JsonSyntaxException | NullPointerException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) DatasetInstanceConfiguration(co.cask.cdap.proto.DatasetInstanceConfiguration)

Example 13 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class DatasetAdminOpHTTPHandler method create.

@POST
@Path("/data/datasets/{name}/admin/create")
public void create(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) {
    propagateUserId(request);
    InternalDatasetCreationParams params = GSON.fromJson(request.getContent().toString(Charsets.UTF_8), InternalDatasetCreationParams.class);
    Preconditions.checkArgument(params.getProperties() != null, "Missing required 'instanceProps' parameter.");
    Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter.");
    DatasetProperties props = params.getProperties();
    DatasetTypeMeta typeMeta = params.getTypeMeta();
    try {
        DatasetId instanceId = new DatasetId(namespaceId, name);
        DatasetSpecification spec = datasetAdminService.createOrUpdate(instanceId, typeMeta, props, null);
        responder.sendJson(HttpResponseStatus.OK, spec);
    } catch (BadRequestException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Exception e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) DatasetTypeMeta(co.cask.cdap.proto.DatasetTypeMeta) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) BadRequestException(co.cask.cdap.common.BadRequestException) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetId(co.cask.cdap.proto.id.DatasetId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 14 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class DatasetAdminOpHTTPHandler method update.

@POST
@Path("/data/datasets/{name}/admin/update")
public void update(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) {
    propagateUserId(request);
    InternalDatasetUpdateParams params = GSON.fromJson(request.getContent().toString(Charsets.UTF_8), InternalDatasetUpdateParams.class);
    Preconditions.checkArgument(params.getProperties() != null, "Missing required 'instanceProps' parameter.");
    Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter.");
    Preconditions.checkArgument(params.getExistingSpec() != null, "Missing required 'existingSpec' parameter.");
    DatasetProperties props = params.getProperties();
    DatasetSpecification existing = params.getExistingSpec();
    DatasetTypeMeta typeMeta = params.getTypeMeta();
    try {
        DatasetId instanceId = new DatasetId(namespaceId, name);
        DatasetSpecification spec = datasetAdminService.createOrUpdate(instanceId, typeMeta, props, existing);
        responder.sendJson(HttpResponseStatus.OK, spec);
    } catch (NotFoundException e) {
        LOG.debug("Got handler exception", e);
        responder.sendString(HttpResponseStatus.NOT_FOUND, StringUtils.defaultIfEmpty(e.getMessage(), ""));
    } catch (BadRequestException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (IncompatibleUpdateException e) {
        responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage());
    } catch (Exception e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetTypeMeta(co.cask.cdap.proto.DatasetTypeMeta) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetId(co.cask.cdap.proto.id.DatasetId) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 15 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method getRuns.

private void getRuns(HttpResponder responder, ProgramId programId, String status, long start, long end, int limit) throws BadRequestException {
    try {
        ProgramRunStatus runStatus = (status == null) ? ProgramRunStatus.ALL : ProgramRunStatus.valueOf(status.toUpperCase());
        Collection<RunRecord> records = Collections2.transform(store.getRuns(programId, runStatus, start, end, limit).values(), CONVERT_TO_RUN_RECORD);
        responder.sendJson(HttpResponseStatus.OK, records);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(String.format("Invalid status %s. Supported options for status of runs are " + "running/completed/failed", status));
    }
}
Also used : RunRecord(co.cask.cdap.proto.RunRecord) ProgramRunStatus(co.cask.cdap.proto.ProgramRunStatus) BadRequestException(co.cask.cdap.common.BadRequestException)

Aggregations

BadRequestException (co.cask.cdap.common.BadRequestException)82 Path (javax.ws.rs.Path)29 NotFoundException (co.cask.cdap.common.NotFoundException)25 HttpResponse (co.cask.common.http.HttpResponse)17 JsonSyntaxException (com.google.gson.JsonSyntaxException)17 URL (java.net.URL)17 POST (javax.ws.rs.POST)17 NamespaceId (co.cask.cdap.proto.id.NamespaceId)16 IOException (java.io.IOException)15 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)13 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)12 HttpRequest (co.cask.common.http.HttpRequest)12 InputStreamReader (java.io.InputStreamReader)11 Reader (java.io.Reader)11 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)9 ApplicationId (co.cask.cdap.proto.id.ApplicationId)8 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)7 ProgramType (co.cask.cdap.proto.ProgramType)7 ProgramId (co.cask.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6