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();
}
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);
}
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
}
}
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);
}
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);
}
Aggregations