use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class TetheringServerHandler method connectControlChannel.
/**
* Sends control commands to the client.
*/
@GET
@Path("/tethering/controlchannels/{peer}")
public void connectControlChannel(FullHttpRequest request, HttpResponder responder, @PathParam("peer") String peer, @QueryParam("messageId") String messageId) throws IOException, NotImplementedException, PeerNotFoundException, ForbiddenException, BadRequestException {
checkTetheringServerEnabled();
store.updatePeerTimestamp(peer);
TetheringStatus tetheringStatus = store.getPeer(peer).getTetheringStatus();
if (tetheringStatus == TetheringStatus.PENDING) {
throw new PeerNotFoundException(String.format("Peer %s not found", peer));
} else if (tetheringStatus == TetheringStatus.REJECTED) {
responder.sendStatus(HttpResponseStatus.FORBIDDEN);
throw new ForbiddenException(String.format("Peer %s is not authorized", peer));
}
List<TetheringControlResponse> controlResponses = new ArrayList<>();
MessageFetcher fetcher = messagingContext.getMessageFetcher();
TopicId topic = new TopicId(NamespaceId.SYSTEM.getNamespace(), topicPrefix + peer);
String lastMessageId = messageId;
try (CloseableIterator<Message> iterator = fetcher.fetch(topic.getNamespace(), topic.getTopic(), 1, messageId)) {
while (iterator.hasNext()) {
Message message = iterator.next();
TetheringControlMessage controlMessage = GSON.fromJson(message.getPayloadAsString(StandardCharsets.UTF_8), TetheringControlMessage.class);
lastMessageId = message.getId();
controlResponses.add(new TetheringControlResponse(lastMessageId, controlMessage));
}
} catch (TopicNotFoundException e) {
LOG.warn("Received control connection from peer {} that's not tethered", peer);
} catch (IllegalArgumentException e) {
throw new BadRequestException(String.format("Invalid message id %s", messageId));
}
if (controlResponses.isEmpty()) {
controlResponses.add(new TetheringControlResponse(lastMessageId, new TetheringControlMessage(TetheringControlMessage.Type.KEEPALIVE)));
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(controlResponses.toArray(new TetheringControlResponse[0]), TetheringControlResponse[].class));
}
use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class LineageClient method getLineage.
private <T> T getLineage(NamespacedEntityId namespacedId, String path, Class<T> type) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
URL lineageURL = config.resolveNamespacedURLV3(new NamespaceId(namespacedId.getNamespace()), path);
HttpResponse response = restClient.execute(HttpRequest.get(lineageURL).build(), config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseBodyAsString());
}
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(response.getResponseBodyAsString());
}
return GSON.fromJson(response.getResponseBodyAsString(), type);
}
use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class MonitorClient method setSystemServiceInstances.
/**
* Sets the number of instances the system service is running on.
*
* @param serviceName name of the system service
* @param instances number of instances the system service is running on
* @throws IOException if a network error occurred
* @throws NotFoundException if the system service with the specified name was not found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void setSystemServiceInstances(String serviceName, int instances) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveURL(String.format("system/services/%s/instances", serviceName));
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(new SystemServiceId(serviceName));
} else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(new String(response.getResponseBody()));
}
}
use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class AppMetadataStore method getProgramRunCounts.
/**
* Get the run counts of the given program collections.
*
* @param programIds the collection of program ids to get the program
* @return the map of the program id to its run count
*/
public Map<ProgramId, Long> getProgramRunCounts(Collection<ProgramId> programIds) throws BadRequestException, IOException {
if (programIds.size() > 100) {
throw new BadRequestException(String.format("%d programs found, the maximum number supported is 100", programIds.size()));
}
Map<ProgramId, Long> result = programIds.stream().collect(Collectors.toMap(id -> id, id -> 0L, (v1, v2) -> 0L, LinkedHashMap::new));
List<List<Field<?>>> multiKeys = programIds.stream().map(id -> getProgramCountPrimaryKeys(TYPE_COUNT, id)).collect(Collectors.toList());
for (StructuredRow row : getProgramCountsTable().multiRead(multiKeys)) {
ProgramId programId = getApplicationIdFromRow(row).program(ProgramType.valueOf(row.getString(StoreDefinition.AppMetadataStore.PROGRAM_TYPE_FIELD)), row.getString(StoreDefinition.AppMetadataStore.PROGRAM_FIELD));
result.put(programId, row.getLong(StoreDefinition.AppMetadataStore.COUNTS));
}
return result;
}
use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class DefaultPreviewManager method getProgramIdFromRequest.
private ProgramId getProgramIdFromRequest(ApplicationId preview, AppRequest<?> request) throws BadRequestException {
PreviewConfig previewConfig = request.getPreview();
if (previewConfig == null) {
throw new BadRequestException("Preview config cannot be null");
}
String programName = previewConfig.getProgramName();
ProgramType programType = previewConfig.getProgramType();
if (programName == null || programType == null) {
throw new IllegalArgumentException("ProgramName or ProgramType cannot be null.");
}
return preview.program(programType, programName);
}
Aggregations