use of io.cdap.http.HttpResponder in project cdap by caskdata.
the class JsonListResponderTest method testSendForWholeListResponder.
@Test
public void testSendForWholeListResponder() throws IOException {
HttpResponder responder = Mockito.mock(HttpResponder.class);
ChunkResponder chunkResponder = Mockito.mock(ChunkResponder.class);
Mockito.when(responder.sendChunkStart(HttpResponseStatus.OK)).thenReturn(chunkResponder);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Mockito.doAnswer(invocation -> {
ByteBuffers.writeToStream(invocation.getArgumentAt(0, ByteBuffer.class), byteArrayOutputStream);
return null;
}).when(chunkResponder).sendChunk(Mockito.any(ByteBuffer.class));
JsonWholeListResponder.respond(new Gson(), responder, (jsonListResponder) -> {
jsonListResponder.send("application");
});
JsonParser parser = new JsonParser();
JsonArray array = (JsonArray) parser.parse(byteArrayOutputStream.toString());
Assert.assertEquals(array.get(0).getAsString(), "application");
}
use of io.cdap.http.HttpResponder in project cdap by caskdata.
the class JsonListResponderTest method testMultipleSendForPaginatedListResponder.
@Test
public void testMultipleSendForPaginatedListResponder() throws IOException {
HttpResponder responder = Mockito.mock(HttpResponder.class);
ChunkResponder chunkResponder = Mockito.mock(ChunkResponder.class);
Mockito.when(responder.sendChunkStart(HttpResponseStatus.OK)).thenReturn(chunkResponder);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Mockito.doAnswer(invocation -> {
ByteBuffers.writeToStream(invocation.getArgumentAt(0, ByteBuffer.class), byteArrayOutputStream);
return null;
}).when(chunkResponder).sendChunk(Mockito.any(ByteBuffer.class));
JsonPaginatedListResponder.respond(new Gson(), responder, "applications", (jsonListResponder) -> {
jsonListResponder.send("application0");
jsonListResponder.send("application1");
return "nextToken";
});
JsonParser parser = new JsonParser();
JsonObject json = (JsonObject) parser.parse(byteArrayOutputStream.toString());
Assert.assertEquals(json.get("applications").getAsJsonArray().get(0).getAsString(), "application0");
Assert.assertEquals(json.get("applications").getAsJsonArray().get(1).getAsString(), "application1");
Assert.assertEquals(json.get("nextPageToken").getAsString(), "nextToken");
}
use of io.cdap.http.HttpResponder in project cdap by caskdata.
the class RuntimeServiceRoutingHandler method routeServiceWithBody.
/**
* Handles PUT and POST calls from program runtime to access CDAP services.
* It simply verify the request and forward the call to internal CDAP services.
*/
@Path("/services/{service}/**")
@PUT
@POST
public BodyConsumer routeServiceWithBody(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("app") String app, @PathParam("version") String version, @PathParam("program-type") String programType, @PathParam("program") String program, @PathParam("run") String run, @PathParam("service") String service) throws Exception {
HttpURLConnection urlConn = openConnection(request, namespace, app, version, programType, program, run, service);
urlConn.setDoOutput(true);
OutputStream output;
try {
output = urlConn.getOutputStream();
} catch (UnknownServiceException e) {
throw new BadRequestException(e.getMessage(), e);
} catch (IOException e) {
// If fails to get output stream, treat it as service unavailable so that the client can retry
throw new ServiceUnavailableException(service, e);
}
return new BodyConsumer() {
@Override
public void chunk(ByteBuf byteBuf, HttpResponder httpResponder) {
try {
byteBuf.readBytes(output, byteBuf.readableBytes());
} catch (IOException e) {
throw new ServiceUnavailableException(service, e);
}
}
@Override
public void finished(HttpResponder httpResponder) {
try {
output.close();
} catch (IOException e) {
throw new ServiceUnavailableException(service, e);
}
try {
ResponseInfo responseInfo = new ResponseInfo(service, urlConn);
responder.sendContent(HttpResponseStatus.valueOf(responseInfo.getResponseCode()), new RelayBodyProducer(urlConn.getURL(), responseInfo), responseInfo.getHeaders());
} catch (BadRequestException e) {
throw new ServiceException(e, HttpResponseStatus.BAD_REQUEST);
}
}
@Override
public void handleError(Throwable throwable) {
LOG.warn("Exception raised for call to {}", urlConn.getURL(), throwable);
}
};
}
use of io.cdap.http.HttpResponder in project cdap by caskdata.
the class RuntimeHandler method writeSparkEventLogs.
/**
* Handles call for Spark event logs upload.
*/
@Path("/spark-event-logs/{id}")
@POST
public BodyConsumer writeSparkEventLogs(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("app") String app, @PathParam("version") String version, @PathParam("program-type") String programType, @PathParam("program") String program, @PathParam("run") String run, @PathParam("id") String id) throws Exception {
if (!eventLogsEnabled) {
throw new UnsupportedOperationException("Spark event logs collection is not enabled");
}
if (!MediaType.APPLICATION_OCTET_STREAM.equals(request.headers().get(HttpHeaderNames.CONTENT_TYPE))) {
throw new BadRequestException("Only application/octet-stream content type is supported.");
}
ApplicationId appId = new NamespaceId(namespace).app(app, version);
ProgramRunId programRunId = new ProgramRunId(appId, ProgramType.valueOfCategoryName(programType, BadRequestException::new), program, run);
requestValidator.getProgramRunStatus(programRunId, request);
Location location = eventLogsBaseLocation.append(String.format("%s-%s-%s-%s-%s", namespace, app, program, run, id));
if (location.exists()) {
LOG.debug("Deleting event logs location {} for program run {}", location, programRunId);
location.delete(true);
}
OutputStream os = location.getOutputStream();
return new BodyConsumer() {
@Override
public void chunk(ByteBuf request, HttpResponder responder) {
try {
request.readBytes(os, request.readableBytes());
} catch (IOException e) {
throw new RuntimeException("Failed to write spark event logs to " + location, e);
}
}
@Override
public void finished(HttpResponder responder) {
try {
os.close();
responder.sendStatus(HttpResponseStatus.OK);
} catch (IOException e) {
throw new RuntimeException("Failed to close spark event logs output stream for " + location, e);
}
}
@Override
public void handleError(Throwable cause) {
LOG.error("Failed to write spark event logs for {}", programRunId, cause);
Closeables.closeQuietly(os);
try {
location.delete();
} catch (IOException e) {
LOG.error("Failed to delete obsolete event logs location {}", location, e);
}
}
};
}
use of io.cdap.http.HttpResponder in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method doGetSchedules.
private void doGetSchedules(HttpResponder responder, ApplicationId applicationId, @Nullable ProgramId programId, @Nullable String triggerTypeStr, @Nullable String statusStr) throws Exception {
ApplicationSpecification appSpec = store.getApplication(applicationId);
if (appSpec == null) {
throw new NotFoundException(applicationId);
}
ProgramScheduleStatus status;
try {
status = statusStr == null ? null : ProgramScheduleStatus.valueOf(statusStr);
} catch (IllegalArgumentException e) {
throw new BadRequestException(String.format("Invalid schedule status '%s'. Must be one of %s.", statusStr, Joiner.on(',').join(ProgramScheduleStatus.values())), e);
}
ProtoTrigger.Type triggerType;
try {
triggerType = triggerTypeStr == null ? null : ProtoTrigger.Type.valueOfCategoryName(triggerTypeStr);
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage(), e);
}
Predicate<ProgramScheduleRecord> predicate = record -> true;
if (status != null) {
predicate = predicate.and(record -> record.getMeta().getStatus().equals(status));
}
if (triggerType != null) {
predicate = predicate.and(record -> record.getSchedule().getTrigger().getType().equals(triggerType));
}
Collection<ProgramScheduleRecord> schedules;
if (programId != null) {
if (!appSpec.getProgramsByType(programId.getType().getApiProgramType()).contains(programId.getProgram())) {
throw new NotFoundException(programId);
}
schedules = programScheduleService.list(programId, predicate);
} else {
schedules = programScheduleService.list(applicationId, predicate);
}
List<ScheduleDetail> details = schedules.stream().map(ProgramScheduleRecord::toScheduleDetail).collect(Collectors.toList());
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(details, Schedulers.SCHEDULE_DETAILS_TYPE));
}
Aggregations