You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2018/01/06 03:39:38 UTC
[3/3] juneau git commit: RestContext refactoring.
RestContext refactoring.
Project: http://git-wip-us.apache.org/repos/asf/juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/juneau/commit/96fae4f9
Tree: http://git-wip-us.apache.org/repos/asf/juneau/tree/96fae4f9
Diff: http://git-wip-us.apache.org/repos/asf/juneau/diff/96fae4f9
Branch: refs/heads/master
Commit: 96fae4f9896b70ca2cea9bd4591f39736b96ecb5
Parents: 76b634a
Author: JamesBognar <ja...@apache.org>
Authored: Fri Jan 5 22:39:31 2018 -0500
Committer: JamesBognar <ja...@apache.org>
Committed: Fri Jan 5 22:39:31 2018 -0500
----------------------------------------------------------------------
.../java/org/apache/juneau/rest/CallMethod.java | 921 -------------------
.../java/org/apache/juneau/rest/CallRouter.java | 100 --
.../org/apache/juneau/rest/RestCallHandler.java | 12 +-
.../org/apache/juneau/rest/RestCallRouter.java | 100 ++
.../org/apache/juneau/rest/RestContext.java | 24 +-
.../apache/juneau/rest/RestInfoProvider.java | 6 +-
.../org/apache/juneau/rest/RestJavaMethod.java | 921 +++++++++++++++++++
7 files changed, 1042 insertions(+), 1042 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/juneau/blob/96fae4f9/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/CallMethod.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/CallMethod.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/CallMethod.java
deleted file mode 100644
index d33b60b..0000000
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/CallMethod.java
+++ /dev/null
@@ -1,921 +0,0 @@
-// ***************************************************************************************************************************
-// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
-// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
-// * to you 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 org.apache.juneau.rest;
-
-import static javax.servlet.http.HttpServletResponse.*;
-import static org.apache.juneau.dto.swagger.SwaggerBuilder.*;
-import static org.apache.juneau.internal.ClassUtils.*;
-import static org.apache.juneau.internal.StringUtils.*;
-import static org.apache.juneau.internal.Utils.*;
-import static org.apache.juneau.BeanContext.*;
-import static org.apache.juneau.rest.RestContext.*;
-
-import java.lang.annotation.*;
-import java.lang.reflect.*;
-import java.util.*;
-
-import javax.servlet.http.*;
-
-import org.apache.juneau.*;
-import org.apache.juneau.dto.swagger.*;
-import org.apache.juneau.encoders.*;
-import org.apache.juneau.http.*;
-import org.apache.juneau.httppart.*;
-import org.apache.juneau.httppart.HttpPartParser;
-import org.apache.juneau.internal.*;
-import org.apache.juneau.json.*;
-import org.apache.juneau.parser.*;
-import org.apache.juneau.rest.annotation.*;
-import org.apache.juneau.rest.widget.*;
-import org.apache.juneau.serializer.*;
-import org.apache.juneau.svl.*;
-import org.apache.juneau.utils.*;
-
-/**
- * Represents a single Java servlet/resource method annotated with {@link RestMethod @RestMethod}.
- */
-class CallMethod implements Comparable<CallMethod> {
- private final java.lang.reflect.Method method;
- private final String httpMethod;
- private final UrlPathPattern pathPattern;
- private final RestParam[] params;
- private final RestGuard[] guards;
- private final RestMatcher[] optionalMatchers;
- private final RestMatcher[] requiredMatchers;
- private final RestConverter[] converters;
- private final SerializerGroup serializers;
- private final ParserGroup parsers;
- private final EncoderGroup encoders;
- private final HttpPartParser partParser;
- private final HttpPartSerializer partSerializer;
- private final ObjectMap properties;
- private final Map<String,Object> defaultRequestHeaders, defaultQuery, defaultFormData;
- private final String defaultCharset;
- private final long maxInput;
- private final boolean deprecated;
- private final String description, tags, summary, externalDocs;
- private final Integer priority;
- private final org.apache.juneau.rest.annotation.Parameter[] parameters;
- private final Response[] responses;
- private final RestContext context;
- private final BeanContext beanContext;
- private final Map<String,Widget> widgets;
- private final List<MediaType> supportedAcceptTypes, supportedContentTypes;
-
- CallMethod(Object servlet, java.lang.reflect.Method method, RestContext context) throws RestServletException {
- Builder b = new Builder(servlet, method, context);
- this.context = context;
- this.method = method;
- this.httpMethod = b.httpMethod;
- this.pathPattern = b.pathPattern;
- this.params = b.params;
- this.guards = b.guards;
- this.optionalMatchers = b.optionalMatchers;
- this.requiredMatchers = b.requiredMatchers;
- this.converters = b.converters;
- this.serializers = b.serializers;
- this.parsers = b.parsers;
- this.encoders = b.encoders;
- this.partParser = b.partParser;
- this.partSerializer = b.partSerializer;
- this.beanContext = b.beanContext;
- this.properties = b.properties;
- this.defaultRequestHeaders = b.defaultRequestHeaders;
- this.defaultQuery = b.defaultQuery;
- this.defaultFormData = b.defaultFormData;
- this.defaultCharset = b.defaultCharset;
- this.maxInput = b.maxInput;
- this.deprecated = b.deprecated;
- this.description = b.description;
- this.tags = b.tags;
- this.summary = b.summary;
- this.externalDocs = b.externalDocs;
- this.priority = b.priority;
- this.parameters = b.parameters;
- this.responses = b.responses;
- this.supportedAcceptTypes = b.supportedAcceptTypes;
- this.supportedContentTypes = b.supportedContentTypes;
- this.widgets = Collections.unmodifiableMap(b.widgets);
- }
-
- private static final class Builder {
- String httpMethod, defaultCharset, description, tags, summary, externalDocs;
- UrlPathPattern pathPattern;
- RestParam[] params;
- RestGuard[] guards;
- RestMatcher[] optionalMatchers, requiredMatchers;
- RestConverter[] converters;
- SerializerGroup serializers;
- ParserGroup parsers;
- EncoderGroup encoders;
- HttpPartParser partParser;
- HttpPartSerializer partSerializer;
- BeanContext beanContext;
- ObjectMap properties;
- Map<String,Object> defaultRequestHeaders, defaultQuery, defaultFormData;
- boolean deprecated;
- long maxInput;
- Integer priority;
- org.apache.juneau.rest.annotation.Parameter[] parameters;
- Response[] responses;
- Map<String,Widget> widgets;
- List<MediaType> supportedAcceptTypes, supportedContentTypes;
-
- Builder(Object servlet, java.lang.reflect.Method method, RestContext context) throws RestServletException {
- String sig = method.getDeclaringClass().getName() + '.' + method.getName();
-
- try {
-
- RestMethod m = method.getAnnotation(RestMethod.class);
- if (m == null)
- throw new RestServletException("@RestMethod annotation not found on method ''{0}''", sig);
-
- if (! m.description().isEmpty())
- description = m.description();
- MethodSwagger sm = m.swagger();
- if (! sm.tags().isEmpty())
- tags = sm.tags();
- if (! m.summary().isEmpty())
- summary = m.summary();
- if (! sm.externalDocs().isEmpty())
- externalDocs = sm.externalDocs();
- deprecated = sm.deprecated();
- parameters = sm.parameters();
- responses = sm.responses();
- serializers = context.getSerializers();
- parsers = context.getParsers();
- partSerializer = context.getPartSerializer();
- partParser = context.getPartParser();
- beanContext = context.getBeanContext();
- encoders = context.getEncoders();
- properties = new ObjectMap().setInner(context.getProperties());
- defaultCharset = context.getDefaultCharset();
- maxInput = context.getMaxInput();
-
- if (! m.defaultCharset().isEmpty())
- defaultCharset = context.getVarResolver().resolve(m.defaultCharset());
- if (! m.maxInput().isEmpty())
- maxInput = StringUtils.parseLongWithSuffix(context.getVarResolver().resolve(m.maxInput()));
-
- HtmlDocBuilder hdb = new HtmlDocBuilder(properties);
-
- HtmlDoc hd = m.htmldoc();
- hdb.process(hd);
-
- widgets = new HashMap<>(context.getWidgets());
- for (Class<? extends Widget> wc : hd.widgets()) {
- Widget w = beanContext.newInstance(Widget.class, wc);
- widgets.put(w.getName(), w);
- hdb.script("INHERIT", "$W{"+w.getName()+".script}");
- hdb.style("INHERIT", "$W{"+w.getName()+".style}");
- }
-
- ASet<String> inherit = new ASet<String>().appendAll(StringUtils.split(m.inherit()));
- if (inherit.contains("*"))
- inherit.appendAll("SERIALIZERS","PARSERS","TRANSFORMS","PROPERTIES","ENCODERS");
-
- SerializerGroupBuilder sgb = null;
- ParserGroupBuilder pgb = null;
- ParserBuilder uepb = null;
- BeanContextBuilder bcb = null;
- PropertyStore cps = context.getPropertyStore();
-
- if (m.serializers().length > 0 || m.parsers().length > 0 || m.properties().length > 0 || m.flags().length > 0
- || m.beanFilters().length > 0 || m.pojoSwaps().length > 0 || m.bpi().length > 0
- || m.bpx().length > 0) {
- sgb = SerializerGroup.create();
- pgb = ParserGroup.create();
- uepb = Parser.create();
- bcb = beanContext.builder();
-
- if (inherit.contains("SERIALIZERS") || m.serializers().length == 0)
- sgb.append(cps.getArrayProperty(REST_serializers, Object.class));
-
- if (inherit.contains("PARSERS") || m.parsers().length == 0)
- pgb.append(cps.getArrayProperty(REST_parsers, Object.class));
- }
-
- httpMethod = m.name().toUpperCase(Locale.ENGLISH);
- if (httpMethod.equals("") && method.getName().startsWith("do"))
- httpMethod = method.getName().substring(2).toUpperCase(Locale.ENGLISH);
- if (httpMethod.equals(""))
- httpMethod = "GET";
- if (httpMethod.equals("METHOD"))
- httpMethod = "*";
-
- priority = m.priority();
-
- String p = m.path();
- converters = new RestConverter[m.converters().length];
- for (int i = 0; i < converters.length; i++)
- converters[i] = beanContext.newInstance(RestConverter.class, m.converters()[i]);
-
- guards = new RestGuard[m.guards().length];
- for (int i = 0; i < guards.length; i++)
- guards[i] = beanContext.newInstance(RestGuard.class, m.guards()[i]);
-
- List<RestMatcher> optionalMatchers = new LinkedList<>(), requiredMatchers = new LinkedList<>();
- for (int i = 0; i < m.matchers().length; i++) {
- Class<? extends RestMatcher> c = m.matchers()[i];
- RestMatcher matcher = beanContext.newInstance(RestMatcher.class, c, true, servlet, method);
- if (matcher.mustMatch())
- requiredMatchers.add(matcher);
- else
- optionalMatchers.add(matcher);
- }
- if (! m.clientVersion().isEmpty())
- requiredMatchers.add(new ClientVersionMatcher(context.getClientVersionHeader(), method));
-
- this.requiredMatchers = requiredMatchers.toArray(new RestMatcher[requiredMatchers.size()]);
- this.optionalMatchers = optionalMatchers.toArray(new RestMatcher[optionalMatchers.size()]);
-
- PropertyStore ps = context.getPropertyStore();
- if (! inherit.contains("TRANSFORMS"))
- ps = ps.builder().set(BEAN_beanFilters, null).set(BEAN_pojoSwaps, null).build();
-
- if (sgb != null) {
- sgb.append(m.serializers());
-
- if (! inherit.contains("PROPERTIES"))
- sgb.beanFilters(ps.getClassArrayProperty(BEAN_beanFilters)).pojoSwaps(ps.getClassArrayProperty(BEAN_pojoSwaps));
- else
- sgb.apply(ps);
- for (Property p1 : m.properties())
- sgb.set(p1.name(), p1.value());
- for (String p1 : m.flags())
- sgb.set(p1, true);
- if (m.bpi().length > 0) {
- Map<String,String> bpiMap = new LinkedHashMap<>();
- for (String s : m.bpi()) {
- for (String s2 : split(s, ';')) {
- int i = s2.indexOf(':');
- if (i == -1)
- throw new RestServletException(
- "Invalid format for @RestMethod.bpi() on method ''{0}''. Must be in the format \"ClassName: comma-delimited-tokens\". \nValue: {1}", sig, s);
- bpiMap.put(s2.substring(0, i).trim(), s2.substring(i+1).trim());
- }
- }
- sgb.includeProperties(bpiMap);
- }
- if (m.bpx().length > 0) {
- Map<String,String> bpxMap = new LinkedHashMap<>();
- for (String s : m.bpx()) {
- for (String s2 : split(s, ';')) {
- int i = s2.indexOf(':');
- if (i == -1)
- throw new RestServletException(
- "Invalid format for @RestMethod.bpx() on method ''{0}''. Must be in the format \"ClassName: comma-delimited-tokens\". \nValue: {1}", sig, s);
- bpxMap.put(s2.substring(0, i).trim(), s2.substring(i+1).trim());
- }
- }
- sgb.excludeProperties(bpxMap);
- }
- sgb.beanFilters(m.beanFilters());
- sgb.pojoSwaps(m.pojoSwaps());
- }
-
- if (pgb != null) {
- pgb.append(m.parsers());
- if (! inherit.contains("PROPERTIES"))
- pgb.beanFilters(ps.getClassArrayProperty(BEAN_beanFilters)).pojoSwaps(ps.getClassArrayProperty(BEAN_pojoSwaps));
- else
- pgb.apply(ps);
- for (Property p1 : m.properties())
- pgb.set(p1.name(), p1.value());
- for (String p1 : m.flags())
- pgb.set(p1, true);
- pgb.beanFilters(m.beanFilters());
- pgb.pojoSwaps(m.pojoSwaps());
- }
-
- if (uepb != null) {
- uepb.apply(ps);
- for (Property p1 : m.properties())
- uepb.set(p1.name(), p1.value());
- for (String p1 : m.flags())
- uepb.set(p1, true);
- uepb.beanFilters(m.beanFilters());
- uepb.pojoSwaps(m.pojoSwaps());
- }
-
- if (bcb != null) {
- bcb.apply(ps);
- for (Property p1 : m.properties())
- bcb.set(p1.name(), p1.value());
- for (String p1 : m.flags())
- bcb.set(p1, true);
- bcb.beanFilters(m.beanFilters());
- bcb.pojoSwaps(m.pojoSwaps());
- }
-
- if (m.properties().length > 0 || m.flags().length > 0) {
- properties = new ObjectMap().setInner(properties);
- for (Property p1 : m.properties())
- properties.put(p1.name(), p1.value());
- for (String p1 : m.flags())
- properties.put(p1, true);
- }
-
- if (m.encoders().length > 0) {
- EncoderGroupBuilder g = EncoderGroup.create().append(IdentityEncoder.INSTANCE);
- if (inherit.contains("ENCODERS"))
- g.append(encoders);
-
- for (Class<? extends Encoder> c : m.encoders()) {
- try {
- g.append(c);
- } catch (Exception e) {
- throw new RestServletException(
- "Exception occurred while trying to instantiate Encoder on method ''{0}'': ''{1}''", sig, c.getSimpleName()).initCause(e);
- }
- }
- encoders = g.build();
- }
-
- defaultRequestHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
- for (String s : m.defaultRequestHeaders()) {
- String[] h = RestUtils.parseKeyValuePair(s);
- if (h == null)
- throw new RestServletException(
- "Invalid default request header specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s);
- defaultRequestHeaders.put(h[0], h[1]);
- }
-
- defaultQuery = new LinkedHashMap<>();
- for (String s : m.defaultQuery()) {
- String[] h = RestUtils.parseKeyValuePair(s);
- if (h == null)
- throw new RestServletException(
- "Invalid default query parameter specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s);
- defaultQuery.put(h[0], h[1]);
- }
-
- defaultFormData = new LinkedHashMap<>();
- for (String s : m.defaultFormData()) {
- String[] h = RestUtils.parseKeyValuePair(s);
- if (h == null)
- throw new RestServletException(
- "Invalid default form data parameter specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s);
- defaultFormData.put(h[0], h[1]);
- }
-
- Type[] pt = method.getGenericParameterTypes();
- Annotation[][] pa = method.getParameterAnnotations();
- for (int i = 0; i < pt.length; i++) {
- for (Annotation a : pa[i]) {
- if (a instanceof Header) {
- Header h = (Header)a;
- if (! h.def().isEmpty())
- defaultRequestHeaders.put(firstNonEmpty(h.name(), h.value()), h.def());
- } else if (a instanceof Query) {
- Query q = (Query)a;
- if (! q.def().isEmpty())
- defaultQuery.put(firstNonEmpty(q.name(), q.value()), q.def());
- } else if (a instanceof FormData) {
- FormData f = (FormData)a;
- if (! f.def().isEmpty())
- defaultFormData.put(firstNonEmpty(f.name(), f.value()), f.def());
- }
- }
- }
-
- pathPattern = new UrlPathPattern(p);
-
- if (sgb != null)
- serializers = sgb.build();
- if (pgb != null)
- parsers = pgb.build();
- if (uepb != null && partParser instanceof Parser) {
- Parser pp = (Parser)partParser;
- partParser = (HttpPartParser)pp.builder().apply(uepb.getPropertyStore()).build();
- }
- if (bcb != null)
- beanContext = bcb.build();
-
- supportedAcceptTypes =
- m.supportedAcceptTypes().length > 0
- ? Collections.unmodifiableList(new ArrayList<>(Arrays.asList(MediaType.forStrings(m.supportedAcceptTypes()))))
- : serializers.getSupportedMediaTypes();
- supportedContentTypes =
- m.supportedContentTypes().length > 0
- ? Collections.unmodifiableList(new ArrayList<>(Arrays.asList(MediaType.forStrings(m.supportedContentTypes()))))
- : parsers.getSupportedMediaTypes();
-
- params = context.findParams(method, pathPattern, false);
-
- // Need this to access methods in anonymous inner classes.
- method.setAccessible(true);
- } catch (RestServletException e) {
- throw e;
- } catch (Exception e) {
- throw new RestServletException("Exception occurred while initializing method ''{0}''", sig).initCause(e);
- }
- }
- }
-
- /**
- * Returns <jk>true</jk> if this Java method has any guards or matchers.
- */
- boolean hasGuardsOrMatchers() {
- return (guards.length != 0 || requiredMatchers.length != 0 || optionalMatchers.length != 0);
- }
-
- /**
- * Returns the HTTP method name (e.g. <js>"GET"</js>).
- */
- String getHttpMethod() {
- return httpMethod;
- }
-
- /**
- * Returns the path pattern for this method.
- */
- String getPathPattern() {
- return pathPattern.toString();
- }
-
- /**
- * Returns the localized Swagger for this Java method.
- */
- Operation getSwaggerOperation(RestRequest req) throws ParseException {
- Operation o = operation()
- .operationId(method.getName())
- .description(getDescription(req))
- .tags(getTags(req))
- .summary(getSummary(req))
- .externalDocs(getExternalDocs(req))
- .parameters(getParameters(req))
- .responses(getResponses(req));
-
- if (isDeprecated())
- o.deprecated(true);
-
- if (! parsers.getSupportedMediaTypes().equals(context.getParsers().getSupportedMediaTypes()))
- o.consumes(parsers.getSupportedMediaTypes());
-
- if (! serializers.getSupportedMediaTypes().equals(context.getSerializers().getSupportedMediaTypes()))
- o.produces(serializers.getSupportedMediaTypes());
-
- return o;
- }
-
- private Operation getSwaggerOperationFromFile(RestRequest req) {
- Swagger s = req.getSwaggerFromFile();
- if (s != null && s.getPaths() != null && s.getPaths().get(pathPattern.getPatternString()) != null)
- return s.getPaths().get(pathPattern.getPatternString()).get(httpMethod);
- return null;
- }
-
- /**
- * Returns the localized summary for this Java method.
- */
- String getSummary(RestRequest req) {
- VarResolverSession vr = req.getVarResolverSession();
- if (summary != null)
- return vr.resolve(summary);
- String summary = context.getMessages().findFirstString(req.getLocale(), method.getName() + ".summary");
- if (summary != null)
- return vr.resolve(summary);
- Operation o = getSwaggerOperationFromFile(req);
- if (o != null)
- return o.getSummary();
- return null;
- }
-
- /**
- * Returns the localized description for this Java method.
- */
- String getDescription(RestRequest req) {
- VarResolverSession vr = req.getVarResolverSession();
- if (description != null)
- return vr.resolve(description);
- String description = context.getMessages().findFirstString(req.getLocale(), method.getName() + ".description");
- if (description != null)
- return vr.resolve(description);
- Operation o = getSwaggerOperationFromFile(req);
- if (o != null)
- return o.getDescription();
- return null;
- }
-
- /**
- * Returns the localized Swagger tags for this Java method.
- */
- private List<String> getTags(RestRequest req) {
- VarResolverSession vr = req.getVarResolverSession();
- JsonParser jp = JsonParser.DEFAULT;
- try {
- if (tags != null)
- return jp.parse(vr.resolve(tags), ArrayList.class, String.class);
- String tags = context.getMessages().findFirstString(req.getLocale(), method.getName() + ".tags");
- if (tags != null)
- return jp.parse(vr.resolve(tags), ArrayList.class, String.class);
- Operation o = getSwaggerOperationFromFile(req);
- if (o != null)
- return o.getTags();
- return null;
- } catch (Exception e) {
- throw new RestException(SC_INTERNAL_SERVER_ERROR, e);
- }
- }
-
- /**
- * Returns the localized Swagger external docs for this Java method.
- */
- private ExternalDocumentation getExternalDocs(RestRequest req) {
- VarResolverSession vr = req.getVarResolverSession();
- JsonParser jp = JsonParser.DEFAULT;
- try {
- if (externalDocs != null)
- return jp.parse(vr.resolve(externalDocs), ExternalDocumentation.class);
- String externalDocs = context.getMessages().findFirstString(req.getLocale(), method.getName() + ".externalDocs");
- if (externalDocs != null)
- return jp.parse(vr.resolve(externalDocs), ExternalDocumentation.class);
- Operation o = getSwaggerOperationFromFile(req);
- if (o != null)
- return o.getExternalDocs();
- return null;
- } catch (Exception e) {
- throw new RestException(SC_INTERNAL_SERVER_ERROR, e);
- }
- }
-
- /**
- * Returns the Swagger deprecated flag for this Java method.
- */
- private boolean isDeprecated() {
- return deprecated;
- }
-
- /**
- * Returns the localized Swagger parameter information for this Java method.
- */
- private List<ParameterInfo> getParameters(RestRequest req) throws ParseException {
- Operation o = getSwaggerOperationFromFile(req);
- if (o != null && o.getParameters() != null)
- return o.getParameters();
-
- VarResolverSession vr = req.getVarResolverSession();
- JsonParser jp = JsonParser.DEFAULT;
- Map<String,ParameterInfo> m = new TreeMap<>();
-
- // First parse @RestMethod.parameters() annotation.
- for (org.apache.juneau.rest.annotation.Parameter v : parameters) {
- String in = vr.resolve(v.in());
- ParameterInfo p = parameterInfo(in, vr.resolve(v.name()));
-
- if (! v.description().isEmpty())
- p.description(vr.resolve(v.description()));
- if (v.required())
- p.required(v.required());
-
- if ("body".equals(in)) {
- if (! v.schema().isEmpty())
- p.schema(jp.parse(vr.resolve(v.schema()), SchemaInfo.class));
- } else {
- if (v.allowEmptyValue())
- p.allowEmptyValue(v.allowEmptyValue());
- if (! v.collectionFormat().isEmpty())
- p.collectionFormat(vr.resolve(v.collectionFormat()));
- if (! v._default().isEmpty())
- p._default(vr.resolve(v._default()));
- if (! v.format().isEmpty())
- p.format(vr.resolve(v.format()));
- if (! v.items().isEmpty())
- p.items(jp.parse(vr.resolve(v.items()), Items.class));
- p.type(vr.resolve(v.type()));
- }
- m.put(p.getIn() + '.' + p.getName(), p);
- }
-
- // Next, look in resource bundle.
- String prefix = method.getName() + ".req";
- for (String key : context.getMessages().keySet(prefix)) {
- if (key.length() > prefix.length()) {
- String value = vr.resolve(context.getMessages().getString(key));
- String[] parts = key.substring(prefix.length() + 1).split("\\.");
- String in = parts[0], name, field;
- boolean isBody = "body".equals(in);
- if (parts.length == (isBody ? 2 : 3)) {
- if ("body".equals(in)) {
- name = null;
- field = parts[1];
- } else {
- name = parts[1];
- field = parts[2];
- }
- String k2 = in + '.' + name;
- ParameterInfo p = m.get(k2);
- if (p == null) {
- p = parameterInfoStrict(in, name);
- m.put(k2, p);
- }
-
- if (field.equals("description"))
- p.description(value);
- else if (field.equals("required"))
- p.required(Boolean.valueOf(value));
-
- if ("body".equals(in)) {
- if (field.equals("schema"))
- p.schema(jp.parse(value, SchemaInfo.class));
- } else {
- if (field.equals("allowEmptyValue"))
- p.allowEmptyValue(Boolean.valueOf(value));
- else if (field.equals("collectionFormat"))
- p.collectionFormat(value);
- else if (field.equals("default"))
- p._default(value);
- else if (field.equals("format"))
- p.format(value);
- else if (field.equals("items"))
- p.items(jp.parse(value, Items.class));
- else if (field.equals("type"))
- p.type(value);
- }
- } else {
- System.err.println("Unknown bundle key '"+key+"'");
- }
- }
- }
-
- // Finally, look for parameters defined on method.
- for (RestParam mp : this.params) {
- RestParamType in = mp.getParamType();
- if (in != RestParamType.OTHER) {
- String k2 = in.toString() + '.' + (in == RestParamType.BODY ? null : mp.getName());
- ParameterInfo p = m.get(k2);
- if (p == null) {
- p = parameterInfoStrict(in.toString(), mp.getName());
- m.put(k2, p);
- }
- }
- }
-
- if (m.isEmpty())
- return null;
- return new ArrayList<>(m.values());
- }
-
- /**
- * Returns the localized Swagger response information about this Java method.
- */
- private Map<Integer,ResponseInfo> getResponses(RestRequest req) throws ParseException {
- Operation o = getSwaggerOperationFromFile(req);
- if (o != null && o.getResponses() != null)
- return o.getResponses();
-
- VarResolverSession vr = req.getVarResolverSession();
- JsonParser jp = JsonParser.DEFAULT;
- Map<Integer,ResponseInfo> m = new TreeMap<>();
- Map<String,HeaderInfo> m2 = new TreeMap<>();
-
- // First parse @RestMethod.parameters() annotation.
- for (Response r : responses) {
- int httpCode = r.value();
- String description = r.description().isEmpty() ? RestUtils.getHttpResponseText(r.value()) : vr.resolve(r.description());
- ResponseInfo r2 = responseInfo(description);
-
- if (r.headers().length > 0) {
- for (org.apache.juneau.rest.annotation.Parameter v : r.headers()) {
- HeaderInfo h = headerInfoStrict(vr.resolve(v.type()));
- if (! v.collectionFormat().isEmpty())
- h.collectionFormat(vr.resolve(v.collectionFormat()));
- if (! v._default().isEmpty())
- h._default(vr.resolve(v._default()));
- if (! v.description().isEmpty())
- h.description(vr.resolve(v.description()));
- if (! v.format().isEmpty())
- h.format(vr.resolve(v.format()));
- if (! v.items().isEmpty())
- h.items(jp.parse(vr.resolve(v.items()), Items.class));
- r2.header(v.name(), h);
- m2.put(httpCode + '.' + v.name(), h);
- }
- }
- m.put(httpCode, r2);
- }
-
- // Next, look in resource bundle.
- String prefix = method.getName() + ".res";
- for (String key : context.getMessages().keySet(prefix)) {
- if (key.length() > prefix.length()) {
- String value = vr.resolve(context.getMessages().getString(key));
- String[] parts = key.substring(prefix.length() + 1).split("\\.");
- int httpCode = Integer.parseInt(parts[0]);
- ResponseInfo r2 = m.get(httpCode);
- if (r2 == null) {
- r2 = responseInfo(null);
- m.put(httpCode, r2);
- }
-
- String name = parts.length > 1 ? parts[1] : "";
-
- if ("header".equals(name) && parts.length > 3) {
- String headerName = parts[2];
- String field = parts[3];
-
- String k2 = httpCode + '.' + headerName;
- HeaderInfo h = m2.get(k2);
- if (h == null) {
- h = headerInfoStrict("string");
- m2.put(k2, h);
- r2.header(name, h);
- }
- if (field.equals("collectionFormat"))
- h.collectionFormat(value);
- else if (field.equals("default"))
- h._default(value);
- else if (field.equals("description"))
- h.description(value);
- else if (field.equals("format"))
- h.format(value);
- else if (field.equals("items"))
- h.items(jp.parse(value, Items.class));
- else if (field.equals("type"))
- h.type(value);
-
- } else if ("description".equals(name)) {
- r2.description(value);
- } else if ("schema".equals(name)) {
- r2.schema(jp.parse(value, SchemaInfo.class));
- } else if ("examples".equals(name)) {
- r2.examples(jp.parse(value, TreeMap.class));
- } else {
- System.err.println("Unknown bundle key '"+key+"'");
- }
- }
- }
-
- return m.isEmpty() ? null : m;
- }
-
- /**
- * Returns <jk>true</jk> if the specified request object can call this method.
- */
- boolean isRequestAllowed(RestRequest req) {
- for (RestGuard guard : guards) {
- req.setJavaMethod(method);
- if (! guard.isRequestAllowed(req))
- return false;
- }
- return true;
- }
-
- /**
- * Workhorse method.
- *
- * @param pathInfo The value of {@link HttpServletRequest#getPathInfo()} (sorta)
- * @return The HTTP response code.
- */
- int invoke(String pathInfo, RestRequest req, RestResponse res) throws RestException {
-
- String[] patternVals = pathPattern.match(pathInfo);
- if (patternVals == null)
- return SC_NOT_FOUND;
-
- String remainder = null;
- if (patternVals.length > pathPattern.getVars().length)
- remainder = patternVals[pathPattern.getVars().length];
- for (int i = 0; i < pathPattern.getVars().length; i++)
- req.getPathMatch().put(pathPattern.getVars()[i], patternVals[i]);
- req.getPathMatch().setRemainder(remainder);
-
- ObjectMap requestProperties = new ResolvingObjectMap(req.getVarResolverSession()).setInner(properties);
-
- req.init(method, requestProperties, defaultRequestHeaders, defaultQuery, defaultFormData, defaultCharset,
- maxInput, serializers, parsers, partParser, beanContext, encoders, widgets, supportedAcceptTypes, supportedContentTypes);
- res.init(requestProperties, defaultCharset, serializers, partSerializer, encoders);
-
- // Class-level guards
- for (RestGuard guard : context.getGuards())
- if (! guard.guard(req, res))
- return SC_UNAUTHORIZED;
-
- // If the method implements matchers, test them.
- for (RestMatcher m : requiredMatchers)
- if (! m.matches(req))
- return SC_PRECONDITION_FAILED;
- if (optionalMatchers.length > 0) {
- boolean matches = false;
- for (RestMatcher m : optionalMatchers)
- matches |= m.matches(req);
- if (! matches)
- return SC_PRECONDITION_FAILED;
- }
-
- context.preCall(req, res);
-
- Object[] args = new Object[params.length];
- for (int i = 0; i < params.length; i++) {
- try {
- args[i] = params[i].resolve(req, res);
- } catch (RestException e) {
- throw e;
- } catch (Exception e) {
- throw new RestException(SC_BAD_REQUEST,
- "Invalid data conversion. Could not convert {0} ''{1}'' to type ''{2}'' on method ''{3}.{4}''.",
- params[i].getParamType().name(), params[i].getName(), params[i].getType(), method.getDeclaringClass().getName(), method.getName()
- ).initCause(e);
- }
- }
-
- try {
-
- for (RestGuard guard : guards)
- if (! guard.guard(req, res))
- return SC_OK;
-
- Object output = method.invoke(context.getResource(), args);
- if (! method.getReturnType().equals(Void.TYPE))
- if (output != null || ! res.getOutputStreamCalled())
- res.setOutput(output);
-
- context.postCall(req, res);
-
- if (res.hasOutput()) {
- output = res.getOutput();
- for (RestConverter converter : converters)
- output = converter.convert(req, output, beanContext.getClassMetaForObject(output));
- res.setOutput(output);
- }
- } catch (IllegalArgumentException e) {
- throw new RestException(SC_BAD_REQUEST,
- "Invalid argument type passed to the following method: ''{0}''.\n\tArgument types: {1}",
- method.toString(), getReadableClassNames(args)
- ).initCause(e);
- } catch (InvocationTargetException e) {
- Throwable e2 = e.getTargetException(); // Get the throwable thrown from the doX() method.
- if (e2 instanceof RestException)
- throw (RestException)e2;
- if (e2 instanceof ParseException)
- throw new RestException(SC_BAD_REQUEST, e2);
- if (e2 instanceof InvalidDataConversionException)
- throw new RestException(SC_BAD_REQUEST, e2);
- throw new RestException(SC_INTERNAL_SERVER_ERROR, e2);
- } catch (RestException e) {
- throw e;
- } catch (Exception e) {
- throw new RestException(SC_INTERNAL_SERVER_ERROR, e);
- }
- return SC_OK;
- }
-
- @Override /* Object */
- public String toString() {
- return "SimpleMethod: name=" + httpMethod + ", path=" + pathPattern.getPatternString();
- }
-
- /*
- * compareTo() method is used to keep SimpleMethods ordered in the CallRouter list.
- * It maintains the order in which matches are made during requests.
- */
- @Override /* Comparable */
- public int compareTo(CallMethod o) {
- int c;
-
- c = priority.compareTo(o.priority);
- if (c != 0)
- return c;
-
- c = pathPattern.compareTo(o.pathPattern);
- if (c != 0)
- return c;
-
- c = compare(o.requiredMatchers.length, requiredMatchers.length);
- if (c != 0)
- return c;
-
- c = compare(o.optionalMatchers.length, optionalMatchers.length);
- if (c != 0)
- return c;
-
- c = compare(o.guards.length, guards.length);
- if (c != 0)
- return c;
-
- return 0;
- }
-
- @Override /* Object */
- public boolean equals(Object o) {
- if (! (o instanceof CallMethod))
- return false;
- return (compareTo((CallMethod)o) == 0);
- }
-
- @Override /* Object */
- public int hashCode() {
- return super.hashCode();
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/juneau/blob/96fae4f9/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/CallRouter.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/CallRouter.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/CallRouter.java
deleted file mode 100644
index fc6c424..0000000
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/CallRouter.java
+++ /dev/null
@@ -1,100 +0,0 @@
-// ***************************************************************************************************************************
-// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
-// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
-// * to you 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 org.apache.juneau.rest;
-
-import static javax.servlet.http.HttpServletResponse.*;
-
-import java.util.*;
-
-import javax.servlet.http.*;
-
-/**
- * Represents a group of CallMethods on a REST resource that handle the same HTTP Method name but with different
- * paths/matchers/guards/etc...
- *
- * <p>
- * Incoming requests for a particular HTTP method type (e.g. <js>"GET"</js>) are handed off to this class and then
- * dispatched to the appropriate CallMethod.
- */
-class CallRouter {
- private final CallMethod[] callMethods;
-
- CallRouter(CallMethod[] callMethods) {
- this.callMethods = callMethods;
- }
-
- /**
- * Builder class.
- */
- static final class Builder {
- private List<CallMethod> childMethods = new ArrayList<>();
- private Set<String> collisions = new HashSet<>();
- private String httpMethodName;
-
- Builder(String httpMethodName) {
- this.httpMethodName = httpMethodName;
- }
-
- String getHttpMethodName() {
- return httpMethodName;
- }
-
- Builder add(CallMethod m) throws RestServletException {
- if (! m.hasGuardsOrMatchers()) {
- String p = m.getHttpMethod() + ":" + m.getPathPattern();
- if (collisions.contains(p))
- throw new RestServletException("Duplicate Java methods assigned to the same method/pattern: ''{0}''", p);
- collisions.add(p);
- }
- childMethods.add(m);
- return this;
- }
-
- CallRouter build() {
- Collections.sort(childMethods);
- return new CallRouter(childMethods.toArray(new CallMethod[childMethods.size()]));
- }
- }
-
- /**
- * Workhorse method.
- *
- * <p>
- * Routes this request to one of the CallMethods.
- *
- * @param pathInfo The value of {@link HttpServletRequest#getPathInfo()} (sorta)
- * @return The HTTP response code.
- */
- int invoke(String pathInfo, RestRequest req, RestResponse res) throws RestException {
- if (callMethods.length == 1)
- return callMethods[0].invoke(pathInfo, req, res);
-
- int maxRc = 0;
- for (CallMethod m : callMethods) {
- int rc = m.invoke(pathInfo, req, res);
- if (rc == SC_OK)
- return SC_OK;
- maxRc = Math.max(maxRc, rc);
- }
- return maxRc;
- }
-
- @Override /* Object */
- public String toString() {
- StringBuilder sb = new StringBuilder("CallRouter: [\n");
- for (CallMethod sm : callMethods)
- sb.append("\t" + sm + "\n");
- sb.append("]");
- return sb.toString();
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/juneau/blob/96fae4f9/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallHandler.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallHandler.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallHandler.java
index 53e929e..adcebbb 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallHandler.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallHandler.java
@@ -44,7 +44,7 @@ public class RestCallHandler {
private final RestContext context;
private final RestLogger logger;
- private final Map<String,CallRouter> callRouters;
+ private final Map<String,RestCallRouter> restCallRouters;
/**
* Constructor.
@@ -54,7 +54,7 @@ public class RestCallHandler {
public RestCallHandler(RestContext context) {
this.context = context;
this.logger = context.getLogger();
- this.callRouters = context.getCallRouters();
+ this.restCallRouters = context.getCallRouters();
}
/**
@@ -153,10 +153,10 @@ public class RestCallHandler {
} else {
// If the specified method has been defined in a subclass, invoke it.
int rc = SC_METHOD_NOT_ALLOWED;
- if (callRouters.containsKey(methodUC)) {
- rc = callRouters.get(methodUC).invoke(pathInfo, req, res);
- } else if (callRouters.containsKey("*")) {
- rc = callRouters.get("*").invoke(pathInfo, req, res);
+ if (restCallRouters.containsKey(methodUC)) {
+ rc = restCallRouters.get(methodUC).invoke(pathInfo, req, res);
+ } else if (restCallRouters.containsKey("*")) {
+ rc = restCallRouters.get("*").invoke(pathInfo, req, res);
}
// If not invoked above, see if it's an OPTIONs request
http://git-wip-us.apache.org/repos/asf/juneau/blob/96fae4f9/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallRouter.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallRouter.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallRouter.java
new file mode 100644
index 0000000..1cf1fc7
--- /dev/null
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestCallRouter.java
@@ -0,0 +1,100 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
+// * to you 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 org.apache.juneau.rest;
+
+import static javax.servlet.http.HttpServletResponse.*;
+
+import java.util.*;
+
+import javax.servlet.http.*;
+
+/**
+ * Represents a group of CallMethods on a REST resource that handle the same HTTP Method name but with different
+ * paths/matchers/guards/etc...
+ *
+ * <p>
+ * Incoming requests for a particular HTTP method type (e.g. <js>"GET"</js>) are handed off to this class and then
+ * dispatched to the appropriate RestJavaMethod.
+ */
+class RestCallRouter {
+ private final RestJavaMethod[] restJavaMethods;
+
+ RestCallRouter(RestJavaMethod[] callMethods) {
+ this.restJavaMethods = callMethods;
+ }
+
+ /**
+ * Builder class.
+ */
+ static final class Builder {
+ private List<RestJavaMethod> childMethods = new ArrayList<>();
+ private Set<String> collisions = new HashSet<>();
+ private String httpMethodName;
+
+ Builder(String httpMethodName) {
+ this.httpMethodName = httpMethodName;
+ }
+
+ String getHttpMethodName() {
+ return httpMethodName;
+ }
+
+ Builder add(RestJavaMethod m) throws RestServletException {
+ if (! m.hasGuardsOrMatchers()) {
+ String p = m.getHttpMethod() + ":" + m.getPathPattern();
+ if (collisions.contains(p))
+ throw new RestServletException("Duplicate Java methods assigned to the same method/pattern: ''{0}''", p);
+ collisions.add(p);
+ }
+ childMethods.add(m);
+ return this;
+ }
+
+ RestCallRouter build() {
+ Collections.sort(childMethods);
+ return new RestCallRouter(childMethods.toArray(new RestJavaMethod[childMethods.size()]));
+ }
+ }
+
+ /**
+ * Workhorse method.
+ *
+ * <p>
+ * Routes this request to one of the CallMethods.
+ *
+ * @param pathInfo The value of {@link HttpServletRequest#getPathInfo()} (sorta)
+ * @return The HTTP response code.
+ */
+ int invoke(String pathInfo, RestRequest req, RestResponse res) throws RestException {
+ if (restJavaMethods.length == 1)
+ return restJavaMethods[0].invoke(pathInfo, req, res);
+
+ int maxRc = 0;
+ for (RestJavaMethod m : restJavaMethods) {
+ int rc = m.invoke(pathInfo, req, res);
+ if (rc == SC_OK)
+ return SC_OK;
+ maxRc = Math.max(maxRc, rc);
+ }
+ return maxRc;
+ }
+
+ @Override /* Object */
+ public String toString() {
+ StringBuilder sb = new StringBuilder("RestCallRouter: [\n");
+ for (RestJavaMethod sm : restJavaMethods)
+ sb.append("\t" + sm + "\n");
+ sb.append("]");
+ return sb.toString();
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/juneau/blob/96fae4f9/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
index 0810abc..7ab8ee7 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
@@ -1434,8 +1434,8 @@ public final class RestContext extends BeanContext {
private final MessageBundle msgs;
private final ConfigFile configFile;
private final VarResolver varResolver;
- private final Map<String,CallRouter> callRouters;
- private final Map<String,CallMethod> callMethods;
+ private final Map<String,RestCallRouter> callRouters;
+ private final Map<String,RestJavaMethod> callMethods;
private final Map<String,RestContext> childResources;
private final RestLogger logger;
private final RestCallHandler callHandler;
@@ -1589,8 +1589,8 @@ public final class RestContext extends BeanContext {
// Done after initializing fields above since we pass this object to the child resources.
//----------------------------------------------------------------------------------------------------
List<String> methodsFound = new LinkedList<>(); // Temporary to help debug transient duplicate method issue.
- Map<String,CallRouter.Builder> routers = new LinkedHashMap<>();
- Map<String,CallMethod> _javaRestMethods = new LinkedHashMap<>();
+ Map<String,RestCallRouter.Builder> routers = new LinkedHashMap<>();
+ Map<String,RestJavaMethod> _javaRestMethods = new LinkedHashMap<>();
Map<String,Method>
_startCallMethods = new LinkedHashMap<>(),
_preCallMethods = new LinkedHashMap<>(),
@@ -1617,7 +1617,7 @@ public final class RestContext extends BeanContext {
if (! Modifier.isPublic(method.getModifiers()))
throw new RestServletException("@RestMethod method {0}.{1} must be defined as public.", resourceClass.getName(), method.getName());
- CallMethod sm = new CallMethod(resource, method, this);
+ RestJavaMethod sm = new RestJavaMethod(resource, method, this);
String httpMethod = sm.getHttpMethod();
// PROXY is a special case where a method returns an interface that we
@@ -1630,7 +1630,7 @@ public final class RestContext extends BeanContext {
if (remoteableMethods.isEmpty())
throw new RestException(SC_INTERNAL_SERVER_ERROR, "Method {0} returns an interface {1} that doesn't define any remoteable methods.", getMethodSignature(method), interfaceClass.getReadableName());
- sm = new CallMethod(resource, method, this) {
+ sm = new RestJavaMethod(resource, method, this) {
@Override
int invoke(String pathInfo, RestRequest req, RestResponse res) throws RestException {
@@ -1769,8 +1769,8 @@ public final class RestContext extends BeanContext {
this.postInitChildFirstMethodParams = _postInitChildFirstMethodParams.toArray(new Class[_postInitChildFirstMethodParams.size()][]);
this.destroyMethodParams = _destroyMethodParams.toArray(new Class[_destroyMethodParams.size()][]);
- Map<String,CallRouter> _callRouters = new LinkedHashMap<>();
- for (CallRouter.Builder crb : routers.values())
+ Map<String,RestCallRouter> _callRouters = new LinkedHashMap<>();
+ for (RestCallRouter.Builder crb : routers.values())
_callRouters.put(crb.getHttpMethodName(), crb.build());
this.callRouters = Collections.unmodifiableMap(_callRouters);
@@ -1829,9 +1829,9 @@ public final class RestContext extends BeanContext {
}
}
- private static void addToRouter(Map<String, CallRouter.Builder> routers, String httpMethodName, CallMethod cm) throws RestServletException {
+ private static void addToRouter(Map<String, RestCallRouter.Builder> routers, String httpMethodName, RestJavaMethod cm) throws RestServletException {
if (! routers.containsKey(httpMethodName))
- routers.put(httpMethodName, new CallRouter.Builder(httpMethodName));
+ routers.put(httpMethodName, new RestCallRouter.Builder(httpMethodName));
routers.get(httpMethodName).add(cm);
}
@@ -2214,7 +2214,7 @@ public final class RestContext extends BeanContext {
*
* @return A map with HTTP method names upper-cased as the keys, and call routers as the values.
*/
- protected Map<String,CallRouter> getCallRouters() {
+ protected Map<String,RestCallRouter> getCallRouters() {
return callRouters;
}
@@ -2848,7 +2848,7 @@ public final class RestContext extends BeanContext {
*
* @return A map of Java method names to call method objects.
*/
- protected Map<String,CallMethod> getCallMethods() {
+ protected Map<String,RestJavaMethod> getCallMethods() {
return callMethods;
}
http://git-wip-us.apache.org/repos/asf/juneau/blob/96fae4f9/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestInfoProvider.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestInfoProvider.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestInfoProvider.java
index e40e26a..5bef97b 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestInfoProvider.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestInfoProvider.java
@@ -141,7 +141,7 @@ public class RestInfoProvider {
.tags(getTags(req))
.externalDocs(getExternalDocs(req));
- for (CallMethod sm : context.getCallMethods().values()) {
+ for (RestJavaMethod sm : context.getCallMethods().values()) {
if (sm.isRequestAllowed(req)) {
Operation o = sm.getSwaggerOperation(req);
s.path(
@@ -207,7 +207,7 @@ public class RestInfoProvider {
* @return The localized summary of the method, or a blank string if no summary was found.
*/
public String getMethodSummary(String javaMethodName, RestRequest req) {
- CallMethod m = context.getCallMethods().get(javaMethodName);
+ RestJavaMethod m = context.getCallMethods().get(javaMethodName);
if (m != null)
return m.getSummary(req);
return "";
@@ -257,7 +257,7 @@ public class RestInfoProvider {
* @return The localized description of the method, or a blank string if no description was found.
*/
public String getMethodDescription(String javaMethodName, RestRequest req) {
- CallMethod m = context.getCallMethods().get(javaMethodName);
+ RestJavaMethod m = context.getCallMethods().get(javaMethodName);
if (m != null)
return m.getDescription(req);
return "";
http://git-wip-us.apache.org/repos/asf/juneau/blob/96fae4f9/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestJavaMethod.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestJavaMethod.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestJavaMethod.java
new file mode 100644
index 0000000..e172e69
--- /dev/null
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestJavaMethod.java
@@ -0,0 +1,921 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
+// * to you 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 org.apache.juneau.rest;
+
+import static javax.servlet.http.HttpServletResponse.*;
+import static org.apache.juneau.dto.swagger.SwaggerBuilder.*;
+import static org.apache.juneau.internal.ClassUtils.*;
+import static org.apache.juneau.internal.StringUtils.*;
+import static org.apache.juneau.internal.Utils.*;
+import static org.apache.juneau.BeanContext.*;
+import static org.apache.juneau.rest.RestContext.*;
+
+import java.lang.annotation.*;
+import java.lang.reflect.*;
+import java.util.*;
+
+import javax.servlet.http.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.dto.swagger.*;
+import org.apache.juneau.encoders.*;
+import org.apache.juneau.http.*;
+import org.apache.juneau.httppart.*;
+import org.apache.juneau.httppart.HttpPartParser;
+import org.apache.juneau.internal.*;
+import org.apache.juneau.json.*;
+import org.apache.juneau.parser.*;
+import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.rest.widget.*;
+import org.apache.juneau.serializer.*;
+import org.apache.juneau.svl.*;
+import org.apache.juneau.utils.*;
+
+/**
+ * Represents a single Java servlet/resource method annotated with {@link RestMethod @RestMethod}.
+ */
+class RestJavaMethod implements Comparable<RestJavaMethod> {
+ private final java.lang.reflect.Method method;
+ private final String httpMethod;
+ private final UrlPathPattern pathPattern;
+ private final RestParam[] params;
+ private final RestGuard[] guards;
+ private final RestMatcher[] optionalMatchers;
+ private final RestMatcher[] requiredMatchers;
+ private final RestConverter[] converters;
+ private final SerializerGroup serializers;
+ private final ParserGroup parsers;
+ private final EncoderGroup encoders;
+ private final HttpPartParser partParser;
+ private final HttpPartSerializer partSerializer;
+ private final ObjectMap properties;
+ private final Map<String,Object> defaultRequestHeaders, defaultQuery, defaultFormData;
+ private final String defaultCharset;
+ private final long maxInput;
+ private final boolean deprecated;
+ private final String description, tags, summary, externalDocs;
+ private final Integer priority;
+ private final org.apache.juneau.rest.annotation.Parameter[] parameters;
+ private final Response[] responses;
+ private final RestContext context;
+ private final BeanContext beanContext;
+ private final Map<String,Widget> widgets;
+ private final List<MediaType> supportedAcceptTypes, supportedContentTypes;
+
+ RestJavaMethod(Object servlet, java.lang.reflect.Method method, RestContext context) throws RestServletException {
+ Builder b = new Builder(servlet, method, context);
+ this.context = context;
+ this.method = method;
+ this.httpMethod = b.httpMethod;
+ this.pathPattern = b.pathPattern;
+ this.params = b.params;
+ this.guards = b.guards;
+ this.optionalMatchers = b.optionalMatchers;
+ this.requiredMatchers = b.requiredMatchers;
+ this.converters = b.converters;
+ this.serializers = b.serializers;
+ this.parsers = b.parsers;
+ this.encoders = b.encoders;
+ this.partParser = b.partParser;
+ this.partSerializer = b.partSerializer;
+ this.beanContext = b.beanContext;
+ this.properties = b.properties;
+ this.defaultRequestHeaders = b.defaultRequestHeaders;
+ this.defaultQuery = b.defaultQuery;
+ this.defaultFormData = b.defaultFormData;
+ this.defaultCharset = b.defaultCharset;
+ this.maxInput = b.maxInput;
+ this.deprecated = b.deprecated;
+ this.description = b.description;
+ this.tags = b.tags;
+ this.summary = b.summary;
+ this.externalDocs = b.externalDocs;
+ this.priority = b.priority;
+ this.parameters = b.parameters;
+ this.responses = b.responses;
+ this.supportedAcceptTypes = b.supportedAcceptTypes;
+ this.supportedContentTypes = b.supportedContentTypes;
+ this.widgets = Collections.unmodifiableMap(b.widgets);
+ }
+
+ private static final class Builder {
+ String httpMethod, defaultCharset, description, tags, summary, externalDocs;
+ UrlPathPattern pathPattern;
+ RestParam[] params;
+ RestGuard[] guards;
+ RestMatcher[] optionalMatchers, requiredMatchers;
+ RestConverter[] converters;
+ SerializerGroup serializers;
+ ParserGroup parsers;
+ EncoderGroup encoders;
+ HttpPartParser partParser;
+ HttpPartSerializer partSerializer;
+ BeanContext beanContext;
+ ObjectMap properties;
+ Map<String,Object> defaultRequestHeaders, defaultQuery, defaultFormData;
+ boolean deprecated;
+ long maxInput;
+ Integer priority;
+ org.apache.juneau.rest.annotation.Parameter[] parameters;
+ Response[] responses;
+ Map<String,Widget> widgets;
+ List<MediaType> supportedAcceptTypes, supportedContentTypes;
+
+ Builder(Object servlet, java.lang.reflect.Method method, RestContext context) throws RestServletException {
+ String sig = method.getDeclaringClass().getName() + '.' + method.getName();
+
+ try {
+
+ RestMethod m = method.getAnnotation(RestMethod.class);
+ if (m == null)
+ throw new RestServletException("@RestMethod annotation not found on method ''{0}''", sig);
+
+ if (! m.description().isEmpty())
+ description = m.description();
+ MethodSwagger sm = m.swagger();
+ if (! sm.tags().isEmpty())
+ tags = sm.tags();
+ if (! m.summary().isEmpty())
+ summary = m.summary();
+ if (! sm.externalDocs().isEmpty())
+ externalDocs = sm.externalDocs();
+ deprecated = sm.deprecated();
+ parameters = sm.parameters();
+ responses = sm.responses();
+ serializers = context.getSerializers();
+ parsers = context.getParsers();
+ partSerializer = context.getPartSerializer();
+ partParser = context.getPartParser();
+ beanContext = context.getBeanContext();
+ encoders = context.getEncoders();
+ properties = new ObjectMap().setInner(context.getProperties());
+ defaultCharset = context.getDefaultCharset();
+ maxInput = context.getMaxInput();
+
+ if (! m.defaultCharset().isEmpty())
+ defaultCharset = context.getVarResolver().resolve(m.defaultCharset());
+ if (! m.maxInput().isEmpty())
+ maxInput = StringUtils.parseLongWithSuffix(context.getVarResolver().resolve(m.maxInput()));
+
+ HtmlDocBuilder hdb = new HtmlDocBuilder(properties);
+
+ HtmlDoc hd = m.htmldoc();
+ hdb.process(hd);
+
+ widgets = new HashMap<>(context.getWidgets());
+ for (Class<? extends Widget> wc : hd.widgets()) {
+ Widget w = beanContext.newInstance(Widget.class, wc);
+ widgets.put(w.getName(), w);
+ hdb.script("INHERIT", "$W{"+w.getName()+".script}");
+ hdb.style("INHERIT", "$W{"+w.getName()+".style}");
+ }
+
+ ASet<String> inherit = new ASet<String>().appendAll(StringUtils.split(m.inherit()));
+ if (inherit.contains("*"))
+ inherit.appendAll("SERIALIZERS","PARSERS","TRANSFORMS","PROPERTIES","ENCODERS");
+
+ SerializerGroupBuilder sgb = null;
+ ParserGroupBuilder pgb = null;
+ ParserBuilder uepb = null;
+ BeanContextBuilder bcb = null;
+ PropertyStore cps = context.getPropertyStore();
+
+ if (m.serializers().length > 0 || m.parsers().length > 0 || m.properties().length > 0 || m.flags().length > 0
+ || m.beanFilters().length > 0 || m.pojoSwaps().length > 0 || m.bpi().length > 0
+ || m.bpx().length > 0) {
+ sgb = SerializerGroup.create();
+ pgb = ParserGroup.create();
+ uepb = Parser.create();
+ bcb = beanContext.builder();
+
+ if (inherit.contains("SERIALIZERS") || m.serializers().length == 0)
+ sgb.append(cps.getArrayProperty(REST_serializers, Object.class));
+
+ if (inherit.contains("PARSERS") || m.parsers().length == 0)
+ pgb.append(cps.getArrayProperty(REST_parsers, Object.class));
+ }
+
+ httpMethod = m.name().toUpperCase(Locale.ENGLISH);
+ if (httpMethod.equals("") && method.getName().startsWith("do"))
+ httpMethod = method.getName().substring(2).toUpperCase(Locale.ENGLISH);
+ if (httpMethod.equals(""))
+ httpMethod = "GET";
+ if (httpMethod.equals("METHOD"))
+ httpMethod = "*";
+
+ priority = m.priority();
+
+ String p = m.path();
+ converters = new RestConverter[m.converters().length];
+ for (int i = 0; i < converters.length; i++)
+ converters[i] = beanContext.newInstance(RestConverter.class, m.converters()[i]);
+
+ guards = new RestGuard[m.guards().length];
+ for (int i = 0; i < guards.length; i++)
+ guards[i] = beanContext.newInstance(RestGuard.class, m.guards()[i]);
+
+ List<RestMatcher> optionalMatchers = new LinkedList<>(), requiredMatchers = new LinkedList<>();
+ for (int i = 0; i < m.matchers().length; i++) {
+ Class<? extends RestMatcher> c = m.matchers()[i];
+ RestMatcher matcher = beanContext.newInstance(RestMatcher.class, c, true, servlet, method);
+ if (matcher.mustMatch())
+ requiredMatchers.add(matcher);
+ else
+ optionalMatchers.add(matcher);
+ }
+ if (! m.clientVersion().isEmpty())
+ requiredMatchers.add(new ClientVersionMatcher(context.getClientVersionHeader(), method));
+
+ this.requiredMatchers = requiredMatchers.toArray(new RestMatcher[requiredMatchers.size()]);
+ this.optionalMatchers = optionalMatchers.toArray(new RestMatcher[optionalMatchers.size()]);
+
+ PropertyStore ps = context.getPropertyStore();
+ if (! inherit.contains("TRANSFORMS"))
+ ps = ps.builder().set(BEAN_beanFilters, null).set(BEAN_pojoSwaps, null).build();
+
+ if (sgb != null) {
+ sgb.append(m.serializers());
+
+ if (! inherit.contains("PROPERTIES"))
+ sgb.beanFilters(ps.getClassArrayProperty(BEAN_beanFilters)).pojoSwaps(ps.getClassArrayProperty(BEAN_pojoSwaps));
+ else
+ sgb.apply(ps);
+ for (Property p1 : m.properties())
+ sgb.set(p1.name(), p1.value());
+ for (String p1 : m.flags())
+ sgb.set(p1, true);
+ if (m.bpi().length > 0) {
+ Map<String,String> bpiMap = new LinkedHashMap<>();
+ for (String s : m.bpi()) {
+ for (String s2 : split(s, ';')) {
+ int i = s2.indexOf(':');
+ if (i == -1)
+ throw new RestServletException(
+ "Invalid format for @RestMethod.bpi() on method ''{0}''. Must be in the format \"ClassName: comma-delimited-tokens\". \nValue: {1}", sig, s);
+ bpiMap.put(s2.substring(0, i).trim(), s2.substring(i+1).trim());
+ }
+ }
+ sgb.includeProperties(bpiMap);
+ }
+ if (m.bpx().length > 0) {
+ Map<String,String> bpxMap = new LinkedHashMap<>();
+ for (String s : m.bpx()) {
+ for (String s2 : split(s, ';')) {
+ int i = s2.indexOf(':');
+ if (i == -1)
+ throw new RestServletException(
+ "Invalid format for @RestMethod.bpx() on method ''{0}''. Must be in the format \"ClassName: comma-delimited-tokens\". \nValue: {1}", sig, s);
+ bpxMap.put(s2.substring(0, i).trim(), s2.substring(i+1).trim());
+ }
+ }
+ sgb.excludeProperties(bpxMap);
+ }
+ sgb.beanFilters(m.beanFilters());
+ sgb.pojoSwaps(m.pojoSwaps());
+ }
+
+ if (pgb != null) {
+ pgb.append(m.parsers());
+ if (! inherit.contains("PROPERTIES"))
+ pgb.beanFilters(ps.getClassArrayProperty(BEAN_beanFilters)).pojoSwaps(ps.getClassArrayProperty(BEAN_pojoSwaps));
+ else
+ pgb.apply(ps);
+ for (Property p1 : m.properties())
+ pgb.set(p1.name(), p1.value());
+ for (String p1 : m.flags())
+ pgb.set(p1, true);
+ pgb.beanFilters(m.beanFilters());
+ pgb.pojoSwaps(m.pojoSwaps());
+ }
+
+ if (uepb != null) {
+ uepb.apply(ps);
+ for (Property p1 : m.properties())
+ uepb.set(p1.name(), p1.value());
+ for (String p1 : m.flags())
+ uepb.set(p1, true);
+ uepb.beanFilters(m.beanFilters());
+ uepb.pojoSwaps(m.pojoSwaps());
+ }
+
+ if (bcb != null) {
+ bcb.apply(ps);
+ for (Property p1 : m.properties())
+ bcb.set(p1.name(), p1.value());
+ for (String p1 : m.flags())
+ bcb.set(p1, true);
+ bcb.beanFilters(m.beanFilters());
+ bcb.pojoSwaps(m.pojoSwaps());
+ }
+
+ if (m.properties().length > 0 || m.flags().length > 0) {
+ properties = new ObjectMap().setInner(properties);
+ for (Property p1 : m.properties())
+ properties.put(p1.name(), p1.value());
+ for (String p1 : m.flags())
+ properties.put(p1, true);
+ }
+
+ if (m.encoders().length > 0) {
+ EncoderGroupBuilder g = EncoderGroup.create().append(IdentityEncoder.INSTANCE);
+ if (inherit.contains("ENCODERS"))
+ g.append(encoders);
+
+ for (Class<? extends Encoder> c : m.encoders()) {
+ try {
+ g.append(c);
+ } catch (Exception e) {
+ throw new RestServletException(
+ "Exception occurred while trying to instantiate Encoder on method ''{0}'': ''{1}''", sig, c.getSimpleName()).initCause(e);
+ }
+ }
+ encoders = g.build();
+ }
+
+ defaultRequestHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
+ for (String s : m.defaultRequestHeaders()) {
+ String[] h = RestUtils.parseKeyValuePair(s);
+ if (h == null)
+ throw new RestServletException(
+ "Invalid default request header specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s);
+ defaultRequestHeaders.put(h[0], h[1]);
+ }
+
+ defaultQuery = new LinkedHashMap<>();
+ for (String s : m.defaultQuery()) {
+ String[] h = RestUtils.parseKeyValuePair(s);
+ if (h == null)
+ throw new RestServletException(
+ "Invalid default query parameter specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s);
+ defaultQuery.put(h[0], h[1]);
+ }
+
+ defaultFormData = new LinkedHashMap<>();
+ for (String s : m.defaultFormData()) {
+ String[] h = RestUtils.parseKeyValuePair(s);
+ if (h == null)
+ throw new RestServletException(
+ "Invalid default form data parameter specified on method ''{0}'': ''{1}''. Must be in the format: ''name[:=]value''", sig, s);
+ defaultFormData.put(h[0], h[1]);
+ }
+
+ Type[] pt = method.getGenericParameterTypes();
+ Annotation[][] pa = method.getParameterAnnotations();
+ for (int i = 0; i < pt.length; i++) {
+ for (Annotation a : pa[i]) {
+ if (a instanceof Header) {
+ Header h = (Header)a;
+ if (! h.def().isEmpty())
+ defaultRequestHeaders.put(firstNonEmpty(h.name(), h.value()), h.def());
+ } else if (a instanceof Query) {
+ Query q = (Query)a;
+ if (! q.def().isEmpty())
+ defaultQuery.put(firstNonEmpty(q.name(), q.value()), q.def());
+ } else if (a instanceof FormData) {
+ FormData f = (FormData)a;
+ if (! f.def().isEmpty())
+ defaultFormData.put(firstNonEmpty(f.name(), f.value()), f.def());
+ }
+ }
+ }
+
+ pathPattern = new UrlPathPattern(p);
+
+ if (sgb != null)
+ serializers = sgb.build();
+ if (pgb != null)
+ parsers = pgb.build();
+ if (uepb != null && partParser instanceof Parser) {
+ Parser pp = (Parser)partParser;
+ partParser = (HttpPartParser)pp.builder().apply(uepb.getPropertyStore()).build();
+ }
+ if (bcb != null)
+ beanContext = bcb.build();
+
+ supportedAcceptTypes =
+ m.supportedAcceptTypes().length > 0
+ ? Collections.unmodifiableList(new ArrayList<>(Arrays.asList(MediaType.forStrings(m.supportedAcceptTypes()))))
+ : serializers.getSupportedMediaTypes();
+ supportedContentTypes =
+ m.supportedContentTypes().length > 0
+ ? Collections.unmodifiableList(new ArrayList<>(Arrays.asList(MediaType.forStrings(m.supportedContentTypes()))))
+ : parsers.getSupportedMediaTypes();
+
+ params = context.findParams(method, pathPattern, false);
+
+ // Need this to access methods in anonymous inner classes.
+ method.setAccessible(true);
+ } catch (RestServletException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new RestServletException("Exception occurred while initializing method ''{0}''", sig).initCause(e);
+ }
+ }
+ }
+
+ /**
+ * Returns <jk>true</jk> if this Java method has any guards or matchers.
+ */
+ boolean hasGuardsOrMatchers() {
+ return (guards.length != 0 || requiredMatchers.length != 0 || optionalMatchers.length != 0);
+ }
+
+ /**
+ * Returns the HTTP method name (e.g. <js>"GET"</js>).
+ */
+ String getHttpMethod() {
+ return httpMethod;
+ }
+
+ /**
+ * Returns the path pattern for this method.
+ */
+ String getPathPattern() {
+ return pathPattern.toString();
+ }
+
+ /**
+ * Returns the localized Swagger for this Java method.
+ */
+ Operation getSwaggerOperation(RestRequest req) throws ParseException {
+ Operation o = operation()
+ .operationId(method.getName())
+ .description(getDescription(req))
+ .tags(getTags(req))
+ .summary(getSummary(req))
+ .externalDocs(getExternalDocs(req))
+ .parameters(getParameters(req))
+ .responses(getResponses(req));
+
+ if (isDeprecated())
+ o.deprecated(true);
+
+ if (! parsers.getSupportedMediaTypes().equals(context.getParsers().getSupportedMediaTypes()))
+ o.consumes(parsers.getSupportedMediaTypes());
+
+ if (! serializers.getSupportedMediaTypes().equals(context.getSerializers().getSupportedMediaTypes()))
+ o.produces(serializers.getSupportedMediaTypes());
+
+ return o;
+ }
+
+ private Operation getSwaggerOperationFromFile(RestRequest req) {
+ Swagger s = req.getSwaggerFromFile();
+ if (s != null && s.getPaths() != null && s.getPaths().get(pathPattern.getPatternString()) != null)
+ return s.getPaths().get(pathPattern.getPatternString()).get(httpMethod);
+ return null;
+ }
+
+ /**
+ * Returns the localized summary for this Java method.
+ */
+ String getSummary(RestRequest req) {
+ VarResolverSession vr = req.getVarResolverSession();
+ if (summary != null)
+ return vr.resolve(summary);
+ String summary = context.getMessages().findFirstString(req.getLocale(), method.getName() + ".summary");
+ if (summary != null)
+ return vr.resolve(summary);
+ Operation o = getSwaggerOperationFromFile(req);
+ if (o != null)
+ return o.getSummary();
+ return null;
+ }
+
+ /**
+ * Returns the localized description for this Java method.
+ */
+ String getDescription(RestRequest req) {
+ VarResolverSession vr = req.getVarResolverSession();
+ if (description != null)
+ return vr.resolve(description);
+ String description = context.getMessages().findFirstString(req.getLocale(), method.getName() + ".description");
+ if (description != null)
+ return vr.resolve(description);
+ Operation o = getSwaggerOperationFromFile(req);
+ if (o != null)
+ return o.getDescription();
+ return null;
+ }
+
+ /**
+ * Returns the localized Swagger tags for this Java method.
+ */
+ private List<String> getTags(RestRequest req) {
+ VarResolverSession vr = req.getVarResolverSession();
+ JsonParser jp = JsonParser.DEFAULT;
+ try {
+ if (tags != null)
+ return jp.parse(vr.resolve(tags), ArrayList.class, String.class);
+ String tags = context.getMessages().findFirstString(req.getLocale(), method.getName() + ".tags");
+ if (tags != null)
+ return jp.parse(vr.resolve(tags), ArrayList.class, String.class);
+ Operation o = getSwaggerOperationFromFile(req);
+ if (o != null)
+ return o.getTags();
+ return null;
+ } catch (Exception e) {
+ throw new RestException(SC_INTERNAL_SERVER_ERROR, e);
+ }
+ }
+
+ /**
+ * Returns the localized Swagger external docs for this Java method.
+ */
+ private ExternalDocumentation getExternalDocs(RestRequest req) {
+ VarResolverSession vr = req.getVarResolverSession();
+ JsonParser jp = JsonParser.DEFAULT;
+ try {
+ if (externalDocs != null)
+ return jp.parse(vr.resolve(externalDocs), ExternalDocumentation.class);
+ String externalDocs = context.getMessages().findFirstString(req.getLocale(), method.getName() + ".externalDocs");
+ if (externalDocs != null)
+ return jp.parse(vr.resolve(externalDocs), ExternalDocumentation.class);
+ Operation o = getSwaggerOperationFromFile(req);
+ if (o != null)
+ return o.getExternalDocs();
+ return null;
+ } catch (Exception e) {
+ throw new RestException(SC_INTERNAL_SERVER_ERROR, e);
+ }
+ }
+
+ /**
+ * Returns the Swagger deprecated flag for this Java method.
+ */
+ private boolean isDeprecated() {
+ return deprecated;
+ }
+
+ /**
+ * Returns the localized Swagger parameter information for this Java method.
+ */
+ private List<ParameterInfo> getParameters(RestRequest req) throws ParseException {
+ Operation o = getSwaggerOperationFromFile(req);
+ if (o != null && o.getParameters() != null)
+ return o.getParameters();
+
+ VarResolverSession vr = req.getVarResolverSession();
+ JsonParser jp = JsonParser.DEFAULT;
+ Map<String,ParameterInfo> m = new TreeMap<>();
+
+ // First parse @RestMethod.parameters() annotation.
+ for (org.apache.juneau.rest.annotation.Parameter v : parameters) {
+ String in = vr.resolve(v.in());
+ ParameterInfo p = parameterInfo(in, vr.resolve(v.name()));
+
+ if (! v.description().isEmpty())
+ p.description(vr.resolve(v.description()));
+ if (v.required())
+ p.required(v.required());
+
+ if ("body".equals(in)) {
+ if (! v.schema().isEmpty())
+ p.schema(jp.parse(vr.resolve(v.schema()), SchemaInfo.class));
+ } else {
+ if (v.allowEmptyValue())
+ p.allowEmptyValue(v.allowEmptyValue());
+ if (! v.collectionFormat().isEmpty())
+ p.collectionFormat(vr.resolve(v.collectionFormat()));
+ if (! v._default().isEmpty())
+ p._default(vr.resolve(v._default()));
+ if (! v.format().isEmpty())
+ p.format(vr.resolve(v.format()));
+ if (! v.items().isEmpty())
+ p.items(jp.parse(vr.resolve(v.items()), Items.class));
+ p.type(vr.resolve(v.type()));
+ }
+ m.put(p.getIn() + '.' + p.getName(), p);
+ }
+
+ // Next, look in resource bundle.
+ String prefix = method.getName() + ".req";
+ for (String key : context.getMessages().keySet(prefix)) {
+ if (key.length() > prefix.length()) {
+ String value = vr.resolve(context.getMessages().getString(key));
+ String[] parts = key.substring(prefix.length() + 1).split("\\.");
+ String in = parts[0], name, field;
+ boolean isBody = "body".equals(in);
+ if (parts.length == (isBody ? 2 : 3)) {
+ if ("body".equals(in)) {
+ name = null;
+ field = parts[1];
+ } else {
+ name = parts[1];
+ field = parts[2];
+ }
+ String k2 = in + '.' + name;
+ ParameterInfo p = m.get(k2);
+ if (p == null) {
+ p = parameterInfoStrict(in, name);
+ m.put(k2, p);
+ }
+
+ if (field.equals("description"))
+ p.description(value);
+ else if (field.equals("required"))
+ p.required(Boolean.valueOf(value));
+
+ if ("body".equals(in)) {
+ if (field.equals("schema"))
+ p.schema(jp.parse(value, SchemaInfo.class));
+ } else {
+ if (field.equals("allowEmptyValue"))
+ p.allowEmptyValue(Boolean.valueOf(value));
+ else if (field.equals("collectionFormat"))
+ p.collectionFormat(value);
+ else if (field.equals("default"))
+ p._default(value);
+ else if (field.equals("format"))
+ p.format(value);
+ else if (field.equals("items"))
+ p.items(jp.parse(value, Items.class));
+ else if (field.equals("type"))
+ p.type(value);
+ }
+ } else {
+ System.err.println("Unknown bundle key '"+key+"'");
+ }
+ }
+ }
+
+ // Finally, look for parameters defined on method.
+ for (RestParam mp : this.params) {
+ RestParamType in = mp.getParamType();
+ if (in != RestParamType.OTHER) {
+ String k2 = in.toString() + '.' + (in == RestParamType.BODY ? null : mp.getName());
+ ParameterInfo p = m.get(k2);
+ if (p == null) {
+ p = parameterInfoStrict(in.toString(), mp.getName());
+ m.put(k2, p);
+ }
+ }
+ }
+
+ if (m.isEmpty())
+ return null;
+ return new ArrayList<>(m.values());
+ }
+
+ /**
+ * Returns the localized Swagger response information about this Java method.
+ */
+ private Map<Integer,ResponseInfo> getResponses(RestRequest req) throws ParseException {
+ Operation o = getSwaggerOperationFromFile(req);
+ if (o != null && o.getResponses() != null)
+ return o.getResponses();
+
+ VarResolverSession vr = req.getVarResolverSession();
+ JsonParser jp = JsonParser.DEFAULT;
+ Map<Integer,ResponseInfo> m = new TreeMap<>();
+ Map<String,HeaderInfo> m2 = new TreeMap<>();
+
+ // First parse @RestMethod.parameters() annotation.
+ for (Response r : responses) {
+ int httpCode = r.value();
+ String description = r.description().isEmpty() ? RestUtils.getHttpResponseText(r.value()) : vr.resolve(r.description());
+ ResponseInfo r2 = responseInfo(description);
+
+ if (r.headers().length > 0) {
+ for (org.apache.juneau.rest.annotation.Parameter v : r.headers()) {
+ HeaderInfo h = headerInfoStrict(vr.resolve(v.type()));
+ if (! v.collectionFormat().isEmpty())
+ h.collectionFormat(vr.resolve(v.collectionFormat()));
+ if (! v._default().isEmpty())
+ h._default(vr.resolve(v._default()));
+ if (! v.description().isEmpty())
+ h.description(vr.resolve(v.description()));
+ if (! v.format().isEmpty())
+ h.format(vr.resolve(v.format()));
+ if (! v.items().isEmpty())
+ h.items(jp.parse(vr.resolve(v.items()), Items.class));
+ r2.header(v.name(), h);
+ m2.put(httpCode + '.' + v.name(), h);
+ }
+ }
+ m.put(httpCode, r2);
+ }
+
+ // Next, look in resource bundle.
+ String prefix = method.getName() + ".res";
+ for (String key : context.getMessages().keySet(prefix)) {
+ if (key.length() > prefix.length()) {
+ String value = vr.resolve(context.getMessages().getString(key));
+ String[] parts = key.substring(prefix.length() + 1).split("\\.");
+ int httpCode = Integer.parseInt(parts[0]);
+ ResponseInfo r2 = m.get(httpCode);
+ if (r2 == null) {
+ r2 = responseInfo(null);
+ m.put(httpCode, r2);
+ }
+
+ String name = parts.length > 1 ? parts[1] : "";
+
+ if ("header".equals(name) && parts.length > 3) {
+ String headerName = parts[2];
+ String field = parts[3];
+
+ String k2 = httpCode + '.' + headerName;
+ HeaderInfo h = m2.get(k2);
+ if (h == null) {
+ h = headerInfoStrict("string");
+ m2.put(k2, h);
+ r2.header(name, h);
+ }
+ if (field.equals("collectionFormat"))
+ h.collectionFormat(value);
+ else if (field.equals("default"))
+ h._default(value);
+ else if (field.equals("description"))
+ h.description(value);
+ else if (field.equals("format"))
+ h.format(value);
+ else if (field.equals("items"))
+ h.items(jp.parse(value, Items.class));
+ else if (field.equals("type"))
+ h.type(value);
+
+ } else if ("description".equals(name)) {
+ r2.description(value);
+ } else if ("schema".equals(name)) {
+ r2.schema(jp.parse(value, SchemaInfo.class));
+ } else if ("examples".equals(name)) {
+ r2.examples(jp.parse(value, TreeMap.class));
+ } else {
+ System.err.println("Unknown bundle key '"+key+"'");
+ }
+ }
+ }
+
+ return m.isEmpty() ? null : m;
+ }
+
+ /**
+ * Returns <jk>true</jk> if the specified request object can call this method.
+ */
+ boolean isRequestAllowed(RestRequest req) {
+ for (RestGuard guard : guards) {
+ req.setJavaMethod(method);
+ if (! guard.isRequestAllowed(req))
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Workhorse method.
+ *
+ * @param pathInfo The value of {@link HttpServletRequest#getPathInfo()} (sorta)
+ * @return The HTTP response code.
+ */
+ int invoke(String pathInfo, RestRequest req, RestResponse res) throws RestException {
+
+ String[] patternVals = pathPattern.match(pathInfo);
+ if (patternVals == null)
+ return SC_NOT_FOUND;
+
+ String remainder = null;
+ if (patternVals.length > pathPattern.getVars().length)
+ remainder = patternVals[pathPattern.getVars().length];
+ for (int i = 0; i < pathPattern.getVars().length; i++)
+ req.getPathMatch().put(pathPattern.getVars()[i], patternVals[i]);
+ req.getPathMatch().setRemainder(remainder);
+
+ ObjectMap requestProperties = new ResolvingObjectMap(req.getVarResolverSession()).setInner(properties);
+
+ req.init(method, requestProperties, defaultRequestHeaders, defaultQuery, defaultFormData, defaultCharset,
+ maxInput, serializers, parsers, partParser, beanContext, encoders, widgets, supportedAcceptTypes, supportedContentTypes);
+ res.init(requestProperties, defaultCharset, serializers, partSerializer, encoders);
+
+ // Class-level guards
+ for (RestGuard guard : context.getGuards())
+ if (! guard.guard(req, res))
+ return SC_UNAUTHORIZED;
+
+ // If the method implements matchers, test them.
+ for (RestMatcher m : requiredMatchers)
+ if (! m.matches(req))
+ return SC_PRECONDITION_FAILED;
+ if (optionalMatchers.length > 0) {
+ boolean matches = false;
+ for (RestMatcher m : optionalMatchers)
+ matches |= m.matches(req);
+ if (! matches)
+ return SC_PRECONDITION_FAILED;
+ }
+
+ context.preCall(req, res);
+
+ Object[] args = new Object[params.length];
+ for (int i = 0; i < params.length; i++) {
+ try {
+ args[i] = params[i].resolve(req, res);
+ } catch (RestException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new RestException(SC_BAD_REQUEST,
+ "Invalid data conversion. Could not convert {0} ''{1}'' to type ''{2}'' on method ''{3}.{4}''.",
+ params[i].getParamType().name(), params[i].getName(), params[i].getType(), method.getDeclaringClass().getName(), method.getName()
+ ).initCause(e);
+ }
+ }
+
+ try {
+
+ for (RestGuard guard : guards)
+ if (! guard.guard(req, res))
+ return SC_OK;
+
+ Object output = method.invoke(context.getResource(), args);
+ if (! method.getReturnType().equals(Void.TYPE))
+ if (output != null || ! res.getOutputStreamCalled())
+ res.setOutput(output);
+
+ context.postCall(req, res);
+
+ if (res.hasOutput()) {
+ output = res.getOutput();
+ for (RestConverter converter : converters)
+ output = converter.convert(req, output, beanContext.getClassMetaForObject(output));
+ res.setOutput(output);
+ }
+ } catch (IllegalArgumentException e) {
+ throw new RestException(SC_BAD_REQUEST,
+ "Invalid argument type passed to the following method: ''{0}''.\n\tArgument types: {1}",
+ method.toString(), getReadableClassNames(args)
+ ).initCause(e);
+ } catch (InvocationTargetException e) {
+ Throwable e2 = e.getTargetException(); // Get the throwable thrown from the doX() method.
+ if (e2 instanceof RestException)
+ throw (RestException)e2;
+ if (e2 instanceof ParseException)
+ throw new RestException(SC_BAD_REQUEST, e2);
+ if (e2 instanceof InvalidDataConversionException)
+ throw new RestException(SC_BAD_REQUEST, e2);
+ throw new RestException(SC_INTERNAL_SERVER_ERROR, e2);
+ } catch (RestException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new RestException(SC_INTERNAL_SERVER_ERROR, e);
+ }
+ return SC_OK;
+ }
+
+ @Override /* Object */
+ public String toString() {
+ return "SimpleMethod: name=" + httpMethod + ", path=" + pathPattern.getPatternString();
+ }
+
+ /*
+ * compareTo() method is used to keep SimpleMethods ordered in the RestCallRouter list.
+ * It maintains the order in which matches are made during requests.
+ */
+ @Override /* Comparable */
+ public int compareTo(RestJavaMethod o) {
+ int c;
+
+ c = priority.compareTo(o.priority);
+ if (c != 0)
+ return c;
+
+ c = pathPattern.compareTo(o.pathPattern);
+ if (c != 0)
+ return c;
+
+ c = compare(o.requiredMatchers.length, requiredMatchers.length);
+ if (c != 0)
+ return c;
+
+ c = compare(o.optionalMatchers.length, optionalMatchers.length);
+ if (c != 0)
+ return c;
+
+ c = compare(o.guards.length, guards.length);
+ if (c != 0)
+ return c;
+
+ return 0;
+ }
+
+ @Override /* Object */
+ public boolean equals(Object o) {
+ if (! (o instanceof RestJavaMethod))
+ return false;
+ return (compareTo((RestJavaMethod)o) == 0);
+ }
+
+ @Override /* Object */
+ public int hashCode() {
+ return super.hashCode();
+ }
+}
\ No newline at end of file