private boolean isMigrationFinishNotice(Operation patch) { if (patch.getBodyRaw() instanceof String) { MigrationFinishedNotice doc = Utils.fromJson(patch.getBodyRaw(), MigrationFinishedNotice.class); return MigrationFinishedNotice.KIND.equals(doc.documentKind); } return patch.getBodyRaw() instanceof MigrationFinishedNotice; }
protected void fail(AdapterRequest request, Operation o, Throwable e) { if (o != null && o.getBodyRaw() != null) { String errMsg = String.format("%s; Reason: %s", e.getMessage(), Utils.toJson(o.getBodyRaw())); e = new Exception(errMsg, e); } fail(request, e); }
@Override public Operation setBodyNoCloning(Object b) { if (b instanceof ServiceErrorResponse) { ((ServiceErrorResponse) b).message += " original body: " + super.getBodyRaw(); } return super.setBodyNoCloning(b); } };
@Override public void handlePatch(Operation patch) { MockTaskState body = patch.getBody(MockTaskState.class); if (patch.getBodyRaw() instanceof ServiceTaskCallbackResponse) { body.callbackResponse = patch.getBodyRaw(); } setState(patch, body); patch.complete(); }
protected Throwable fail(AdapterRequest request, Operation o, Throwable e) { if (o != null && o.getBodyRaw() != null) { e = exceptionFromFailedDockerOperation(o, e); } fail(request, e); return e; }
public static byte[] encodeBody(Operation op, boolean isRequest) throws Exception { return encodeBody(op, op.getBodyRaw(), op.getContentType(), isRequest); }
private int getErrorCode(Operation o) { if (o.hasBody() && o.getContentType().equals(Operation.MEDIA_TYPE_APPLICATION_JSON)) { Object obj = o.getBodyRaw(); if (obj instanceof ServiceErrorResponse) { ServiceErrorResponse rsp = (ServiceErrorResponse)obj; return rsp.getErrorCode(); } } return 0; }
/** * Sends an asynchronous request and returns the eventual response body as deferred result. * @param op The request to send. * @param resultType The expected type of the response body. * @return Deferred result with the resultType state of the response body or * <code>null</code> if body is empty. */ default <T> DeferredResult<T> sendWithDeferredResult(Operation op, Class<T> resultType) { return sendWithDeferredResult(op) .thenApply(response -> response.getBodyRaw() == null ? null : response.getBody(resultType) ); }
public static void failForwardedRequest(Operation op, Operation fo, Throwable fe) { op.setStatusCode(fo.getStatusCode()); op.setBodyNoCloning(fo.getBodyRaw()).setContentType(MEDIA_TYPE_APPLICATION_JSON).fail(fe); }
Object operationDecoder(Operation completedOp, MethodInfo httpMethod) { if (httpMethod.getType() instanceof ParameterizedType) { String json = Utils.toJson(completedOp.getBodyRaw()); return Utils.fromJson(json, httpMethod.getType()); } else { return completedOp.getBody(httpMethod.getReturnType()); } }
private void getExternalPopularImages(Operation get, Runnable notFoundHandler) { sendRequest(Operation.createGet(UriUtils.buildUri(getHost(), EXTERNAL_LINK)) .setCompletion((op, ex) -> { if (ex != null || op.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) { notFoundHandler.run(); } else { get.setBody(op.getBodyRaw()); get.setStatusCode(op.getStatusCode()); get.setContentType(Operation.MEDIA_TYPE_APPLICATION_JSON); get.complete(); } })); }
private void forwardToLucene(QueryTask task, Operation get) { try { Operation localPatch = Operation.createPatch(UriUtils.buildUri(getHost(), task.indexLink)) .setBodyNoCloning(task) .setCompletion((o, e) -> { if (e == null) { task.results = (ServiceDocumentQueryResult) o.getBodyRaw(); } handleQueryCompletion(task, e, get); }); sendRequest(localPatch); } catch (Exception e) { handleQueryCompletion(task, e, get); } }
private void startChildService(Operation post, String path, Service childService) { getHost().startService( Operation.createPost(UriUtils.extendUri(post.getUri(), path)) .setBody(post.getBodyRaw()), childService ); childServices.add(childService); }
private void startChildService(Operation post, String path, Service childService) { getHost().startService( Operation.createPost(UriUtils.extendUri(post.getUri(), path)) .setBody(post.getBodyRaw()), childService ); childServices.add(childService); }
private void startChildService(Operation post, String path, Service childService) { getHost().startService( Operation.createPost(UriUtils.extendUri(post.getUri(), path)) .setBody(post.getBodyRaw()), childService ); childServices.add(childService); }
private void sendSynchronizationRequest( String factoryLink, Operation factorySynchPost, Operation op) { URI uri = UriUtils.buildUri(factorySynchPost.getUri(), factoryLink); uri = UriUtils.extendUri(uri, ServiceHost.SERVICE_URI_SUFFIX_SYNCHRONIZATION); factorySynchPost.setUri(uri); factorySynchPost.setCompletion((o, e) -> { if (e != null) { this.log(Level.WARNING, "Failed to start factory synchronization: %s", e); op.fail(e); return; } op.setBody(o.getBodyRaw()); op.complete(); }).sendWith(this); }
private void verifyOp(Operation op, SerializedOperation sop) { assertEquals(op.getAction(), sop.action); assertEquals(op.getUri().getHost(), sop.host); assertEquals(op.getUri().getPort(), sop.port); assertEquals(op.getUri().getPath(), sop.path); assertEquals(op.getUri().getQuery(), sop.query); assertEquals(op.getId(), sop.id.longValue()); assertEquals(op.getReferer(), sop.referer); assertEquals(op.getBodyRaw(), sop.jsonBody); assertEquals(op.getStatusCode(), sop.statusCode); assertEquals(op.getOptions(), sop.options); assertEquals(op.getContextId(), sop.contextId); assertEquals(op.getTransactionId(), sop.transactionId); assertEquals(op.getUri().getUserInfo(), sop.userInfo); assertEquals(SerializedOperation.KIND, sop.documentKind); assertEquals(op.getExpirationMicrosUtc(), sop.documentExpirationTimeMicros); }
private void performAndVerify304Response(String servicePath, Action action, boolean isRemote) { NotModifiedOwnerSelectedService.State dummyBody = new NotModifiedOwnerSelectedService.State(); Operation op = Operation.createGet(this.host, servicePath).setBody(dummyBody); op.setAction(action); if (isRemote) { op.forceRemote(); } Operation resultOp = this.sender.sendAndWait(op); assertNull(resultOp.getBodyRaw()); assertEquals(isRemote, resultOp.isRemote()); assertEquals(STATUS_CODE_NOT_MODIFIED, resultOp.getStatusCode()); }
private void performAndVerify304Response(String servicePath, Action action, boolean isRemote) { NotModifiedOwnerSelectedService.State dummyBody = new NotModifiedOwnerSelectedService.State(); Operation op = Operation.createGet(this.host, servicePath).setBody(dummyBody); op.setAction(action); if (isRemote) { op.forceRemote(); } Operation resultOp = this.sender.sendAndWait(op); assertNull(resultOp.getBodyRaw()); assertEquals(isRemote, resultOp.isRemote()); assertEquals(STATUS_CODE_NOT_MODIFIED, resultOp.getStatusCode()); }
@Test public void testRootUiService() throws Throwable { setUp(false); // Stopping the RootNamespaceService this.host.waitForResponse(Operation .createDelete(UriUtils.buildUri(this.host, UriUtils.URI_PATH_CHAR))); class RootUiService extends UiFileContentService { public static final String SELF_LINK = UriUtils.URI_PATH_CHAR; } // Starting the CustomUiService service this.host.startServiceAndWait(new RootUiService(), RootUiService.SELF_LINK, null); // Loading the default page Operation result = this.host.waitForResponse(Operation .createGet(UriUtils.buildUri(this.host, RootUiService.SELF_LINK))); assertEquals("<html><title>Root</title></html>", result.getBodyRaw()); }