Java Code Examples for com.google.apphosting.api.ApiProxy#getCurrentEnvironment()

The following examples show how to use com.google.apphosting.api.ApiProxy#getCurrentEnvironment() . 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: AppIdentityServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: LocalRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
/**
 * 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 3
Source File: IdentityServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  resp.setContentType("text/plain");
  ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
  resp.getWriter().print("default_version_hostname: ");
  resp.getWriter()
      .println(env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"));
}
 
Example 4
Source File: AppIdentityBasicTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: AppIdentityBasicTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testInstanceId() {
    ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
    String instanceId = (String) env.getAttributes().get("com.google.appengine.instance.id");
    String errMsg = "The instance id should not be null";
    Assert.assertNotNull(errMsg, instanceId);
}
 
Example 6
Source File: AppIdentityBasicTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetVersionedHostname() {
    String expectedHostname = getExpectedAppHostname("testGetVersionedHostname");
    ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
    String hostname = (String) env.getAttributes().get("com.google.appengine.runtime.default_version_hostname");
    String errMsg = "The versioned hostname should end with " + expectedHostname + ", but was " + hostname;
    Assert.assertTrue(errMsg, hostname.endsWith(expectedHostname));
}
 
Example 7
Source File: AppIdentityServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: AppengineWebAppContext.java    From yawp with MIT License 5 votes vote down vote up
@Override
protected void doStart() throws Exception {
    this.helper = createHelper();
    this.environment = ApiProxy.getCurrentEnvironment();
    getServletContext().setAttribute(API_PROXY_LOCAL, ApiProxy.getDelegate());
    getServletContext().setAttribute(APPENGINE_WEB_XML, readAppengineWebXml());
    getServletContext().setAttribute(WEB_XML, readWebXml());
    SystemProperty.environment.set(SystemProperty.Environment.Value.Development);
    configureUserRealmAppengineHelper();
    super.doStart();
}
 
Example 9
Source File: SessionManager.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
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 10
Source File: AppEngineAuthentication.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * 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 11
Source File: VmRuntimeLogHandler.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the thread local environment if it is a VmApiProxyEnvironment.
 *
 * @return The ThreadLocal environment or null if no VmApiProxyEnvironment is set on this thread.
 */
private VmApiProxyEnvironment getThreadLocalEnvironment() {
  Environment env = ApiProxy.getCurrentEnvironment();
  if (env instanceof VmApiProxyEnvironment) {
    return (VmApiProxyEnvironment) env;
  }
  return null;
}
 
Example 12
Source File: VmHealthServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
private String fullVersionId() {
  ApiProxy.Environment environment = ApiProxy.getCurrentEnvironment();
  String actualVersionId = environment.getVersionId();
  if (!(environment.getModuleId() == null
        || environment.getModuleId().isEmpty()
        || environment.getModuleId().equals("default"))) {
    actualVersionId = environment.getModuleId() + ":" + actualVersionId;
  }
  return actualVersionId;
}
 
Example 13
Source File: LoadOnStartupServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
@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 14
Source File: GetEnvironmentAttributesServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  resp.setContentType("text/plain");
  Environment env = ApiProxy.getCurrentEnvironment();

  for (Entry entry : env.getAttributes().entrySet()) {
    resp.getWriter().println(entry.getKey() + " => " + entry.getValue());
  }
}
 
Example 15
Source File: Config.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the GAE's internal request ID of a request. This is not related to HttpServletRequest.
 *
 * @see <a href="https://cloud.google.com/appengine/docs/standard/java/how-requests-are-handled">https://cloud.google.com/appengine/docs/standard/java/how-requests-are-handled</a>
 */
public static String getRequestId() {
    ApiProxy.Environment serverEnvironment = ApiProxy.getCurrentEnvironment();
    if (serverEnvironment == null) {
        // This will be the case in dev server
        return "dummyrequestid";
    }
    return String.valueOf(ApiProxy.getCurrentEnvironment().getAttributes()
            .get("com.google.appengine.runtime.request_log_id"));
}
 
Example 16
Source File: Config.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
private static String readAppUrl() {
    ApiProxy.Environment serverEnvironment = ApiProxy.getCurrentEnvironment();
    if (serverEnvironment == null) {
        return null;
    }
    String hostname = (String) serverEnvironment.getAttributes()
            .get("com.google.appengine.runtime.default_version_hostname");
    if (hostname == null) {
        return null;
    }
    return (isDevServer() ? "http://" : "https://") + hostname;
}
 
Example 17
Source File: HelloInfo.java    From appengine-java-vm-hello with Apache License 2.0 5 votes vote down vote up
public static String getInfo() {
  String version = SystemProperty.applicationVersion.get();
  String majorVersion = version.substring(0, version.indexOf('.'));
  Environment env = ApiProxy.getCurrentEnvironment();
  String hostname =
      "" + env.getAttributes().get("com.google.appengine.runtime.default_version_hostname");
  String infostring = "version: " + majorVersion + " and hostname: " + hostname;
  return infostring;
}
 
Example 18
Source File: PipelineTest.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  traceBuffer = new StringBuffer();
  helper.setUp();
  apiProxyEnvironment = ApiProxy.getCurrentEnvironment();
  System.setProperty(USE_SIMPLE_GUIDS_FOR_DEBUGGING, "true");
  taskQueue = LocalTaskQueueTestConfig.getLocalTaskQueue();
}
 
Example 19
Source File: AppEngineEnvironment.java    From nomulus with Apache License 2.0 5 votes vote down vote up
public AppEngineEnvironment() {
  isPlaceHolderNeeded = ApiProxy.getCurrentEnvironment() == null;
  // isPlaceHolderNeeded may be true when we are invoked in a test with AppEngineRule.
  if (isPlaceHolderNeeded) {
    ApiProxy.setEnvironmentForCurrentThread(PLACEHOLDER_ENV);
  }
}
 
Example 20
Source File: URLFetchTestBase.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
protected static URL getUrl(String path) throws MalformedURLException {
    ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
    Object localhost = env.getAttributes().get("com.google.appengine.runtime.default_version_hostname");
    return new URL("http://" + localhost + "/" + path);
}