@Override public Observable<AstrixServiceInvocationResponse> submitRoutedRequest( final AstrixServiceInvocationRequest request, final RoutingKey routingKey) { ClusterMember clusterMember = getTargetMember(routingKey); final HttpPost postRequest = new HttpPost(clusterMember.getRemoteEndpointUri()); postRequest.setEntity(new SerializableEntity(request)); return Observable.create(new OnSubscribe<AstrixServiceInvocationResponse>() { @Override public void call(final Subscriber<? super AstrixServiceInvocationResponse> t1) { try { httpclient.execute(postRequest, serviceResponseCallback(t1)); } catch (Exception e) { t1.onError(e); } } }); }
@Override public Observable<List<AstrixServiceInvocationResponse>> submitBroadcastRequest( AstrixServiceInvocationRequest request) { Observable<AstrixServiceInvocationResponse> result = Observable.empty(); for (ClusterMember clusterMember : getAllClusterMembers()) { final HttpPost postRequest = new HttpPost(clusterMember.getRemoteEndpointUri()); postRequest.setEntity(new SerializableEntity(request)); result = result.mergeWith(Observable.create(new OnSubscribe<AstrixServiceInvocationResponse>() { @Override public void call(final Subscriber<? super AstrixServiceInvocationResponse> t1) { try { httpclient.execute(postRequest, serviceResponseCallback(t1)); } catch (Exception e) { t1.onError(e); } } })); } return result.toList(); }
@Override public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) { response.setStatusCode(HttpStatus.SC_OK); final Map<String, String> parameters = RequestHandlingUtilities .parseParameter(request); try { response.setEntity(new SerializableEntity( (HashMap<String, String>) parameters, false)); } catch (final Exception e) { response.setEntity(new StringEntity("ERROR", ContentType.DEFAULT_TEXT)); } }
@Override public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) { response.setStatusCode(HttpStatus.SC_OK); final Map<String, String> parameters = RequestHandlingUtilities .parseCookies(request); try { response.setEntity(new SerializableEntity( (HashMap<String, String>) parameters, false)); } catch (final Exception e) { response.setEntity(new StringEntity("ERROR", ContentType.DEFAULT_TEXT)); } }
@Override public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) { try { final List<FileItem> files = RequestFileHandlingUtilities .handleFileUpload(request); final HashMap<String, String> fileMap = new HashMap<String, String>(); for (final FileItem fileItem : files) { fileMap.put(fileItem.getName(), Files .getCanonicalPath(((DiskFileItem) fileItem) .getStoreLocation())); } response.setEntity(new SerializableEntity(fileMap, false)); fileItems.addAll(files); } catch (Exception e) { response.setEntity(new StringEntity("ERROR", ContentType.DEFAULT_TEXT)); } }
/** * <p>Test for detachment of the inherited serializer.</p> * * @since 1.3.0 */ @Test public final void testDetachSerializer() throws ParseException, IOException { String subpath = "/detach"; User user = new User(1, "Roy", "Mustang", 30, false); stubFor(put(urlEqualTo(subpath)) .willReturn(aResponse() .withStatus(200))); serializerEndpoint.detachSerializer(user); verify(putRequestedFor(urlEqualTo(subpath)) .withRequestBody(equalTo(EntityUtils.toString(new SerializableEntity(user, true))))); }
/** * <p>Test for a {@link Request} with a {@link Serializable} entity.</p> * * @since 1.3.0 */ @Test public final void testSerializableEntity() throws ParseException, IOException { String subpath = "/serializableentity"; User entity = new User(1L, "Eren", "Yeager", 15, false); SerializableEntity se = new SerializableEntity(entity, true); stubFor(put(urlEqualTo(subpath)) .willReturn(aResponse() .withStatus(200))); requestEndpoint.serializableEntity(entity); verify(putRequestedFor(urlEqualTo(subpath)) .withRequestBody(equalTo(EntityUtils.toString(se)))); }
/** * <p>Test for a {@link Request} with a {@link Serializable} entity.</p> * * @since 1.3.0 */ @Test public final void testSerializableEntity() throws ParseException, IOException { Robolectric.getFakeHttpLayer().interceptHttpRequests(false); String subpath = "/serializableentity"; User entity = new User(1L, "Eren", "Yeager", 15, false); SerializableEntity se = new SerializableEntity(entity, true); stubFor(put(urlEqualTo(subpath)) .willReturn(aResponse() .withStatus(200))); requestEndpoint.serializableEntity(entity); verify(putRequestedFor(urlEqualTo(subpath)) .withRequestBody(equalTo(EntityUtils.toString(se)))); }
/** * This method tests the @link{HouseRestController#populate()} API with a valid client token * token * @throws ClientProtocolException * @throws IOException */ @Test(groups="rest") public void populateTest() throws ClientProtocolException, IOException { String token = "abcebb42-af86-11e1-ad74-005056b4006f"; String url = TestServers.getHouse() + "devices/populate/" + token; HttpPost method = new HttpPost(url); HttpEntity entity = new SerializableEntity( (Serializable) new HashMap<String, Object>(), false); method.setEntity(entity); HttpClient httpClient = new DefaultHttpClient(); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpClient.execute(method, responseHandler); Assert.assertTrue(!responseBody.isEmpty()); }
/** * This method tests the @link{HouseRestController#populate()} API with a invalid * client token * @throws ClientProtocolException * @throws IOException */ @Test(groups="rest") public void populateInvalidTest() throws ClientProtocolException, IOException { String invalidClientToken = "xyz"; String url = TestServers.getHouse() + "devices/populate/" + invalidClientToken; HttpPost method = new HttpPost(url); HttpEntity entity = new SerializableEntity( (Serializable) new HashMap<String, Object>(), false); method.setEntity(entity); HttpClient httpClient = new DefaultHttpClient(); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpClient.execute(method, responseHandler); Assert.assertEquals(responseBody, "0"); }
/** * <p>Discovers the {@link HttpEntity} which is suitable for wrapping an instance of the given {@link Class}. * This discovery proceeds in the following order by checking the provided generic type:</p> * * <ol> * <li>org.apache.http.{@link HttpEntity} --> returned as-is.</li> * <li>{@code byte[]}, {@link Byte}[] --> {@link ByteArrayEntity}</li> * <li>java.io.{@link File} --> {@link FileEntity}</li> * <li>java.io.{@link InputStream} --> {@link BufferedHttpEntity}</li> * <li>{@link CharSequence} --> {@link StringEntity}</li> * <li>java.io.{@link Serializable} --> {@link SerializableEntity} (with an internal buffer)</li> * </ol> * * @param genericType * a generic {@link Class} to be translated to an {@link HttpEntity} type * <br><br> * @return the {@link Class} of the translated {@link HttpEntity} * <br><br> * @throws NullPointerException * if the supplied generic type was {@code null} * <br><br> * @throws EntityResolutionFailedException * if the given generic {@link Class} failed to be translated to an {@link HttpEntity} type * <br><br> * @since 1.3.0 */ public static final Class<?> resolve(Class<?> genericType) { assertNotNull(genericType); try { Class<?> entityType = HttpEntity.class.isAssignableFrom(genericType)? HttpEntity.class : (byte[].class.isAssignableFrom(genericType) || Byte[].class.isAssignableFrom(genericType))? ByteArrayEntity.class: File.class.isAssignableFrom(genericType)? FileEntity.class : InputStream.class.isAssignableFrom(genericType)? BufferedHttpEntity.class : CharSequence.class.isAssignableFrom(genericType)? StringEntity.class : Serializable.class.isAssignableFrom(genericType)? SerializableEntity.class: null; if(entityType == null) { throw new EntityResolutionFailedException(genericType); } return entityType; } catch(Exception e) { throw (e instanceof EntityResolutionFailedException)? (EntityResolutionFailedException)e :new EntityResolutionFailedException(genericType, e); } }
/** * <p>Test for detachment of the inherited serializer.</p> * * @since 1.3.0 */ @Test public final void testDetachSerializer() throws ParseException, IOException { Robolectric.getFakeHttpLayer().interceptHttpRequests(false); String subpath = "/detach"; User user = new User(1, "Roy", "Mustang", 30, false); stubFor(put(urlEqualTo(subpath)) .willReturn(aResponse() .withStatus(200))); serializerEndpoint.detachSerializer(user); verify(putRequestedFor(urlEqualTo(subpath)) .withRequestBody(equalTo(EntityUtils.toString(new SerializableEntity(user, true))))); }