use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class ServletRequestParamReader method deserializeParams.
protected Object[] deserializeParams(JsonNode node) throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ServiceException {
EndpointMethod method = getMethod();
Class<?>[] paramClasses = method.getParameterClasses();
TypeToken<?>[] paramTypes = method.getParameterTypes();
Object[] params = new Object[paramClasses.length];
List<String> parameterNames = getParameterNames(method);
for (int i = 0; i < paramClasses.length; i++) {
TypeToken<?> type = paramTypes[i];
Class<?> clazz = paramClasses[i];
if (User.class.isAssignableFrom(clazz)) {
// User type parameter requires no Named annotation (ignored if present)
User user = getUser();
if (user == null && methodConfig != null && methodConfig.getAuthLevel() == AuthLevel.REQUIRED) {
throw new UnauthorizedException("Valid user credentials are required.");
}
if (user == null || clazz.isAssignableFrom(user.getClass())) {
params[i] = user;
logger.atFine().log("deserialize: User injected into param[%d]", i);
} else {
logger.atWarning().log("deserialize: User object of type %s is not assignable to %s. User will be null.", user.getClass().getName(), clazz.getName());
}
} else if (APPENGINE_USER_CLASS_NAME.equals(clazz.getName())) {
// User type parameter requires no Named annotation (ignored if present)
com.google.appengine.api.users.User appEngineUser = getAppEngineUser();
if (appEngineUser == null && methodConfig != null && methodConfig.getAuthLevel() == AuthLevel.REQUIRED) {
throw new UnauthorizedException("Valid user credentials are required.");
}
params[i] = appEngineUser;
logger.atFine().log("deserialize: App Engine User injected into param[%d]", i);
} else if (clazz == HttpServletRequest.class) {
// HttpServletRequest type parameter requires no Named annotation (ignored if present)
params[i] = endpointsContext.getRequest();
logger.atFine().log("deserialize: HttpServletRequest injected into param[%d]", i);
} else if (clazz == ServletContext.class) {
// ServletContext type parameter requires no Named annotation (ignored if present)
params[i] = servletContext;
logger.atFine().log("deserialize: ServletContext %s injected into param[%d]", params[i], i);
} else {
String name = parameterNames.get(i);
if (Strings.isNullOrEmpty(name)) {
params[i] = (node == null) ? null : objectReader.forType(clazz).readValue(node);
logger.atFine().log("deserialize: %s %s injected into unnamed param[%d]", clazz, params[i], i);
} else if (StandardParameters.isStandardParamName(name)) {
params[i] = getStandardParamValue(node, name);
} else {
JsonNode nodeValue = node.get(name);
if (nodeValue == null) {
params[i] = null;
} else {
// Check for collection type
if (Collection.class.isAssignableFrom(clazz) && type.getType() instanceof ParameterizedType) {
params[i] = deserializeCollection(clazz, (ParameterizedType) type.getType(), nodeValue);
} else {
params[i] = objectReader.forType(clazz).readValue(nodeValue);
}
}
if (params[i] == null && isRequiredParameter(method, i)) {
throw new BadRequestException("null value for parameter '" + name + "' not allowed");
}
logger.atFine().log("deserialize: %s %s injected into param[%d] named {%s}", clazz, params[i], i, name);
}
}
}
return params;
}
use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class ServletRequestParamReader method getParameterNames.
protected static List<String> getParameterNames(EndpointMethod endpointMethod) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<String> parameterNames = endpointMethod.getParameterNames();
if (parameterNames == null) {
Method method = endpointMethod.getMethod();
parameterNames = new ArrayList<>();
for (int parameter = 0; parameter < method.getParameterTypes().length; parameter++) {
Annotation annotation = AnnotationUtil.getNamedParameter(method, parameter, Named.class);
if (annotation == null) {
parameterNames.add(null);
} else {
parameterNames.add((String) annotation.getClass().getMethod("value").invoke(annotation));
}
}
endpointMethod.setParameterNames(parameterNames);
}
return parameterNames;
}
use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class SwaggerGenerator method writeApiClass.
private void writeApiClass(ApiConfig apiConfig, Swagger swagger, GenerationContext genCtx) throws ApiConfigException {
Map<EndpointMethod, ApiMethodConfig> methodConfigs = apiConfig.getApiClassConfig().getMethods();
for (Map.Entry<EndpointMethod, ApiMethodConfig> methodConfig : methodConfigs.entrySet()) {
if (!methodConfig.getValue().isIgnored()) {
ApiMethodConfig config = methodConfig.getValue();
writeApiMethod(config, apiConfig, swagger, genCtx);
}
}
}
use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class JsonConfigWriter method convertApiMethods.
private void convertApiMethods(ObjectNode methodsNode, ObjectNode descriptorSchemasNode, ObjectNode descriptorMethodsNode, ApiConfig apiConfig) throws IllegalArgumentException, SecurityException, ApiConfigException {
Map<EndpointMethod, ApiMethodConfig> methodConfigs = apiConfig.getApiClassConfig().getMethods();
for (Map.Entry<EndpointMethod, ApiMethodConfig> methodConfig : methodConfigs.entrySet()) {
if (!methodConfig.getValue().isIgnored()) {
EndpointMethod endpointMethod = methodConfig.getKey();
ApiMethodConfig config = methodConfig.getValue();
convertApiMethod(methodsNode, descriptorSchemasNode, descriptorMethodsNode, endpointMethod, config, apiConfig);
}
}
}
use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class ApiMethodConfig method setDefaults.
/**
* Sets all fields to their default value to be used if not set otherwise. Override to change the
* default configuration.
*/
protected void setDefaults(EndpointMethod endpointMethod, TypeLoader typeLoader, String apiDefaultResource) {
Method method = endpointMethod.getMethod();
RestMethod restMethod = getRestMethod(method);
String resourceTypeName;
if (apiDefaultResource != null) {
resourceTypeName = apiDefaultResource.toLowerCase();
} else {
resourceTypeName = restMethod.guessResourceName(apiClassConfig.getApiConfig(), endpointMethod, typeLoader.getClassTypes());
}
name = null;
httpMethod = Preconditions.checkNotNull(restMethod.getHttpMethod(), "httpMethod");
setPath(Preconditions.checkNotNull(resourceTypeName == null ? method.getName() : resourceTypeName.toLowerCase(), "path"));
authLevel = AuthLevel.UNSPECIFIED;
scopeExpression = null;
audiences = null;
issuerAudiences = ApiIssuerAudienceConfig.UNSPECIFIED;
clientIds = null;
authenticators = null;
peerAuthenticators = null;
ignored = false;
apiKeyRequired = null;
returnType = endpointMethod.getReturnType();
metricCosts = ImmutableList.of();
}
Aggregations