src/java/de/uapcore/lightpit/Functions.java

changeset 11
737ab27e37b3
parent 10
89e3e6e28b69
child 13
f4608ad6c947
equal deleted inserted replaced
10:89e3e6e28b69 11:737ab27e37b3
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 * 27 *
28 */ 28 */
29 package de.uapcore.lightpit; 29 package de.uapcore.lightpit;
30 30
31 import java.util.Optional;
32 import javax.servlet.http.HttpServletRequest;
31 import org.slf4j.Logger; 33 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory; 34 import org.slf4j.LoggerFactory;
33 35
34 /** 36 /**
35 * Contains common static functions. 37 * Contains common static functions.
40 42
41 public static String jspPath(String filename) { 43 public static String jspPath(String filename) {
42 return Constants.JSP_PATH_PREFIX + filename; 44 return Constants.JSP_PATH_PREFIX + filename;
43 } 45 }
44 46
45 public static String fullyQualifiedName(String base, String name) { 47 public static String fqn(String base, String name) {
46 return base+"."+name; 48 return base+"."+name;
47 } 49 }
48 50
49 public static String fullyQualifiedName(Class clazz, String name) { 51 public static String fqn(Class clazz, String name) {
50 return fullyQualifiedName(clazz.getName(), name); 52 return fqn(clazz.getName(), name);
53 }
54
55 public static String fullPath(LightPITModule module, RequestMapping mapping) {
56 StringBuilder sb = new StringBuilder();
57 sb.append(module.modulePath());
58 sb.append('/');
59 if (!mapping.requestPath().isEmpty()) {
60 sb.append(mapping.requestPath().isEmpty());
61 sb.append('/');
62 }
63 return sb.toString();
64 }
65
66 public static String fullPath(HttpServletRequest req) {
67 return req.getServletPath() + Optional.ofNullable(req.getPathInfo()).orElse("");
51 } 68 }
52 69
53 /** 70 /**
54 * Returns the module path of the given LightPIT Servlet module. 71 * Returns the module path of the given LightPIT Servlet module.
55 * 72 *
58 * 75 *
59 * @param clazz the servlet class 76 * @param clazz the servlet class
60 * @return the module path 77 * @return the module path
61 */ 78 */
62 public static String modulePathOf(Class<? extends AbstractLightPITServlet> clazz) { 79 public static String modulePathOf(Class<? extends AbstractLightPITServlet> clazz) {
63 LightPITModule moduleInfo = clazz.getAnnotation(LightPITModule.class); 80 Optional<LightPITModule> moduleInfo = Optional.ofNullable(clazz.getAnnotation(LightPITModule.class));
64 if (moduleInfo == null) { 81 if (moduleInfo.isPresent()) {
82 return moduleInfo.get().modulePath();
83 } else {
65 LOG.warn( 84 LOG.warn(
66 "{} is a LightPIT Servlet but is missing the module annotation.", 85 "{} is a LightPIT Servlet but is missing the module annotation.",
67 clazz.getName() 86 clazz.getName()
68 ); 87 );
69 return "/error/404.html"; 88 return "/error/404.html";
70 } else {
71 return moduleInfo.modulePath();
72 } 89 }
73 } 90 }
74 91
75 /** 92 /**
76 * This class is not instantiatable. 93 * This class is not instantiatable.

mercurial