Search in sources :

Example 1 with BadRequestException

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

the class MetadataHttpHandler method readArray.

private String[] readArray(HttpRequest request) throws BadRequestException {
    ChannelBuffer content = request.getContent();
    if (!content.readable()) {
        throw new BadRequestException("Unable to read a list of tags from the request.");
    }
    List<String> toReturn;
    try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(content), Charsets.UTF_8)) {
        toReturn = GSON.fromJson(reader, LIST_STRING_TYPE);
    } catch (IOException e) {
        throw new BadRequestException("Unable to read a list of tags from the request.", e);
    }
    if (toReturn == null) {
        throw new BadRequestException("Null tags were read from the request.");
    }
    return toReturn.toArray(new String[toReturn.size()]);
}
Also used : InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 2 with BadRequestException

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

the class LineageHandler method parseRange.

// TODO: CDAP-3715 This is a fairly common operation useful in various handlers
private TimeRange parseRange(String startStr, String endStr) throws BadRequestException {
    if (startStr == null) {
        throw new BadRequestException("Start time is required.");
    }
    if (endStr == null) {
        throw new BadRequestException("End time is required.");
    }
    long now = TimeMathParser.nowInSeconds();
    long start;
    long end;
    try {
        // start and end are specified in seconds from HTTP request,
        // but specified in milliseconds to the LineageGenerator
        start = TimeUnit.SECONDS.toMillis(TimeMathParser.parseTimeInSeconds(now, startStr));
        end = TimeUnit.SECONDS.toMillis(TimeMathParser.parseTimeInSeconds(now, endStr));
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    }
    if (start < 0) {
        throw new BadRequestException(String.format("Invalid start time (%s -> %d), should be >= 0.", startStr, start));
    }
    if (end < 0) {
        throw new BadRequestException(String.format("Invalid end time (%s -> %d), should be >= 0.", endStr, end));
    }
    if (start > end) {
        throw new BadRequestException(String.format("Start time (%s -> %d) should be lesser than end time (%s -> %d).", startStr, start, endStr, end));
    }
    return new TimeRange(start, end);
}
Also used : BadRequestException(co.cask.cdap.common.BadRequestException)

Example 3 with BadRequestException

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

the class LineageHandler method getCollapseTypes.

private static Set<CollapseType> getCollapseTypes(@Nullable List<String> collapse) throws BadRequestException {
    if (collapse == null) {
        return Collections.emptySet();
    }
    Set<CollapseType> collapseTypes = new HashSet<>();
    for (String c : collapse) {
        try {
            CollapseType type = CollapseType.valueOf(c.toUpperCase());
            collapseTypes.add(type);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(String.format("Invalid collapse type %s", c));
        }
    }
    return collapseTypes;
}
Also used : CollapseType(co.cask.cdap.proto.metadata.lineage.CollapseType) BadRequestException(co.cask.cdap.common.BadRequestException) HashSet(java.util.HashSet)

Example 4 with BadRequestException

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

the class AppFabricClient method getProgramSchedules.

public List<ScheduleDetail> getProgramSchedules(String namespace, String app, String workflow) throws NotFoundException {
    MockResponder responder = new MockResponder();
    String uri = String.format("%s/apps/%s/workflows/%s/schedules", getNamespacePath(namespace), app, workflow);
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    try {
        workflowHttpHandler.getWorkflowSchedules(request, responder, namespace, app, workflow, null);
    } catch (BadRequestException e) {
        // cannot happen
        throw Throwables.propagate(e);
    }
    List<ScheduleDetail> schedules = responder.decodeResponseContent(SCHEDULE_DETAILS_TYPE, GSON);
    verifyResponse(HttpResponseStatus.OK, responder.getStatus(), "Getting workflow schedules failed");
    return schedules;
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) BadRequestException(co.cask.cdap.common.BadRequestException) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail)

Example 5 with BadRequestException

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

the class MetadataHttpHandlerTestRun method testInvalidSearchParams.

@Test
public void testInvalidSearchParams() throws Exception {
    NamespaceId namespace = new NamespaceId("invalid");
    Set<EntityTypeSimpleName> targets = EnumSet.allOf(EntityTypeSimpleName.class);
    try {
        searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.ENTITY_NAME_KEY);
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with bad sort field
    try {
        searchMetadata(namespace, "*", targets, "name asc");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with bad sort order
    try {
        searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.ENTITY_NAME_KEY + " unknown");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with numCursors for relevance sort
    try {
        searchMetadata(NamespaceId.DEFAULT, "search*", targets, null, 0, Integer.MAX_VALUE, 1, null);
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with cursor for relevance sort
    try {
        searchMetadata(NamespaceId.DEFAULT, "search*", targets, null, 0, Integer.MAX_VALUE, 0, "cursor");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with invalid query
    try {
        searchMetadata(NamespaceId.DEFAULT, "");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Test(org.junit.Test)

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