Search in sources :

Example 1 with ApiConfigException

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

the class ApiConfigAnnotationReader method loadEndpointClass.

@Override
public void loadEndpointClass(ServiceContext serviceContext, Class<?> endpointClass, ApiConfig config) throws ApiConfigException {
    try {
        Annotation api = getDeclaredAnnotation(endpointClass, annotationTypes.get("Api"));
        Annotation apiClass = getDeclaredAnnotation(endpointClass, annotationTypes.get("ApiClass"));
        if (!readEndpointClass(config, endpointClass, api, apiClass, endpointClass)) {
            throw new ApiConfigException(endpointClass + " has no @Api annotation.");
        }
    } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
        throw new ApiConfigException(e);
    }
}
Also used : ApiConfigException(com.google.api.server.spi.config.ApiConfigException) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ApiConfigException

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

the class ProxyingDiscoveryProvider method getDirectory.

@Override
public DirectoryList getDirectory(String root) throws InternalServerErrorException {
    try {
        Map<ApiKey, String> configStrings = configWriter.writeConfig(rewriteConfigsWithRoot(getAllApiConfigs(), root));
        ApiConfigs configs = new ApiConfigs();
        configs.setConfigs(Lists.newArrayList(configStrings.values()));
        return discovery.apis().generateDirectory(configs).execute();
    } catch (IOException | ApiConfigException e) {
        logger.atSevere().withCause(e).log("Could not generate or cache directory");
        throw new InternalServerErrorException("Internal Server Error", e);
    }
}
Also used : ApiKey(com.google.api.server.spi.config.model.ApiKey) ApiConfigException(com.google.api.server.spi.config.ApiConfigException) InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) IOException(java.io.IOException) ApiConfigs(com.google.api.services.discovery.model.ApiConfigs)

Example 3 with ApiConfigException

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

the class ApiConfigAnnotationReaderTest method testEndpointNoApiAnnotation.

@Test
public void testEndpointNoApiAnnotation() throws Exception {
    ApiConfig config = createConfig(Object.class);
    try {
        annotationReader.loadEndpointClass(serviceContext, Object.class, config);
        fail("No @Api annotation should've caused reader to fail.");
    } catch (ApiConfigException expected) {
        assertEquals("class java.lang.Object has no @Api annotation.", expected.getMessage());
    }
}
Also used : ApiConfigException(com.google.api.server.spi.config.ApiConfigException) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) Test(org.junit.Test)

Example 4 with ApiConfigException

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

the class SwaggerGenerator method addDefinedMetricCosts.

private void addDefinedMetricCosts(Map<String, ApiLimitMetricConfig> limitMetrics, Operation operation, List<ApiMetricCostConfig> metricCosts) throws ApiConfigException {
    if (!metricCosts.isEmpty()) {
        Map<String, Integer> costs = new HashMap<>();
        for (ApiMetricCostConfig cost : metricCosts) {
            if (!limitMetrics.containsKey(cost.name())) {
                throw new ApiConfigException(String.format("Could not add a metric cost for metric '%s'. The limit metric must be " + "defined at the API level.", cost.name()));
            }
            costs.put(cost.name(), cost.cost());
        }
        operation.setVendorExtension("x-google-quota", ImmutableMap.of("metricCosts", costs));
    }
}
Also used : ApiMetricCostConfig(com.google.api.server.spi.config.model.ApiMetricCostConfig) ApiConfigException(com.google.api.server.spi.config.ApiConfigException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ApiConfigException

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

the class SwaggerGenerator method addNonConflictingSecurityDefinition.

private static String addNonConflictingSecurityDefinition(Swagger swagger, IssuerConfig issuerConfig, ImmutableSet<String> audiences) throws ApiConfigException {
    Map<String, SecuritySchemeDefinition> securityDefinitions = getOrCreateSecurityDefinitionMap(swagger);
    String issuerPlusHash = String.format("%s-%x", issuerConfig.getName(), audiences.hashCode());
    SecuritySchemeDefinition existingDef = securityDefinitions.get(issuerConfig.getName());
    SecuritySchemeDefinition newDef = toScheme(issuerConfig, audiences);
    if (existingDef != null && !existingDef.equals(newDef)) {
        throw new ApiConfigException("Multiple conflicting definitions found for issuer " + issuerConfig.getName());
    }
    swagger.securityDefinition(issuerPlusHash, newDef);
    return issuerPlusHash;
}
Also used : ApiConfigException(com.google.api.server.spi.config.ApiConfigException) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition)

Aggregations

ApiConfigException (com.google.api.server.spi.config.ApiConfigException)5 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)1 ApiKey (com.google.api.server.spi.config.model.ApiKey)1 ApiMetricCostConfig (com.google.api.server.spi.config.model.ApiMetricCostConfig)1 InternalServerErrorException (com.google.api.server.spi.response.InternalServerErrorException)1 ApiConfigs (com.google.api.services.discovery.model.ApiConfigs)1 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1