Search in sources :

Example 56 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class ClientMessagingService method listTopics.

@Override
public List<TopicId> listTopics(NamespaceId namespaceId) throws IOException, UnauthorizedException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, namespaceId.getNamespace() + "/topics").build();
    HttpResponse response = remoteClient.execute(request);
    handleError(response, "Failed to list topics in namespace " + namespaceId);
    List<String> topics = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_LIST_TYPE);
    List<TopicId> result = new ArrayList<>(topics.size());
    for (String topic : topics) {
        result.add(namespaceId.topic(topic));
    }
    return Collections.unmodifiableList(result);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ArrayList(java.util.ArrayList) HttpResponse(io.cdap.common.http.HttpResponse) TopicId(io.cdap.cdap.proto.id.TopicId)

Example 57 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class ClientMessagingService method performWriteRequest.

/**
 * Makes a request to the server for writing to the messaging system
 *
 * @param request contains information about what to write
 * @param publish {@code true} to make publish call, {@code false} to make store call.
 * @return the response from the server
 * @throws IOException if failed to perform the write operation
 * @throws TopicNotFoundException if the topic to write to does not exist
 */
private HttpResponse performWriteRequest(StoreRequest request, boolean publish) throws IOException, TopicNotFoundException, UnauthorizedException {
    GenericRecord record = new GenericData.Record(Schemas.V1.PublishRequest.SCHEMA);
    if (request.isTransactional()) {
        record.put("transactionWritePointer", request.getTransactionWritePointer());
    }
    record.put("messages", convertPayloads(request));
    // Encode the request as avro
    ExposedByteArrayOutputStream os = new ExposedByteArrayOutputStream();
    try (OutputStream encoderOutput = compressOutputStream(os)) {
        Encoder encoder = EncoderFactory.get().directBinaryEncoder(encoderOutput, null);
        DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
        datumWriter.write(record, encoder);
        encoder.flush();
    }
    // Make the publish request
    String writeType = publish ? "publish" : "store";
    TopicId topicId = request.getTopicId();
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaders.CONTENT_TYPE, "avro/binary");
    if (compressPayload) {
        headers.put(HttpHeaders.CONTENT_ENCODING, "gzip");
    }
    HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/" + writeType).addHeaders(headers).withBody(os.toByteBuffer()).build();
    HttpResponse response = remoteClient.execute(httpRequest);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to " + writeType + " message to topic " + topicId);
    return response;
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ExposedByteArrayOutputStream(io.cdap.cdap.internal.io.ExposedByteArrayOutputStream) HashMap(java.util.HashMap) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) ExposedByteArrayOutputStream(io.cdap.cdap.internal.io.ExposedByteArrayOutputStream) HttpResponse(io.cdap.common.http.HttpResponse) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) Encoder(org.apache.avro.io.Encoder) GenericRecord(org.apache.avro.generic.GenericRecord) TopicId(io.cdap.cdap.proto.id.TopicId) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 58 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class ClientMessagingService method getTopic.

@Override
public TopicMetadata getTopic(TopicId topicId) throws TopicNotFoundException, IOException, UnauthorizedException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createTopicPath(topicId)).build();
    HttpResponse response = remoteClient.execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to update topic " + topicId);
    Map<String, String> properties = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_PROPERTY_TYPE);
    return new TopicMetadata(topicId, properties);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata)

Example 59 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class ClientMessagingService method deleteTopic.

@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException, UnauthorizedException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.DELETE, createTopicPath(topicId)).build();
    HttpResponse response = remoteClient.execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to update topic " + topicId);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse)

Example 60 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class RemoteSecureStore method get.

@Override
public SecureStoreData get(String namespace, String name) throws Exception {
    // 1. Get metadata of the secure key
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name) + "/metadata").build();
    HttpResponse response = remoteClient.execute(request);
    handleResponse(response, namespace, name, String.format("Error occurred while getting metadata for key %s:%s", namespace, name));
    SecureStoreMetadata metadata = GSON.fromJson(response.getResponseBodyAsString(), SecureStoreMetadata.class);
    // 2. Get sensitive data for the secure key
    request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name)).build();
    response = remoteClient.execute(request);
    handleResponse(response, namespace, name, String.format("Error occurred while getting key %s:%s", namespace, name));
    // response is not a json object
    byte[] data = response.getResponseBody();
    return new SecureStoreData(metadata, data);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) HttpResponse(io.cdap.common.http.HttpResponse)

Aggregations

HttpRequest (io.cdap.common.http.HttpRequest)124 HttpResponse (io.cdap.common.http.HttpResponse)92 URL (java.net.URL)81 Test (org.junit.Test)33 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 IOException (java.io.IOException)14 AccessToken (io.cdap.cdap.security.authentication.client.AccessToken)13 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)10 BadRequestException (io.cdap.cdap.common.BadRequestException)9 NotFoundException (io.cdap.cdap.common.NotFoundException)8 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)5 ServiceManager (io.cdap.cdap.test.ServiceManager)5 TypeToken (com.google.gson.reflect.TypeToken)4 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)4 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)4 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)4 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)4 TopicId (io.cdap.cdap.proto.id.TopicId)4 ApplicationManager (io.cdap.cdap.test.ApplicationManager)4 ByteBuffer (java.nio.ByteBuffer)4