Search in sources :

Example 21 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class ArtifactClient method deleteProperty.

/**
 * Delete a property for an artifact. If the property does not exist, this will be a no-op.
 *
 * @param artifactId the artifact to delete a property from
 * @param key the property to delete
 * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws ArtifactNotFoundException if the artifact does not exist
 * @throws IOException if a network error occurred
 */
public void deleteProperty(ArtifactId artifactId, String key) throws IOException, UnauthenticatedException, ArtifactNotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s/properties/%s", artifactId.getArtifact(), artifactId.getVersion(), key);
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpRequest.Builder requestBuilder = HttpRequest.delete(url);
    HttpRequest request = requestBuilder.build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 22 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class ArtifactClient method deleteProperties.

/**
 * Delete all properties for an artifact. If no properties exist, this will be a no-op.
 *
 * @param artifactId the artifact to delete properties from
 * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws ArtifactNotFoundException if the artifact does not exist
 * @throws IOException if a network error occurred
 */
public void deleteProperties(ArtifactId artifactId) throws IOException, UnauthenticatedException, ArtifactNotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s/properties", artifactId.getArtifact(), artifactId.getVersion());
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpRequest.Builder requestBuilder = HttpRequest.delete(url);
    HttpRequest request = requestBuilder.build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 23 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class ArtifactClient method writeProperties.

/**
 * Write properties for an artifact. Any existing properties will be overwritten.
 *
 * @param artifactId the artifact to add properties to
 * @param properties the properties to add
 * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws ArtifactNotFoundException if the artifact does not exist
 * @throws IOException if a network error occurred
 */
public void writeProperties(ArtifactId artifactId, Map<String, String> properties) throws IOException, UnauthenticatedException, ArtifactNotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s/properties", artifactId.getArtifact(), artifactId.getVersion());
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpRequest.Builder requestBuilder = HttpRequest.put(url);
    HttpRequest request = requestBuilder.withBody(GSON.toJson(properties)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 24 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class ApplicationClient method addSchedule.

/**
 * Add a schedule to an application.
 *
 * @param app the application
 * @param scheduleDetail the schedule to be added
 */
public void addSchedule(ApplicationId app, ScheduleDetail scheduleDetail) throws ApplicationNotFoundException, IOException, UnauthenticatedException, UnauthorizedException, BadRequestException {
    String path = String.format("apps/%s/versions/%s/schedules/%s", app.getApplication(), app.getVersion(), scheduleDetail.getName());
    HttpResponse response = restClient.execute(HttpMethod.PUT, config.resolveNamespacedURLV3(app.getParent(), path), GSON.toJson(scheduleDetail), ImmutableMap.<String, String>of(), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(app);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(String.format("Bad Request. Reason: %s", response.getResponseBodyAsString()));
    }
}
Also used : ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException)

Example 25 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class DatasetModuleClient method add.

/**
 * Adds a new dataset module.
 *
 * @param module the new dataset module
 * @param className name of the dataset module class within the moduleJarFile
 * @param moduleJarFile Jar file containing the dataset module class and dependencies
 * @throws BadRequestException if the moduleJarFile does not exist
 * @throws AlreadyExistsException if a dataset module with the same name already exists
 * @throws IOException if a network error occurred
 */
public void add(DatasetModuleId module, String className, File moduleJarFile) throws BadRequestException, AlreadyExistsException, IOException, UnauthenticatedException {
    URL url = config.resolveNamespacedURLV3(module.getParent(), String.format("data/modules/%s", module.getModule()));
    Map<String, String> headers = ImmutableMap.of("X-Class-Name", className);
    HttpRequest request = HttpRequest.put(url).addHeaders(headers).withBody(moduleJarFile).build();
    HttpResponse response = restClient.upload(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_CONFLICT);
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(String.format("Module jar file does not exist: %s", moduleJarFile));
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
        throw new DatasetModuleAlreadyExistsException(module);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) URL(java.net.URL) DatasetModuleAlreadyExistsException(io.cdap.cdap.common.DatasetModuleAlreadyExistsException)

Aggregations

BadRequestException (io.cdap.cdap.common.BadRequestException)188 Path (javax.ws.rs.Path)68 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)54 IOException (java.io.IOException)46 JsonSyntaxException (com.google.gson.JsonSyntaxException)44 NotFoundException (io.cdap.cdap.common.NotFoundException)42 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)42 POST (javax.ws.rs.POST)42 HttpResponse (io.cdap.common.http.HttpResponse)36 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)34 URL (java.net.URL)34 ProgramType (io.cdap.cdap.proto.ProgramType)30 InputStreamReader (java.io.InputStreamReader)28 Reader (java.io.Reader)28 ArrayList (java.util.ArrayList)28 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)26 ProgramId (io.cdap.cdap.proto.id.ProgramId)26 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)24 GET (javax.ws.rs.GET)24 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)22