All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.api.server.spi.ServiceContext Maven / Gradle / Ivy

There is a newer version: 2.2.2
Show newest version
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.server.spi;

import com.google.common.base.Strings;

/**
 * Provides context for a service class when its API configuration is being generated.
 */
public class ServiceContext {

  /** API name to use when it is not provided or inferred. */
  public static final String DEFAULT_API_NAME = "myapi";

  /** Application name to use when it is not provided or inferred. */
  public static final String DEFAULT_APP_NAME = "myapp";

  private final String defaultApiName;
  private final String appHostName;

  /**
   * Creates a service context with default application and API name.
   */
  public static ServiceContext create() {
    return create(DEFAULT_APP_NAME, DEFAULT_API_NAME);
  }

  /**
   * Creates a service context with a given default application and API name.
   * @param applicationId Default application id to use.  Format is either "<appName>" or
   *     "google.com:<appName>", where the host name of the former is
   *     "<appName>.appspot.com" and ther latter is "<appName>.googleplex.com".
   * @param apiName Default API name to use.
   */
  public static ServiceContext create(String applicationId, String apiName) {
    return new ServiceContext(applicationId, apiName);
  }

  private ServiceContext(String applicationId, String apiName) {
    if (applicationId == null || applicationId.trim().isEmpty()) {
      applicationId = DEFAULT_APP_NAME;
    }
    // For Endpoints runtime, appHostName is generated from App Engine environment directly. When
    // generating endpoints client library statically from annotated class, derive appHostName from
    // application id set in appengine-web.xml.
    int colon = applicationId.indexOf(":");
    if (colon >= 0) {
      String appName = applicationId.substring(colon + 1);
      this.defaultApiName = apiName == null ? appName : apiName;
      if (applicationId.substring(0, colon).equals("google.com")) {
        this.appHostName =
            Strings.isNullOrEmpty(EnvUtil.getAppHostName()) ? appName + ".googleplex.com"
                : EnvUtil.getAppHostName();
      } else {
        throw new IllegalArgumentException("Invalid application id '" + applicationId + "'");
      }
    } else {
      String appName = applicationId;
      this.defaultApiName = apiName == null ? appName : apiName;
      this.appHostName =
          Strings.isNullOrEmpty(EnvUtil.getAppHostName()) ? applicationId + ".appspot.com"
              : EnvUtil.getAppHostName();
    }
  }

  public String getDefaultApiName() {
    return defaultApiName;
  }

  public String getAppHostName() {
    return appHostName;
  }

  public String getTransferProtocol() {
    // Return https when running on App Engine prod or when not running in App Engine environment
    // (dev server or app engine prod) at all. The latter case will happen when endpoints tool
    // generates client library statically. Otherwise return "http".
    return EnvUtil.isRunningOnAppEngineProd() || !EnvUtil.isRunningOnAppEngine() ? "https"
        : "http";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy