PageRenderTime 22ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/restpathactions/java/src/org/jpublish/module/restpathactions/RestPathActionsModule.java

http://jpublish.googlecode.com/
Java | 205 lines | 148 code | 37 blank | 20 comment | 22 complexity | a1749db613355337ad71ef3191472e01 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. * Copyright (c) 2009. Florin T.PATRASCU
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jpublish.module.restpathactions;
  17. import com.anthonyeden.lib.config.Configuration;
  18. import com.anthonyeden.lib.config.ConfigurationException;
  19. import com.anthonyeden.lib.config.XMLConfiguration;
  20. import com.atlassian.util.profiling.UtilTimerStack;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.jpublish.JPublishContext;
  24. import org.jpublish.JPublishModule;
  25. import org.jpublish.SiteContext;
  26. import org.jpublish.action.Action;
  27. import org.jpublish.action.ActionWrapper;
  28. import org.jpublish.action.PathAction;
  29. import java.io.File;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. /**
  35. * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
  36. * @since $Revision$ (created: 2010-11-27)
  37. */
  38. public class RestPathActionsModule implements JPublishModule {
  39. protected static final Log log = LogFactory.getLog(RestPathActionsModule.class);
  40. public static final String RESPONSE = "rpa_response";
  41. private static final String CALLBACK = "callback";
  42. private static final String FORMAT = "format";
  43. private static final String NAME = "rpa";
  44. private static final String DESCRIPTION = "REST-like Path Actions support";
  45. private List<String> infoDetails = new ArrayList<String>();
  46. private String modulePath;
  47. private String formatParameter = FORMAT;
  48. private String callbackParameter = CALLBACK;
  49. private Map<String, Action> actions = new HashMap<String, Action>(1);
  50. private SiteContext site;
  51. private List<RestPathActionModel> restModels = new ArrayList<RestPathActionModel>();
  52. private String defaultRepository;
  53. private boolean debug;
  54. private boolean profiling;
  55. public void init(SiteContext site, Configuration configuration) throws Exception {
  56. this.site = site;
  57. infoDetails.add("*** Module: RestLikePathActions (RPA) initializing ...");
  58. if (configuration != null) {
  59. load(configuration);
  60. } else {
  61. log.warn(" No configuration specified; module disabled.");
  62. }
  63. }
  64. public Map getDefinedActions() {
  65. return new HashMap();
  66. }
  67. public void destroy() {
  68. }
  69. public String getName() {
  70. return NAME;
  71. }
  72. public String getInfo() {
  73. return DESCRIPTION;
  74. }
  75. public List<String> getInfoDetails() {
  76. return infoDetails;
  77. }
  78. public void load(Configuration configuration) throws ConfigurationException {
  79. modulePath = configuration.getChildValue("path", NAME);
  80. defaultRepository = configuration.getChildValue("repository", "content");
  81. List<Configuration> routesNodesOrConfigFiles = configuration.getChildren("routes");
  82. for (Configuration routesNodesOrConfigFile : routesNodesOrConfigFiles) {
  83. List<Configuration> routes = null;
  84. String routesConfig = routesNodesOrConfigFile.getAttribute("config");
  85. if (routesConfig != null) {
  86. File configFile = new File(site.getRoot(), routesConfig);
  87. Configuration routesConfiguration = new XMLConfiguration("RPAConfiguration", configFile);
  88. infoDetails.add(" Loading routes from: " + configFile.getAbsolutePath());
  89. List<Configuration> rc = routesConfiguration.getChildren();
  90. if (rc != null) {
  91. routes = rc;
  92. } else {
  93. infoDetails.add(" no routes defined.");
  94. }
  95. } else {
  96. routes = routesNodesOrConfigFile.getChildren();
  97. }
  98. if (routes != null && !routes.isEmpty()) {
  99. for (Configuration routeConfiguration : routes) {
  100. RestPathActionModel restModel = new RestPathActionModel();
  101. restModel.setPath(routeConfiguration.getAttribute("path", ""));
  102. restModel.setAction(routeConfiguration.getAttribute("action"));
  103. restModel.setPage(routeConfiguration.getAttribute("page"));
  104. restModel.setMethods(routeConfiguration.getAttribute("method", "GET"));
  105. restModel.setContentType(routeConfiguration.getAttribute("content-type"));
  106. restModel.setConfiguration(routeConfiguration);
  107. restModels.add(restModel);
  108. infoDetails.add(" route: " + restModel);
  109. }
  110. }
  111. }
  112. try {
  113. formatParameter = configuration.getChild(FORMAT).getAttribute("parameter", FORMAT);
  114. callbackParameter = configuration.getChild(CALLBACK).getAttribute("parameter", CALLBACK);
  115. debug = configuration.getChild("debug").getValue("false").equalsIgnoreCase("true");
  116. profiling = configuration.getChild("profiling").getValue("false").equalsIgnoreCase("true");
  117. } catch (Exception e) {
  118. //will use the default
  119. }
  120. infoDetails.add(" format parameter .......: " + formatParameter);
  121. infoDetails.add(" jsonp callback parameter: " + callbackParameter);
  122. infoDetails.add(" module profiling .......: " + profiling);
  123. infoDetails.add(" debug ..................: " + debug);
  124. final PathAction restPathAction = new PathAction(modulePath, new RestPathAction(modulePath, this));
  125. site.getActionManager().getPathActions().add(new ActionWrapper(restPathAction, null));
  126. actions.put(NAME, restPathAction);
  127. infoDetails.add("RPA fully loaded. Good luck!");
  128. infoDetails.add("****************************");
  129. printInfoDetails(infoDetails);
  130. UtilTimerStack.setActive(this.isProfiling());
  131. }
  132. private void printInfoDetails(List<String> infoDetails) {
  133. if (infoDetails != null && !infoDetails.isEmpty()) {
  134. for (String info : infoDetails) {
  135. log.info(info);
  136. }
  137. }
  138. }
  139. public List<RestPathActionModel> getRestModels() {
  140. return restModels;
  141. }
  142. public String getModulePath() {
  143. return modulePath;
  144. }
  145. public void execute(String action, JPublishContext context, Configuration configuration) throws Exception {
  146. if (action != null && context != null) {
  147. site.getActionManager().execute(action, context, configuration);
  148. }
  149. }
  150. public SiteContext getSite() {
  151. return site;
  152. }
  153. public String getDefaultRepository() {
  154. return defaultRepository;
  155. }
  156. public String getFormatParameter() {
  157. return formatParameter;
  158. }
  159. public String getCallbackParameter() {
  160. return callbackParameter;
  161. }
  162. public boolean isDebug() {
  163. return debug;
  164. }
  165. public boolean isProfiling() {
  166. return profiling;
  167. }
  168. }