Search in sources :

Example 1 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class GenApiConfigAction method genApiConfig.

/**
 * Generates API configuration for an array of service classes.
 * @param classPath Class path to load service classes and their dependencies
 * @param outputDirPath Directory to write API configuration files into
 * @param serviceClassNames Array of service class names of the API
 * @param warPath Directory or file containing a WAR layout
 * @param outputToDisk Iff {@code true}, outputs a *.api file to disk for each API.
 * @return JSON-formatted configurations for each API.
 */
public Iterable<String> genApiConfig(URL[] classPath, String outputDirPath, String warPath, List<String> serviceClassNames, boolean outputToDisk) throws ClassNotFoundException, IOException, ApiConfigException {
    File outputDir = new File(outputDirPath);
    if (!outputDir.isDirectory()) {
        throw new IllegalArgumentException(outputDirPath + " is not a directory");
    }
    ClassLoader classLoader = new URLClassLoader(classPath, getClass().getClassLoader());
    ApiConfig.Factory configFactory = new ApiConfig.Factory();
    TypeLoader typeLoader = new TypeLoader(classLoader);
    SchemaRepository schemaRepository = new SchemaRepository(typeLoader);
    ApiConfigValidator validator = new ApiConfigValidator(typeLoader, schemaRepository);
    JsonConfigWriter jsonConfigWriter = new JsonConfigWriter(typeLoader, validator);
    ApiConfigGenerator generator = new AnnotationApiConfigGenerator(jsonConfigWriter, classLoader, configFactory);
    Map<String, String> apiConfigs = generator.generateConfig(ServiceContext.create(AppEngineUtil.getApplicationId(warPath), ServiceContext.DEFAULT_API_NAME), loadClasses(classLoader, serviceClassNames));
    if (outputToDisk) {
        for (Map.Entry<String, String> entry : apiConfigs.entrySet()) {
            String apiConfigFileName = entry.getKey();
            String apiConfigFileContent = entry.getValue();
            String apiConfigFilePath = outputDir + "/" + apiConfigFileName;
            Files.write(apiConfigFileContent, new File(apiConfigFilePath), UTF_8);
            System.out.println("API configuration written to " + apiConfigFilePath);
        }
    }
    return apiConfigs.values();
}
Also used : TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) JsonConfigWriter(com.google.api.server.spi.config.jsonwriter.JsonConfigWriter) URLClassLoader(java.net.URLClassLoader) ApiConfigValidator(com.google.api.server.spi.config.validation.ApiConfigValidator) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Map(java.util.Map) SchemaRepository(com.google.api.server.spi.config.model.SchemaRepository)

Example 2 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class EndpointsMethodHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    classLoader = EndpointsMethodHandlerTest.class.getClassLoader();
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    context = new EndpointsContext("", "", request, response, true);
    systemService = SystemService.builder().withDefaults(classLoader).addService(TestEndpoint.class, new TestEndpoint()).build();
    typeLoader = new TypeLoader(classLoader);
    apiConfig = new ApiConfig.Factory().create(ServiceContext.create(), typeLoader, TestEndpoint.class);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) EndpointsContext(com.google.api.server.spi.EndpointsContext) TypeLoader(com.google.api.server.spi.TypeLoader) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 3 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class ApiAnnotationIntrospectorTest method testGetSerializerTypeWithIncompatibleSourceType.

@Test
public void testGetSerializerTypeWithIncompatibleSourceType() throws Exception {
    try {
        ApiConfig config = new ApiConfig.Factory().create(ServiceContext.create(), new TypeLoader(), TestEndpoint.class);
        ApiAnnotationIntrospector.getSchemaType(TypeToken.of(BadSerializerBean.class), config);
        fail("no exception thrown when a bad serializer was specified");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) Test(org.junit.Test)

Example 4 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class ApiConfigLoaderTest method setUp.

@Before
public void setUp() throws Exception {
    typeLoader = new TypeLoader();
    endpointClass = TestEndpoint.class;
    Mockito.when(configFactory.create(serviceContext, typeLoader, endpointClass)).thenReturn(config);
    Mockito.when(config.getApiClassConfig()).thenReturn(classConfig);
    Mockito.when(classConfig.getMethods()).thenReturn(methodsMap);
    Mockito.when(configFactory.copy(config)).thenReturn(config2);
    Mockito.when(config2.getApiClassConfig()).thenReturn(classConfig);
    loader = new ApiConfigLoader(configFactory, typeLoader, annotationSource, datastoreSource);
}
Also used : TypeLoader(com.google.api.server.spi.TypeLoader) Before(org.junit.Before)

Example 5 with TypeLoader

use of com.google.api.server.spi.TypeLoader in project endpoints-java by cloudendpoints.

the class ApiMethodAnnotationConfigTest method setUp.

@Before
public void setUp() throws Exception {
    serializationConfig = new ApiSerializationConfig();
    Mockito.when(apiClassConfig.getApiConfig()).thenReturn(apiConfig);
    Mockito.when(apiClassConfig.getAuthLevel()).thenReturn(AuthLevel.NONE);
    Mockito.when(apiClassConfig.getScopeExpression()).thenReturn(defaultScopeExpression);
    Mockito.when(apiClassConfig.getAudiences()).thenReturn(defaultAudiences);
    Mockito.when(apiClassConfig.getClientIds()).thenReturn(defaultClientIds);
    Mockito.<List<Class<? extends Authenticator>>>when(apiClassConfig.getAuthenticators()).thenReturn(defaultAuthenticators);
    Mockito.<List<Class<? extends PeerAuthenticator>>>when(apiClassConfig.getPeerAuthenticators()).thenReturn(defaultPeerAuthenticators);
    Mockito.when(apiClassConfig.getApiClassJavaSimpleName()).thenReturn(TestEndpoint.class.getSimpleName());
    Mockito.when(apiConfig.getSerializationConfig()).thenReturn(serializationConfig);
    EndpointMethod method = EndpointMethod.create(TestEndpoint.class, TestEndpoint.class.getMethod("overrideMethod1"));
    config = new ApiMethodConfig(method, new TypeLoader(), apiClassConfig);
    annotationConfig = new ApiMethodAnnotationConfig(config);
}
Also used : TestEndpoint(com.google.api.server.spi.testing.TestEndpoint) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointsPeerAuthenticator(com.google.api.server.spi.auth.EndpointsPeerAuthenticator) PassPeerAuthenticator(com.google.api.server.spi.testing.PassPeerAuthenticator) PeerAuthenticator(com.google.api.server.spi.config.PeerAuthenticator) EndpointMethod(com.google.api.server.spi.EndpointMethod) TypeLoader(com.google.api.server.spi.TypeLoader) ApiSerializationConfig(com.google.api.server.spi.config.model.ApiSerializationConfig) EndpointsPeerAuthenticator(com.google.api.server.spi.auth.EndpointsPeerAuthenticator) PassAuthenticator(com.google.api.server.spi.testing.PassAuthenticator) PassPeerAuthenticator(com.google.api.server.spi.testing.PassPeerAuthenticator) Authenticator(com.google.api.server.spi.config.Authenticator) EndpointsAuthenticator(com.google.api.server.spi.auth.EndpointsAuthenticator) PeerAuthenticator(com.google.api.server.spi.config.PeerAuthenticator) Before(org.junit.Before)

Aggregations

TypeLoader (com.google.api.server.spi.TypeLoader)18 Before (org.junit.Before)11 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)7 ApiConfigLoader (com.google.api.server.spi.config.ApiConfigLoader)6 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)6 SchemaRepository (com.google.api.server.spi.config.model.SchemaRepository)5 ApiConfigValidator (com.google.api.server.spi.config.validation.ApiConfigValidator)5 ServiceContext (com.google.api.server.spi.ServiceContext)4 TestEndpoint (com.google.api.server.spi.testing.TestEndpoint)4 ApiKey (com.google.api.server.spi.config.model.ApiKey)3 File (java.io.File)3 URLClassLoader (java.net.URLClassLoader)3 Test (org.junit.Test)3 Api (com.google.api.server.spi.config.Api)2 ApiSerializationConfig (com.google.api.server.spi.config.model.ApiSerializationConfig)2 EndpointsPrettyPrinter (com.google.api.server.spi.response.EndpointsPrettyPrinter)2 Map (java.util.Map)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1