From commits-return-4760-archive-asf-public=cust-asf.ponee.io@juneau.apache.org Sat Jan 6 00:07:55 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 56137180647 for ; Sat, 6 Jan 2018 00:07:55 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 45D8F160C28; Fri, 5 Jan 2018 23:07:55 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 3C5D9160C27 for ; Sat, 6 Jan 2018 00:07:54 +0100 (CET) Received: (qmail 86609 invoked by uid 500); 5 Jan 2018 23:07:53 -0000 Mailing-List: contact commits-help@juneau.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@juneau.apache.org Delivered-To: mailing list commits@juneau.apache.org Received: (qmail 86600 invoked by uid 99); 5 Jan 2018 23:07:53 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 05 Jan 2018 23:07:53 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 0A41718064E for ; Fri, 5 Jan 2018 23:07:53 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -11.731 X-Spam-Level: X-Spam-Status: No, score=-11.731 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01, USER_IN_DEF_SPF_WL=-7.5] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id RPKKxdtXlcCz for ; Fri, 5 Jan 2018 23:07:49 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 2AEE55F295 for ; Fri, 5 Jan 2018 23:07:48 +0000 (UTC) Received: (qmail 85688 invoked by uid 99); 5 Jan 2018 23:07:47 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 05 Jan 2018 23:07:47 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 58DE0DFDE6; Fri, 5 Jan 2018 23:07:47 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: jamesbognar@apache.org To: commits@juneau.incubator.apache.org Message-Id: <8d2b8f083dea467297aed3e1ad98ae73@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: juneau git commit: RestContext refactoring. Date: Fri, 5 Jan 2018 23:07:47 +0000 (UTC) Repository: juneau Updated Branches: refs/heads/master 160cf1d8c -> 779be8b4c RestContext refactoring. Project: http://git-wip-us.apache.org/repos/asf/juneau/repo Commit: http://git-wip-us.apache.org/repos/asf/juneau/commit/779be8b4 Tree: http://git-wip-us.apache.org/repos/asf/juneau/tree/779be8b4 Diff: http://git-wip-us.apache.org/repos/asf/juneau/diff/779be8b4 Branch: refs/heads/master Commit: 779be8b4c2ba9b615c7414c4d3e545ec3c9a4a0a Parents: 160cf1d Author: JamesBognar Authored: Fri Jan 5 18:07:43 2018 -0500 Committer: JamesBognar Committed: Fri Jan 5 18:07:43 2018 -0500 ---------------------------------------------------------------------- .../juneau/rest/MessageBundleLocation.java | 36 ++++++++ .../org/apache/juneau/rest/RestContext.java | 94 ++++++++++++-------- .../apache/juneau/rest/RestContextBuilder.java | 76 ++++++++++++++++ .../juneau/rest/annotation/RestResource.java | 18 ++++ 4 files changed, 189 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/juneau/blob/779be8b4/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/MessageBundleLocation.java ---------------------------------------------------------------------- diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/MessageBundleLocation.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/MessageBundleLocation.java new file mode 100644 index 0000000..c10f7ac --- /dev/null +++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/MessageBundleLocation.java @@ -0,0 +1,36 @@ +// *************************************************************************************************************************** +// * 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; + +/** + * Message bundle location. + * + *

+ * Identifies a message bundle by a base class and bundle path. + */ +public class MessageBundleLocation { + + final Class baseClass; + final String bundlePath; + + /** + * Constructor. + * + * @param baseClass The base class that the bundle path is relative to. + * @param bundlePath The bundle path relative to the base class. + */ + public MessageBundleLocation(Class baseClass, String bundlePath) { + this.baseClass = baseClass; + this.bundlePath = bundlePath; + } +} http://git-wip-us.apache.org/repos/asf/juneau/blob/779be8b4/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 46aa142..b270a97 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 @@ -15,7 +15,6 @@ package org.apache.juneau.rest; import static javax.servlet.http.HttpServletResponse.*; import static org.apache.juneau.internal.ClassUtils.*; import static org.apache.juneau.internal.IOUtils.*; -import static org.apache.juneau.internal.ReflectionUtils.*; import static org.apache.juneau.internal.StringUtils.*; import java.io.*; @@ -892,6 +891,52 @@ public final class RestContext extends BeanContext { public static final String REST_staticFiles = PREFIX + "staticFiles.lo"; /** + * Configuration property: Messages. + * + *

    + *
  • Name: "RestContext.messages.lo" + *
  • Data type: List<MessageBundleLocation> + *
  • Default: null + *
  • Session-overridable: false + *
+ * + *

+ * Identifies the location of the resource bundle for this class. + * + *

+ * This annotation is used to provide localized messages for the following methods: + *

    + *
  • {@link RestRequest#getMessage(String, Object...)} + *
  • {@link RestContext#getMessages()} + *
+ * + *

+ * Refer to the {@link MessageBundle} class for a description of the message key formats used in the properties file. + * + *

+ * The value can be a relative path like "nls/Messages", indicating to look for the resource bundle + * "com.foo.sample.nls.Messages" if the resource class is in "com.foo.sample", or it can be an + * absolute path, like "com.foo.sample.nls.Messages" + * + *

Notes:
+ *
    + *
  • Property: {@link RestContext#REST_messages} + *
  • Annotations: + *
      + *
    • {@link RestResource#messages()} + *
    + *
  • Methods: + *
      + *
    • {@link RestContextBuilder#messages(String)}, + *
    • {@link RestContextBuilder#messages(Class,String)} + *
    • {@link RestContextBuilder#messages(MessageBundleLocation)} + *
    + *
  • Mappings are cumulative from parent to child. + *
+ */ + public static final String REST_messages = PREFIX + "messages.lo"; + + /** * Configuration property: Static file response headers. * *
    @@ -1369,6 +1414,7 @@ public final class RestContext extends BeanContext { this.parentContext = builder.parentContext; PropertyStore ps = getPropertyStore().builder().add(builder.properties).build(); + Class resourceClass = resource.getClass(); contextPath = nullIfEmpty(getProperty(REST_contextPath, String.class, null)); allowHeaderParams = getProperty(REST_allowHeaderParams, boolean.class, true); @@ -1434,7 +1480,7 @@ public final class RestContext extends BeanContext { ClasspathResourceFinder rf = getInstanceProperty(REST_classpathResourceFinder, ClasspathResourceFinder.class, ClasspathResourceFinderBasic.class); boolean useClasspathResourceCaching = getProperty(REST_useClasspathResourceCaching, boolean.class, true); - staticResourceManager = new ClasspathResourceManager(resource.getClass(), rf, useClasspathResourceCaching); + staticResourceManager = new ClasspathResourceManager(resourceClass, rf, useClasspathResourceCaching); supportedContentTypes = getListProperty(REST_supportedContentTypes, MediaType.class, serializers.getSupportedMediaTypes()); supportedAcceptTypes = getListProperty(REST_supportedAcceptTypes, MediaType.class, parsers.getSupportedMediaTypes()); @@ -1445,10 +1491,18 @@ public final class RestContext extends BeanContext { s.add(sfm.path); staticFilesPaths = s.toArray(new String[s.size()]); - Builder b = new Builder(builder, ps); - this.msgs = b.messageBundle; + MessageBundleLocation[] mbl = getInstanceArrayProperty(REST_messages, MessageBundleLocation.class, new MessageBundleLocation[0]); + if (mbl.length == 0) + msgs = new MessageBundle(resourceClass, ""); + else { + msgs = new MessageBundle(mbl[0] != null ? mbl[0].baseClass : resourceClass, mbl[0].bundlePath); + for (int i = 1; i < mbl.length; i++) + msgs.addSearchPath(mbl[i] != null ? mbl[i].baseClass : resourceClass, mbl[i].bundlePath); + } + + fullPath = (builder.parentContext == null ? "" : (builder.parentContext.fullPath + '/')) + builder.path; + this.childResources = Collections.synchronizedMap(new LinkedHashMap()); // Not unmodifiable on purpose so that children can be replaced. - this.fullPath = b.fullPath; Map _widgets = new LinkedHashMap<>(); for (Widget w : getInstanceArrayProperty(REST_widgets, Widget.class, new Widget[0], true, resource, ps)) @@ -1706,36 +1760,6 @@ public final class RestContext extends BeanContext { routers.get(httpMethodName).add(cm); } - private static final class Builder { - - MessageBundle messageBundle; - String fullPath; - - Builder(RestContextBuilder rcb, PropertyStore ps) throws Exception { - - Object resource = rcb.resource; - - LinkedHashMap,RestResource> restResourceAnnotationsChildFirst = findAnnotationsMap(RestResource.class, resource.getClass()); - - // Find resource resource bundle location. - for (Map.Entry,RestResource> e : restResourceAnnotationsChildFirst.entrySet()) { - Class c = e.getKey(); - RestResource r = e.getValue(); - if (! r.messages().isEmpty()) { - if (messageBundle == null) - messageBundle = new MessageBundle(c, r.messages()); - else - messageBundle.addSearchPath(c, r.messages()); - } - } - - if (messageBundle == null) - messageBundle = new MessageBundle(resource.getClass(), ""); - - fullPath = (rcb.parentContext == null ? "" : (rcb.parentContext.fullPath + '/')) + rcb.path; - } - } - static final boolean getBoolean(Object o, String systemProperty, boolean def) { if (o == null) o = SystemUtils.getFirstBoolean(def, systemProperty); http://git-wip-us.apache.org/repos/asf/juneau/blob/779be8b4/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java ---------------------------------------------------------------------- diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java index 712a74e..70ec0db 100644 --- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java +++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java @@ -209,6 +209,8 @@ public class RestContextBuilder extends BeanContextBuilder implements ServletCon contextPath(r.contextPath()); for (String mapping : r.staticFiles()) staticFiles(c, vr.resolve(mapping)); + if (! r.messages().isEmpty()) + messages(c, vr.resolve(r.messages())); staticFileResponseHeaders(resolveVars(vr, r.staticFileResponseHeaders())); if (! r.useClasspathResourceCaching().isEmpty()) useClasspathResourceCaching(Boolean.valueOf(vr.resolve(r.useClasspathResourceCaching()))); @@ -1900,6 +1902,80 @@ public class RestContextBuilder extends BeanContextBuilder implements ServletCon } /** + * Configuration property: Messages. + * + *

    + * Identifies the location of the resource bundle for this class. + * + *

    + * This annotation is used to provide localized messages for the following methods: + *

      + *
    • {@link RestRequest#getMessage(String, Object...)} + *
    • {@link RestContext#getMessages()} + *
    + * + *

    + * Refer to the {@link MessageBundle} class for a description of the message key formats used in the properties file. + * + *

    + * The value can be a relative path like "nls/Messages", indicating to look for the resource bundle + * "com.foo.sample.nls.Messages" if the resource class is in "com.foo.sample", or it can be an + * absolute path, like "com.foo.sample.nls.Messages" + * + *

    Notes:
    + *
      + *
    • Property: {@link RestContext#REST_messages} + *
    • Annotations: + *
        + *
      • {@link RestResource#messages()} + *
      + *
    • Methods: + *
        + *
      • {@link RestContextBuilder#messages(String)}, + *
      • {@link RestContextBuilder#messages(Class,String)} + *
      • {@link RestContextBuilder#messages(MessageBundleLocation)} + *
      + *
    • Mappings are cumulative from parent to child. + *
    + * + * @param messageBundleLocation The message bundle location to add to the search path. + * @return This object (for method chaining). + */ + public RestContextBuilder messages(MessageBundleLocation messageBundleLocation) { + return addTo(REST_messages, messageBundleLocation); + } + + /** + * Configuration property: Messages. + * + *

    + * Same as {@link #messages(MessageBundleLocation)} except allows you to pass in the base class and bundle + * path separately. + * + * @param baseClass + * The base class that the bundle path is relative to. + *
    If null, assumed to be the resource class itself. + * @param bundlePath The bundle path relative to the base class. + * @return This object (for method chaining). + */ + public RestContextBuilder messages(Class baseClass, String bundlePath) { + return addTo(REST_messages, new MessageBundleLocation(baseClass, bundlePath)); + } + + /** + * Configuration property: Messages. + * + *

    + * Same as {@link #messages(Class,String)} except assumes the base class is the resource class itself. + * + * @param bundlePath The bundle path relative to the base class. + * @return This object (for method chaining). + */ + public RestContextBuilder messages(String bundlePath) { + return addTo(REST_messages, new MessageBundleLocation(null, bundlePath)); + } + + /** * Configuration property: Static file response headers. * *

    http://git-wip-us.apache.org/repos/asf/juneau/blob/779be8b4/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java index 1f28c10..de0f449 100644 --- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java +++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java @@ -47,6 +47,8 @@ import org.apache.juneau.utils.*; public @interface RestResource { /** + * Messages. + * * Identifies the location of the resource bundle for this class. * *

    @@ -63,6 +65,22 @@ public @interface RestResource { * The value can be a relative path like "nls/Messages", indicating to look for the resource bundle * "com.foo.sample.nls.Messages" if the resource class is in "com.foo.sample", or it can be an * absolute path, like "com.foo.sample.nls.Messages" + * + *

    Notes:
    + *
      + *
    • Property: {@link RestContext#REST_messages} + *
    • Annotations: + *
        + *
      • {@link RestResource#messages()} + *
      + *
    • Methods: + *
        + *
      • {@link RestContextBuilder#messages(String)}, + *
      • {@link RestContextBuilder#messages(Class,String)} + *
      • {@link RestContextBuilder#messages(MessageBundleLocation)} + *
      + *
    • Mappings are cumulative from parent to child. + *
    */ String messages() default "";