private void validateAuthorizedRequestResult(String authorizedUser, EntityEnclosingMethod method, int expectedStatusCode) throws Exception { if(method.getStatusLine() == null){ throw new Exception("Failed to send request, status line is not available"); } else if(method.getStatusCode()==401){ throw new IOException("Unauthorized, this user have no rights to publish packages as "+authorizedUser+", or API key is invalid"); } else if(method.getStatusCode() != expectedStatusCode){ throw new Exception(method.getStatusCode()+" : "+ method.getStatusText()); } }
private void writeCurrencyViaJson(String key, String description, int rank) throws IOException { EntityEnclosingMethod put = new PutMethod(restEndpoint + "/" + key); ObjectNode currency = MAPPER.createObjectNode(); currency.put(TYPE, getEntityName()); currency.put("description", description); currency.put("rank", rank); put.setRequestEntity(new StringRequestEntity(currency.toString(), "application/json", "UTF-8")); restClient.executeMethod(put); System.out.println(put.getResponseBodyAsString()); assertEquals(put.getStatusCode(), HttpStatus.SC_OK); }
getHttpClient().executeMethod(method); final InputStream is = method.getResponseBodyAsStream(); if (method.getStatusCode() != 200 && method.getStatusCode() != 201) { throw new ProponoException("ERROR HTTP status=" + method.getStatusCode() + " : " + Utilities.streamToString(is));
void write() throws Exception { EntityEnclosingMethod post = new PostMethod(getEndpoint(this.cacheName) + "/" + this.key); if (this.keyContentType != null) { post.addRequestHeader("Key-Content-Type", this.keyContentType); } if (this.value instanceof byte[]) { String contentType = this.contentType == null ? APPLICATION_OCTET_STREAM_TYPE : this.contentType; post.setRequestEntity(new ByteArrayRequestEntity((byte[]) this.value, contentType)); } else { String payload = this.value.toString(); String contentType = this.contentType == null ? TEXT_PLAIN_TYPE : this.contentType; post.setRequestEntity(new StringRequestEntity(payload, contentType, UTF_8.toString())); } restClient.executeMethod(post); assertEquals(post.getStatusCode(), HttpStatus.SC_OK); }
if (method.getStatusCode() != 200 && method.getStatusCode() != 201) { throw new ProponoException("ERROR HTTP status=" + method.getStatusCode() + " : " + Utilities.streamToString(is)); throw new ProponoException("ERROR: saving media entry"); if (method.getStatusCode() != 201) { throw new ProponoException("ERROR HTTP status=" + method.getStatusCode());
getHttpClient().executeMethod(method); final InputStream is = method.getResponseBodyAsStream(); code = method.getStatusCode(); if (code != 200 && code != 201) { throw new ProponoException("ERROR HTTP status=" + code + " : " + Utilities.streamToString(is));
if (method.getStatusCode() == 200 || method.getStatusCode() == 201) { final Entry romeEntry = Atom10Parser.parseEntry(new InputStreamReader(is), col.getHrefResolved(), Locale.US); BeanUtils.copyProperties(this, romeEntry); throw new ProponoException("ERROR HTTP status-code=" + method.getStatusCode() + " status-line: " + method.getStatusLine());
public void testRestPutEmbeddedHotRodGet() throws Exception { final String key = "1"; // 1. Put with REST EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity( "<hey>ho</hey>".getBytes(), MediaType.TEXT_PLAIN_TYPE)); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded assertEquals("<hey>ho</hey>", cacheFactory.getEmbeddedCache().get(key)); // 3. Get with Hot Rod assertEquals("<hey>ho</hey>", cacheFactory.getHotRodCache().get(key)); }
public void testRestPutEmbeddedHotRodGet() throws Exception { final String key = "1"; // 1. Put with REST EntityEnclosingMethod put = new PutMethod(cacheFactory1.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity( "<hey>ho</hey>".getBytes(), MediaType.TEXT_PLAIN_TYPE)); HttpClient restClient = cacheFactory1.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded Cache embeddedCache = cacheFactory2.getEmbeddedCache().getAdvancedCache(); assertEquals("<hey>ho</hey>", embeddedCache.get(key)); // 3. Get with Hot Rod assertEquals("<hey>ho</hey>", cacheFactory2.getHotRodCache().get(key)); }
public void testRestPutStringHotRodGet() throws Exception { final String key = "1"; // 1. Put text content with REST EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new StringRequestEntity("<hey>ho</hey>", "text/plain", "UTF-8")); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 3. Get with Hot Rod assertEquals("<hey>ho</hey>", cacheFactory.getHotRodCache().get(key)); final String newKey = "2"; final String newValue = "<let's>go</let's>"; //4. Put text content with Hot Rod RemoteCache<String, Object> hotRodCache = cacheFactory.getHotRodCache(); hotRodCache.put(newKey, newValue); //5. Read with rest HttpMethod get = new GetMethod(cacheFactory.getRestUrl() + "/" + newKey); cacheFactory.getRestClient().executeMethod(get); assertEquals(HttpStatus.SC_OK, get.getStatusCode()); assertEquals(newValue, get.getResponseBodyAsString()); }
public void testRestPutEmbeddedMemcachedHotRodGetTest() throws Exception { final String key = "3"; // 1. Put with REST EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity( "<hey>ho</hey>".getBytes(), MediaType.TEXT_PLAIN_TYPE)); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded (given a marshaller, it can unmarshall the result) assertEquals("<hey>ho</hey>", cacheFactory.getEmbeddedCache().get(key)); // 3. Get with Memcached (given a marshaller, it can unmarshall the result) assertEquals("<hey>ho</hey>", cacheFactory.getMemcachedClient().get(key)); // 4. Get with Hot Rod (given a marshaller, it can unmarshall the result) assertEquals("<hey>ho</hey>", cacheFactory.getHotRodCache().get(key)); }
@Test public void testRestPutEmbeddedMemcachedHotRodGetTest() throws Exception { final String key = "3"; final Object value = "<hey>ho</hey>"; final Marshaller marshaller = cacheFactory.getMarshaller(); // 1. Put with REST byte[] bytes = marshaller.objectToByteBuffer(value); EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity(bytes, marshaller.mediaType().toString())); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded (given a marshaller, it can unmarshall the result) assertEquals(value, cacheFactory.getEmbeddedCache().get(key)); // 3. Get with Memcached (given a marshaller, it can unmarshall the result) assertEquals(value, cacheFactory.getMemcachedClient().get(key)); // 4. Get with Hot Rod assertEquals(value, cacheFactory.getHotRodCache().get(key)); } }
@Override protected RemoteCacheManager createRemoteCacheManager() throws IOException { RemoteCacheManager remoteCacheManager = new RemoteCacheManager(new org.infinispan.client.hotrod.configuration.ConfigurationBuilder() .addServer().host("localhost").port(hotRodServer.getPort()) .marshaller(new ProtoStreamMarshaller()) .build()); //initialize client-side serialization context SerializationContext serializationContext = ProtoStreamMarshaller.getSerializationContext(remoteCacheManager); ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder(); String protoFile = protoSchemaBuilder.fileName("crypto.proto") .addClass(CryptoCurrency.class) .build(serializationContext); //initialize server-side serialization context via rest endpoint String metadataCacheEndpoint = String.format("http://localhost:%s/rest/%s", restServer.getPort(), PROTOBUF_METADATA_CACHE_NAME); EntityEnclosingMethod put = new PutMethod(metadataCacheEndpoint + "/crypto.proto"); put.setRequestEntity(new StringRequestEntity(protoFile, "text/plain", "UTF-8")); restClient.executeMethod(put); assertEquals(put.getStatusCode(), HttpStatus.SC_OK); return remoteCacheManager; }