Search in sources :

Example 56 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class StreamHandlerTest method createStream.

private HttpResponse createStream(StreamId streamId, int... allowedErrorCodes) throws Exception {
    URL url = createURL(streamId.getNamespace(), "streams/" + streamId.getEntityName());
    HttpRequest request = HttpRequest.put(url).build();
    HttpResponse response = HttpRequests.execute(request);
    int responseCode = response.getResponseCode();
    if (!ArrayUtils.contains(allowedErrorCodes, responseCode)) {
        Assert.assertEquals(200, responseCode);
    }
    return response;
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 57 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class AbstractNamespaceQueryClient method list.

@Override
public List<NamespaceMeta> list() throws Exception {
    HttpRequest request = HttpRequest.get(resolve("namespaces")).build();
    HttpResponse response = execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return ObjectResponse.fromJsonBody(response, new TypeToken<List<NamespaceMeta>>() {
        }).getResponseObject();
    }
    throw new IOException(String.format("Cannot list namespaces. Reason: %s", response.getResponseBodyAsString()));
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TypeToken(com.google.common.reflect.TypeToken) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException)

Example 58 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ClientMessagingService method createTopic.

@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
    TopicId topicId = topicMetadata.getTopicId();
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createTopicPath(topicId)).withBody(GSON.toJson(topicMetadata.getProperties())).build();
    HttpResponse response = remoteClient.execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
        throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to create topic " + topicId);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) TopicId(co.cask.cdap.proto.id.TopicId) TopicAlreadyExistsException(co.cask.cdap.api.messaging.TopicAlreadyExistsException)

Example 59 with HttpRequest

use of co.cask.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 {
    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();
    Encoder encoder = EncoderFactory.get().directBinaryEncoder(os, null);
    DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
    datumWriter.write(record, encoder);
    // Make the publish request
    String writeType = publish ? "publish" : "store";
    TopicId topicId = request.getTopicId();
    HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/" + writeType).addHeader(HttpHeaders.CONTENT_TYPE, "avro/binary").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(co.cask.common.http.HttpRequest) Encoder(org.apache.avro.io.Encoder) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) GenericRecord(org.apache.avro.generic.GenericRecord) TopicId(co.cask.cdap.proto.id.TopicId) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 60 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ClientMessagingService method deleteTopic.

@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException {
    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(co.cask.common.http.HttpRequest) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse)

Aggregations

HttpRequest (co.cask.common.http.HttpRequest)97 URL (java.net.URL)75 HttpResponse (co.cask.common.http.HttpResponse)71 Test (org.junit.Test)22 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)13 BadRequestException (co.cask.cdap.common.BadRequestException)10 NotFoundException (co.cask.cdap.common.NotFoundException)9 ApplicationManager (co.cask.cdap.test.ApplicationManager)9 ServiceManager (co.cask.cdap.test.ServiceManager)9 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)6 IOException (java.io.IOException)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)5 TypeToken (com.google.common.reflect.TypeToken)5 TypeToken (com.google.gson.reflect.TypeToken)5 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)4 Instances (co.cask.cdap.proto.Instances)4 SparkManager (co.cask.cdap.test.SparkManager)4 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)3