Search in sources :

Example 81 with ByteString

use of com.linkedin.data.ByteString in project voldemort by voldemort.

the class R2Store method get.

@Override
public List<Versioned<byte[]>> get(ByteArray key, byte[] transforms) throws VoldemortException {
    List<Versioned<byte[]>> resultList = new ArrayList<Versioned<byte[]>>();
    String base64Key = RestUtils.encodeVoldemortKey(key.get());
    RestRequestBuilder rb = null;
    try {
        rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + base64Key));
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_OP_CODE));
        rb.setHeader("Accept", MULTIPART_CONTENT_TYPE);
        RestResponse response = fetchGetResponse(rb, timeoutStr);
        final ByteString entity = response.getEntity();
        if (entity != null) {
            resultList = parseGetResponse(entity);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Did not get any response!");
            }
        }
    } catch (ExecutionException e) {
        if (e.getCause() instanceof RestException) {
            RestException exception = (RestException) e.getCause();
            if (logger.isDebugEnabled()) {
                logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
            }
        } else {
            throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e);
        }
    } catch (InterruptedException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Operation interrupted : " + e.getMessage(), e);
        }
        throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e);
    } catch (URISyntaxException e) {
        throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e);
    }
    return resultList;
}
Also used : Versioned(voldemort.versioning.Versioned) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) ArrayList(java.util.ArrayList) RestException(com.linkedin.r2.message.rest.RestException) ByteString(com.linkedin.data.ByteString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VoldemortException(voldemort.VoldemortException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 82 with ByteString

use of com.linkedin.data.ByteString in project voldemort by voldemort.

the class CoordinatorAdminClient method getStoreClientConfigString.

public String getStoreClientConfigString(List<String> storeNames, String coordinatorUrl) {
    try {
        // Create the REST request
        StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl).append(URL_SEPARATOR).append(STORE_CLIENT_CONFIG_OPS).append(URL_SEPARATOR).append(Joiner.on(",").join(storeNames));
        RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString()));
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_OP_CODE));
        requestBuilder.setMethod(requestType.GET.toString());
        requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
        requestBuilder = setCommonRequestHeader(requestBuilder);
        RestRequest request = requestBuilder.build();
        Future<RestResponse> future = client.restRequest(request);
        // This will block
        RestResponse response = future.get();
        ByteString entity = response.getEntity();
        return entity.asString("UTF-8");
    } catch (Exception e) {
        if (e.getCause() instanceof RestException) {
            return ((RestException) e.getCause()).getResponse().getEntity().asString("UTF-8");
        }
        handleRequestAndResponseException(e);
    }
    return null;
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) RestException(com.linkedin.r2.message.rest.RestException) VoldemortException(voldemort.VoldemortException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 83 with ByteString

use of com.linkedin.data.ByteString in project voldemort by voldemort.

the class CoordinatorAdminClient method deleteStoreClientConfig.

public boolean deleteStoreClientConfig(List<String> storeNames, String coordinatorUrl) {
    String responseMessage = null;
    Boolean success = false;
    try {
        // Create the REST request
        StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl).append(URL_SEPARATOR).append(STORE_CLIENT_CONFIG_OPS).append(URL_SEPARATOR).append(Joiner.on(",").join(storeNames));
        RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString()));
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_OP_CODE));
        // Create a HTTP POST request
        requestBuilder.setMethod(requestType.DELETE.toString());
        requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
        requestBuilder = setCommonRequestHeader(requestBuilder);
        RestRequest request = requestBuilder.build();
        Future<RestResponse> future = client.restRequest(request);
        // This will block
        RestResponse response = future.get();
        final ByteString entity = response.getEntity();
        if (entity == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Empty response !");
            }
            responseMessage = "Received empty response from " + coordinatorUrl;
        } else {
            responseMessage = entity.asString("UTF-8");
            success = true;
        }
    } catch (Exception e) {
        if (e.getCause() instanceof RestException) {
            responseMessage = ((RestException) e.getCause()).getResponse().getEntity().asString("UTF-8");
        } else {
            responseMessage = "An exception other than RestException happens!";
        }
        handleRequestAndResponseException(e);
    } finally {
        System.out.println(responseMessage);
    }
    return success;
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) RestException(com.linkedin.r2.message.rest.RestException) VoldemortException(voldemort.VoldemortException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 84 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestMapTemplate method testBytesMap.

@Test
public void testBytesMap() {
    MapDataSchema schema = (MapDataSchema) DataTemplateUtil.parseSchema("{ \"type\" : \"map\", \"values\" : \"bytes\" }");
    Map<String, ByteString> input = asMap("one", ByteString.copyAvroString("1", false), "three", ByteString.copyAvroString("3", false), "five", ByteString.copyAvroString("5", false), "seven", ByteString.copyAvroString("7", false), "eleven", ByteString.copyAvroString("11", false));
    Map<String, ByteString> adds = asMap("thirteen", ByteString.copyAvroString("13", false), "seventeen", ByteString.copyAvroString("17", false), "nineteen", ByteString.copyAvroString("19", false));
    Map<String, Object> badInput = asMap("boolean", true, "integer", 99, "long", 999L, "float", 88.0f, "double", 888.0, "data", "\u0100", "object", new Object(), "null", null, "array", new StringArray(), "record", new FooRecord());
    Map<String, Object> badOutput = asMap("boolean", true, "integer", 99, "long", 999L, "float", 88.0f, "double", 888.0, "data", "\u0100", "map", new DataMap(), "list", new DataList());
    testMap(BytesMap.class, schema, input, adds);
    testMapBadInput(BytesMap.class, schema, input, badInput, badOutput);
}
Also used : DataList(com.linkedin.data.DataList) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 85 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestDynamicRecordTemplate method TestPrimitiveFieldsOnDynamicRecord.

@Test
public void TestPrimitiveFieldsOnDynamicRecord() {
    DynamicFoo foo = new DynamicFoo();
    foo.setBoolean(true);
    Assert.assertEquals(true, foo.getBoolean());
    foo.setInt(54);
    Assert.assertEquals(54, foo.getInt());
    foo.setFloat(5.67F);
    Assert.assertEquals(5.67F, foo.getFloat());
    foo.setDouble(4.45);
    Assert.assertEquals(4.45, foo.getDouble());
    foo.setLong(12345L);
    Assert.assertEquals(12345L, foo.getLong());
    ByteString byteString = ByteString.copyAvroString("someString", false);
    foo.setBytes(byteString);
    Assert.assertEquals(byteString, foo.getBytes());
    foo.setString("myString");
    Assert.assertEquals("myString", foo.getString());
    foo.setEnum(TestRecordAndUnionTemplate.EnumType.BANANA);
    Assert.assertEquals(TestRecordAndUnionTemplate.EnumType.BANANA, foo.getEnum());
}
Also used : ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Aggregations

ByteString (com.linkedin.data.ByteString)152 Test (org.testng.annotations.Test)77 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 MimeMultipart (javax.mail.internet.MimeMultipart)31 MimeBodyPart (javax.mail.internet.MimeBodyPart)26 DataMap (com.linkedin.data.DataMap)25 RestResponse (com.linkedin.r2.message.rest.RestResponse)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)22 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)22 RestRequest (com.linkedin.r2.message.rest.RestRequest)21 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)21 URI (java.net.URI)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 RequestContext (com.linkedin.r2.message.RequestContext)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)18 Callback (com.linkedin.common.callback.Callback)17 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)14 RestException (com.linkedin.r2.message.rest.RestException)12 HashMap (java.util.HashMap)12 DataList (com.linkedin.data.DataList)11