juneau-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamesbog...@apache.org
Subject juneau git commit: RestContext refactoring.
Date Fri, 05 Jan 2018 23:07:47 GMT
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 <jamesbognar@apache.org>
Authored: Fri Jan 5 18:07:43 2018 -0500
Committer: JamesBognar <jamesbognar@apache.org>
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.
+ * 
+ * <p>
+ * 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";
 	
 	/**
+	 * <b>Configuration property:</b>  Messages. 
+	 *
+	 * <ul>
+	 * 	<li><b>Name:</b> <js>"RestContext.messages.lo"</js>
+	 * 	<li><b>Data type:</b> <code>List&lt;MessageBundleLocation&gt;</code>
+	 * 	<li><b>Default:</b> <jk>null</jk>
+	 * 	<li><b>Session-overridable:</b> <jk>false</jk>
+	 * </ul>
+	 * 
+	 * <p>
+	 * Identifies the location of the resource bundle for this class.
+	 *
+	 * <p>
+	 * This annotation is used to provide localized messages for the following methods:
+	 * <ul>
+	 * 	<li>{@link RestRequest#getMessage(String, Object...)}
+	 * 	<li>{@link RestContext#getMessages()}
+	 * </ul>
+	 *
+	 * <p>
+	 * Refer to the {@link MessageBundle} class for a description of the message key formats
used in the properties file.
+	 *
+	 * <p>
+	 * The value can be a relative path like <js>"nls/Messages"</js>, indicating
to look for the resource bundle
+	 * <js>"com.foo.sample.nls.Messages"</js> if the resource class is in <js>"com.foo.sample"</js>,
or it can be an
+	 * absolute path, like <js>"com.foo.sample.nls.Messages"</js>
+	 * 
+	 * <h6 class='topic'>Notes:</h6>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property:  {@link RestContext#REST_messages}
+	 * 	<li>Annotations: 
+	 * 		<ul>
+	 * 			<li>{@link RestResource#messages()} 
+	 * 		</ul>
+	 * 	<li>Methods: 
+	 * 		<ul>
+	 * 			<li>{@link RestContextBuilder#messages(String)},
+	 * 			<li>{@link RestContextBuilder#messages(Class,String)}
+	 * 			<li>{@link RestContextBuilder#messages(MessageBundleLocation)} 
+	 * 		</ul>
+	 * 	<li>Mappings are cumulative from parent to child.  
+	 * </ul>
+	 */
+	public static final String REST_messages = PREFIX + "messages.lo";
+	
+	/**
 	 * <b>Configuration property:</b>  Static file response headers. 
 	 *
 	 * <ul>
@@ -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<String,RestContext>());
 // Not unmodifiable on purpose so that children can be replaced.
-			this.fullPath = b.fullPath;
 			
 			Map<String,Widget> _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<Class<?>,RestResource> restResourceAnnotationsChildFirst = findAnnotationsMap(RestResource.class,
resource.getClass());
-			
-			// Find resource resource bundle location.
-			for (Map.Entry<Class<?>,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
 	}
 
 	/**
+	 * <b>Configuration property:</b>  Messages. 
+	 *
+	 * <p>
+	 * Identifies the location of the resource bundle for this class.
+	 *
+	 * <p>
+	 * This annotation is used to provide localized messages for the following methods:
+	 * <ul>
+	 * 	<li>{@link RestRequest#getMessage(String, Object...)}
+	 * 	<li>{@link RestContext#getMessages()}
+	 * </ul>
+	 *
+	 * <p>
+	 * Refer to the {@link MessageBundle} class for a description of the message key formats
used in the properties file.
+	 *
+	 * <p>
+	 * The value can be a relative path like <js>"nls/Messages"</js>, indicating
to look for the resource bundle
+	 * <js>"com.foo.sample.nls.Messages"</js> if the resource class is in <js>"com.foo.sample"</js>,
or it can be an
+	 * absolute path, like <js>"com.foo.sample.nls.Messages"</js>
+	 * 
+	 * <h6 class='topic'>Notes:</h6>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property:  {@link RestContext#REST_messages}
+	 * 	<li>Annotations: 
+	 * 		<ul>
+	 * 			<li>{@link RestResource#messages()} 
+	 * 		</ul>
+	 * 	<li>Methods: 
+	 * 		<ul>
+	 * 			<li>{@link RestContextBuilder#messages(String)},
+	 * 			<li>{@link RestContextBuilder#messages(Class,String)}
+	 * 			<li>{@link RestContextBuilder#messages(MessageBundleLocation)} 
+	 * 		</ul>
+	 * 	<li>Mappings are cumulative from parent to child.  
+	 * </ul>
+	 *
+	 * @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);
+	}
+
+	/**
+	 * <b>Configuration property:</b>  Messages. 
+	 *
+	 * <p>
+	 * 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.
+	 * 	<br>If <jk>null</jk>, 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));
+	}
+	
+	/**
+	 * <b>Configuration property:</b>  Messages. 
+	 *
+	 * <p>
+	 * 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));
+	}
+
+	/**
 	 * <b>Configuration property:</b>  Static file response headers. 
 	 *
 	 * <p>

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.
 	 *
 	 * <p>
@@ -63,6 +65,22 @@ public @interface RestResource {
 	 * The value can be a relative path like <js>"nls/Messages"</js>, indicating
to look for the resource bundle
 	 * <js>"com.foo.sample.nls.Messages"</js> if the resource class is in <js>"com.foo.sample"</js>,
or it can be an
 	 * absolute path, like <js>"com.foo.sample.nls.Messages"</js>
+	 * 
+	 * <h6 class='topic'>Notes:</h6>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property:  {@link RestContext#REST_messages}
+	 * 	<li>Annotations: 
+	 * 		<ul>
+	 * 			<li>{@link RestResource#messages()} 
+	 * 		</ul>
+	 * 	<li>Methods: 
+	 * 		<ul>
+	 * 			<li>{@link RestContextBuilder#messages(String)},
+	 * 			<li>{@link RestContextBuilder#messages(Class,String)}
+	 * 			<li>{@link RestContextBuilder#messages(MessageBundleLocation)} 
+	 * 		</ul>
+	 * 	<li>Mappings are cumulative from parent to child.  
+	 * </ul>
 	 */
 	String messages() default "";
 


Mime
View raw message