com.google.apphosting.api.ApiProxy Java Examples
The following examples show how to use
com.google.apphosting.api.ApiProxy.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VmRuntimeJettyAuthTest.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
public void testAuth_AdminRequiredNoUser() throws Exception { String loginUrl = "http://login-url?url=http://test-app.googleapp.com/user/test-auth"; CreateLoginURLResponse loginUrlResponse = new CreateLoginURLResponse(); loginUrlResponse.setLoginUrl(loginUrl); // Fake the expected call to "user/CreateLoginUrl". FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate(); ApiProxy.setDelegate(fakeApiProxy); fakeApiProxy.addApiResponse(loginUrlResponse); HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); GetMethod get = new GetMethod(createUrl("/admin/test-auth").toString()); get.setFollowRedirects(false); int httpCode = httpClient.executeMethod(get); assertEquals(302, httpCode); Header redirUrl = get.getResponseHeader("Location"); assertEquals(loginUrl, redirUrl.getValue()); }
Example #2
Source File: VmRequestThreadFactory.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
/** * Create a new {@link Thread} that executes {@code runnable} for the duration of the current * request. This thread will be interrupted at the end of the current request. * * @param runnable The object whose run method is invoked when this thread is started. If null, * this classes run method does nothing. * * @throws ApiProxy.ApiProxyException If called outside of a running request. * @throws IllegalStateException If called after the request thread stops. */ @Override public Thread newThread(final Runnable runnable) { checkState(requestEnvironment != null, "Request threads can only be created within the context of a running request."); Thread thread = new Thread(new Runnable() { @Override public void run() { if (runnable == null) { return; } checkState(allowNewRequestThreadCreation, "Cannot start new threads after the request thread stops."); ApiProxy.setEnvironmentForCurrentThread(requestEnvironment); runnable.run(); } }); checkState( allowNewRequestThreadCreation, "Cannot create new threads after the request thread stops."); synchronized (mutex) { createdThreads.add(thread); } return thread; }
Example #3
Source File: WorkerServlet.java From io2014-codelabs with Apache License 2.0 | 6 votes |
private void doPolling() { Queue notificationQueue = QueueFactory.getQueue("notification-delivery"); Worker worker = new Worker(notificationQueue); while (!LifecycleManager.getInstance().isShuttingDown()) { boolean tasksProcessed = worker.processBatchOfTasks(); ApiProxy.flushLogs(); if (!tasksProcessed) { // Wait before trying to lease tasks again. try { Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED); } catch (InterruptedException e) { return; } } } log.info("Instance is shutting down"); }
Example #4
Source File: OrphanedJobGraphTest.java From appengine-pipelines with Apache License 2.0 | 6 votes |
@Override public void run() { PipelineService service = PipelineServiceFactory.newPipelineService(); ApiProxy.setEnvironmentForCurrentThread(apiProxyEnvironment); // TODO(user): Try something better than sleep to make sure // this happens after the processing the caller's runTask try { Thread.sleep(1000); } catch (InterruptedException e1) { // ignore - use uninterruptables } try { service.submitPromisedValue(promiseHandle, 0); } catch (NoSuchObjectException e) { throw new RuntimeException(e); } catch (OrphanedObjectException f) { orphanedObjectExcetionCount.incrementAndGet(); } }
Example #5
Source File: GcsServiceFactory.java From appengine-gcs-client with Apache License 2.0 | 6 votes |
static RawGcsService createRawGcsService(Map<String, String> headers) { ImmutableSet.Builder<HTTPHeader> builder = ImmutableSet.builder(); if (headers != null) { for (Map.Entry<String, String> header : headers.entrySet()) { builder.add(new HTTPHeader(header.getKey(), header.getValue())); } } RawGcsService rawGcsService; Value location = SystemProperty.environment.value(); if (location == SystemProperty.Environment.Value.Production || hasCustomAccessTokenProvider()) { rawGcsService = OauthRawGcsServiceFactory.createOauthRawGcsService(builder.build()); } else if (location == SystemProperty.Environment.Value.Development) { rawGcsService = LocalRawGcsServiceFactory.createLocalRawGcsService(); } else { Delegate<?> delegate = ApiProxy.getDelegate(); if (delegate == null || delegate.getClass().getName().startsWith("com.google.appengine.tools.development")) { rawGcsService = LocalRawGcsServiceFactory.createLocalRawGcsService(); } else { rawGcsService = OauthRawGcsServiceFactory.createOauthRawGcsService(builder.build()); } } return rawGcsService; }
Example #6
Source File: VmApiProxyDelegateTest.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
public void testAPIExceptionWrapping() { VmApiProxyDelegate delegate = new VmApiProxyDelegate(createMockHttpClient()); RuntimeException exception = delegate.constructApiException("logservice", "a"); assertEquals(LogServiceException.class, exception.getClass()); assertEquals("RCP Failure for API call: logservice a", exception.getMessage()); exception = delegate.constructApiException("modules", "b"); assertEquals(ModulesException.class, exception.getClass()); assertEquals("RCP Failure for API call: modules b", exception.getMessage()); exception = delegate.constructApiException("datastore_v3", "c"); assertEquals(DatastoreFailureException.class, exception.getClass()); assertEquals("RCP Failure for API call: datastore_v3 c", exception.getMessage()); exception = delegate.constructApiException("barf", "d"); assertEquals(ApiProxy.RPCFailedException.class, exception.getClass()); assertEquals( "The remote RPC to the application server failed for the call barf.d().", exception.getMessage()); }
Example #7
Source File: TestDatagramSocketServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ApiProxy.Delegate oldDelegate = setUpMockDelegate(); response.setContentType("text/plain"); try { testOpenAndClose(response); testConnectWriteAndRead(response); testSocketOpt(response); testSetDatagramSocketImpl(response); testSocketImplConstructor(response); } catch (AssertionFailedException e) { return; //return the error response } finally { ApiProxy.setDelegate(oldDelegate); } response.getWriter().print("Success!"); }
Example #8
Source File: AppIdentityServiceTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testGetDefaultGcsBucketName() { ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); String expectedBucketName; Property property = property("testGetDefaultGcsBucketName"); if (property.exists()) { expectedBucketName = property.getPropertyValue(); } else { expectedBucketName = (String) env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"); } try { String bucketName = appIdentity.getDefaultGcsBucketName(); Assert.assertEquals(expectedBucketName, bucketName); } catch (AppIdentityServiceFailureException aisfe) { //TODO: This means that there is no default bucket setup for this project. Have a better way to verify this. } }
Example #9
Source File: LocalRawGcsService.java From appengine-gcs-client with Apache License 2.0 | 6 votes |
/** * Runs calls in a background thread so that the results will actually be asynchronous. * * @see com.google.appengine.tools.cloudstorage.RawGcsService#continueObjectCreationAsync( * com.google.appengine.tools.cloudstorage.RawGcsService.RawGcsCreationToken, * java.nio.ByteBuffer, long) */ @Override public Future<RawGcsCreationToken> continueObjectCreationAsync(final RawGcsCreationToken token, final ByteBuffer chunk, long timeoutMillis) { try { ensureInitialized(); } catch (IOException e) { throw new RuntimeException(e); } final Environment environment = ApiProxy.getCurrentEnvironment(); return writePool.schedule(new Callable<RawGcsCreationToken>() { @Override public RawGcsCreationToken call() throws Exception { ApiProxy.setEnvironmentForCurrentThread(environment); return append(token, chunk); } }, 50, TimeUnit.MILLISECONDS); }
Example #10
Source File: VmRuntimeWebAppContext.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * * @param throwable an exception associated with this log message, * or {@code null}. */ @Override public void log(String message, Throwable throwable) { StringWriter writer = new StringWriter(); writer.append("javax.servlet.ServletContext log: "); writer.append(message); if (throwable != null) { writer.append("\n"); throwable.printStackTrace(new PrintWriter(writer)); } LogRecord.Level logLevel = throwable == null ? LogRecord.Level.info : LogRecord.Level.error; ApiProxy.log(new ApiProxy.LogRecord(logLevel, System.currentTimeMillis() * 1000L, writer.toString())); }
Example #11
Source File: FakeableVmApiProxyDelegate.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override protected byte[] runSyncCall(VmApiProxyEnvironment environment, String packageName, String methodName, byte[] requestData, int timeoutMs) throws ApiProxy.ApiProxyException { // Lots of tests triggers logging. Ignore calls to the logservice by default. Tests // verifying logging behavior can enable the log api capture calling setIgnoreLogging(false). if (ignoreLogging && "logservice".equals(packageName)) { return new ApiBasePb.VoidProto().toByteArray(); } if ("google.util".equals(packageName) && "Delay".equals(methodName)) { return handleDelayApi(requestData); } requests.add(new ApiRequest(environment, packageName, methodName, requestData)); if (responses.isEmpty()) { throw new RuntimeException( "Got unexpected ApiProxy call to: " + packageName + "/" + methodName); } return responses.removeFirst().toByteArray(); }
Example #12
Source File: VmRuntimeJettyKitchenSinkTest.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
public void testAsyncRequests_WaitUntilDone() throws Exception { long sleepTime = 2000; FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate(); ApiProxy.setDelegate(fakeApiProxy); HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); GetMethod get = new GetMethod(createUrl("/sleep").toString()); get.addRequestHeader("Use-Async-Sleep-Api", "true"); get.addRequestHeader("Sleep-Time", Long.toString(sleepTime)); long startTime = System.currentTimeMillis(); int httpCode = httpClient.executeMethod(get); assertEquals(200, httpCode); Header vmApiWaitTime = get.getResponseHeader(VmRuntimeUtils.ASYNC_API_WAIT_HEADER); assertNotNull(vmApiWaitTime); assertTrue(Integer.parseInt(vmApiWaitTime.getValue()) > 0); long elapsed = System.currentTimeMillis() - startTime; assertTrue(elapsed >= sleepTime); }
Example #13
Source File: UserServiceTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testCreateLoginUrlFederatedNotSetProd() throws Exception { assumeEnvironment(Environment.APPSPOT); // Assuming Authentication Type set to Google Accounts, so org should be blank. String userOrg = ApiProxy.getCurrentEnvironment() .getAttributes() .get("com.google.appengine.api.users.UserService.user_organization").toString(); Assert.assertEquals("", userOrg); String authDomain = "othergaetcktest.org"; String federatedIdentity = "FedIdentTest"; Set<String> attrRequest = new HashSet<>(); // throws IllegalArgumentException since not set to Federated Identity. Exception thrownException = null; try { UserServiceFactory.getUserService().createLoginURL(DEST_URL, authDomain, federatedIdentity, attrRequest); } catch (Exception e) { thrownException = e; } // Testing exception like this since we cannot use the junit annotation in this case. Assert.assertEquals(IllegalArgumentException.class, thrownException.getClass()); }
Example #14
Source File: PushNotificationWorkerServlet.java From solutions-ios-push-notification-sample-backend-java with Apache License 2.0 | 6 votes |
private void doPolling() { Queue notificationQueue = QueueFactory.getQueue("notification-delivery"); PushNotificationWorker worker = new PushNotificationWorker(notificationQueue); while (!LifecycleManager.getInstance().isShuttingDown()) { boolean tasksProcessed = worker.processBatchOfTasks(); ApiProxy.flushLogs(); if (!tasksProcessed) { // Wait before trying to lease tasks again. try { Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED); } catch (InterruptedException e) { return; } } } log.info("Instance is shutting down"); }
Example #15
Source File: VmRuntimeJettyAuthTest.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
public void testAuth_UserRequiredNoUser() throws Exception { String loginUrl = "http://login-url?url=http://test-app.googleapp.com/user/test-auth"; CreateLoginURLResponse loginUrlResponse = new CreateLoginURLResponse(); loginUrlResponse.setLoginUrl(loginUrl); // Fake the expected call to "user/CreateLoginUrl". FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate(); ApiProxy.setDelegate(fakeApiProxy); fakeApiProxy.addApiResponse(loginUrlResponse); HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); GetMethod get = new GetMethod(createUrl("/user/test-auth").toString()); get.setFollowRedirects(false); int httpCode = httpClient.executeMethod(get); assertEquals(302, httpCode); Header redirUrl = get.getResponseHeader("Location"); assertEquals(loginUrl, redirUrl.getValue()); }
Example #16
Source File: VmApiProxyDelegate.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
/** * Convert RemoteApiPb.Response errors to the appropriate exception. * * <p>The response must have exactly one of the RpcError and ApplicationError fields set. * * @param remoteResponse the Response * @param packageName the name of the API package. * @param methodName the name of the method within the API package. * @param logger the Logger used to create log messages. * @return ApiProxyException */ private static ApiProxyException convertRemoteError(RemoteApiPb.Response remoteResponse, String packageName, String methodName, Logger logger) { if (remoteResponse.hasRpcError()) { return convertApiResponseRpcErrorToException( remoteResponse.getRpcError(), packageName, methodName, logger); } // Otherwise it's an application error RemoteApiPb.ApplicationError error = remoteResponse.getApplicationError(); return new ApiProxy.ApplicationException(error.getCode(), error.getDetail()); }
Example #17
Source File: MemcacheSessionStore.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override public void saveSession(String key, SessionData data) throws Retryable { try { memcache.put(key, serialize(data)); } catch (ApiProxy.ApiDeadlineExceededException e) { throw new Retryable(e); } }
Example #18
Source File: AppIdentityBasicTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testRequestId() { ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); String requestId = (String) env.getAttributes().get("com.google.appengine.runtime.request_log_id"); String errMsg = "The request id should not be null"; Assert.assertNotNull(errMsg, requestId); }
Example #19
Source File: VmRuntimeJettyKitchenSink2Test.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
/** * Test that the count servlet was loaded, and that memcache calls are * forwarded through the VmApiProxyDelegate. * * @throws Exception */ public void testCountMemcache() throws Exception { // Replace the API proxy delegate so we can fake API responses. FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate(); ApiProxy.setDelegate(fakeApiProxy); for (int i = 1; i <= 5; i++) { MemcacheIncrementResponse responsePb = MemcacheIncrementResponse.newBuilder() .setIncrementStatus(IncrementStatusCode.OK).setNewValue(i).build(); fakeApiProxy.addApiResponse(responsePb); String[] lines = fetchUrl(createUrl("/count?type=memcache")); assertEquals(1, lines.length); assertEquals("" + i, lines[0].trim()); } }
Example #20
Source File: SessionManager.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
SessionData loadSession(String sessionId) { String key = SESSION_PREFIX + sessionId; SessionData data = null; for (SessionStore sessionStore : sessionStoresInReadOrder) { // Keep iterating until we find a store that has the session data we // want. try { data = sessionStore.getSession(key); if (data != null) { break; } } catch (RuntimeException e) { String msg = "Exception while loading session data"; logger.log(Level.WARNING, msg, e); if (ApiProxy.getCurrentEnvironment() != null) { ApiProxy.log(createWarningLogRecord(msg, e)); } break; } } if (data != null) { if (System.currentTimeMillis() > data.getExpirationTime()) { logger.fine("Session " + sessionId + " expired " + ((System.currentTimeMillis() - data.getExpirationTime()) / 1000) + " seconds ago, ignoring."); return null; } } return data; }
Example #21
Source File: FakeableVmApiProxyDelegate.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
private byte[] handleDelayApi(byte[] requestData) { ApiBasePb.Integer32Proto intRequest = new ApiBasePb.Integer32Proto(); intRequest.parseFrom(requestData); try { Thread.sleep(intRequest.getValue()); } catch (InterruptedException e) { throw new ApiProxy.ApiProxyException("Got unexpected thread interrupt!"); } return new ApiBasePb.VoidProto().toByteArray(); }
Example #22
Source File: AppEngineAuthentication.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
/** * Returns the thread local environment if it is a VmApiProxyEnvironment. * * @return The ThreadLocal environment or null if no VmApiProxyEnvironment is set. */ private VmApiProxyEnvironment getThreadLocalEnvironment() { Environment env = ApiProxy.getCurrentEnvironment(); if (env instanceof VmApiProxyEnvironment) { return (VmApiProxyEnvironment) env; } return null; }
Example #23
Source File: LoadOnStartupServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); ApiProxy.Environment requestEnv = ApiProxy.getCurrentEnvironment(); for (String key : requestEnv.getAttributes().keySet()) { if (!INIT_ENV.getAttributes().containsKey(key) && !REQUEST_ONLY_ATTRIBUTES.contains(key)) { resp.getWriter().println("Init environment attributes do not contain " + key); } } }
Example #24
Source File: TestSocketServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override public byte[] makeSyncCall( ApiProxy.Environment environment, String packageName, String methodName, byte[] request) { if (!"remote_socket".equals(packageName)) { throw new UnsupportedOperationException(); } return makeResponse(methodName, request); }
Example #25
Source File: TestInetAddressServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override public Future<byte[]> makeAsyncCall( ApiProxy.Environment environment, String packageName, String methodName, byte[] request, ApiProxy.ApiConfig apiConfig) { if ("remote_socket".equals(packageName) && "Resolve".equals(methodName)) { return new Future<byte[]>() { @Override public boolean cancel(boolean mayInterruptIfRunning) { return false; } @Override public boolean isCancelled() { return false; } @Override public boolean isDone() { return true; } @Override public byte[] get() throws InterruptedException, ExecutionException { return RESOLVER_RESPONSE; } @Override public byte[] get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return RESOLVER_RESPONSE; } }; } throw new UnsupportedOperationException(); }
Example #26
Source File: TestInetAddressServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override public byte[] makeSyncCall( ApiProxy.Environment environment, String packageName, String methodName, byte[] request) { if ("remote_socket".equals(packageName) && "Resolve".equals(methodName)) { return RESOLVER_RESPONSE; } throw new UnsupportedOperationException(); }
Example #27
Source File: AppIdentityServiceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testParseFullAppId() { // [(partition)~][(domain):](display-app-id) ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); String hostname = (String) env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"); AppIdentityService.ParsedAppId parsed = appIdentity.parseFullAppId(hostname); String message = createParsed(parsed); Assert.assertEquals(message, property("testParseFullAppId_partition").getPropertyValue(), parsed.getPartition()); Assert.assertEquals(message, getExpectedAppHostname("testParseFullAppId_domain"), parsed.getDomain()); Assert.assertEquals(message, getExpectedAppId("testParseFullAppId_id"), parsed.getId()); }
Example #28
Source File: RequestStatusCheckerImpl.java From nomulus with Apache License 2.0 | 5 votes |
/** * Returns the unique log identifier of the current request. * * <p>May be safely called multiple times, will always return the same result (within the same * request). * * @see <a href="https://cloud.google.com/appengine/docs/standard/java/how-requests-are-handled#request-ids">appengine documentation</a> */ @Override public String getLogId() { String requestLogId = ApiProxy.getCurrentEnvironment().getAttributes().get(REQUEST_LOG_ID_KEY).toString(); logger.atInfo().log("Current requestLogId: %s", requestLogId); // We want to make sure there actually is a log to query for this request, even if the request // dies right after this call. // // flushLogs() is synchronous, so once the function returns, no matter what happens next, the // returned requestLogId will point to existing logs. ApiProxy.flushLogs(); return requestLogId; }
Example #29
Source File: SessionManagerTest.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public byte[] makeSyncCall( ApiProxy.Environment environment, String packageName, String methodName, byte[] request) throws ApiProxyException { if (packageName.equals("datastore_v3") && timeoutCount > 0) { timeoutCount--; throw new DatastoreTimeoutException("Timeout"); } return delegate.makeSyncCall(environment, packageName, methodName, request); }
Example #30
Source File: TestSocketServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override public Future<byte[]> makeAsyncCall( ApiProxy.Environment environment, String packageName, String methodName, byte[] request, ApiProxy.ApiConfig apiConfig) { if ("remote_socket".equals(packageName)) { final byte[] response = makeResponse(methodName, request); return new Future<byte[]>() { @Override public boolean cancel(boolean mayInterruptIfRunning) { return false; } @Override public boolean isCancelled() { return false; } @Override public boolean isDone() { return true; } @Override public byte[] get() throws InterruptedException, ExecutionException { return response; } @Override public byte[] get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return response; } }; } throw new UnsupportedOperationException(); }