Coverage Report - net.sf.jolene.struts.DomletAction
 
Classes in this File Line Coverage Branch Coverage Complexity
DomletAction
0%
0/69
0%
0/42
0
 
 1  
 package net.sf.jolene.struts;
 2  
 
 3  
 import net.sf.jolene.util.PrefsReader;
 4  
 import net.sf.jolene.dom.Document;
 5  
 import net.sf.jolene.dom.HTMLElement;
 6  
 import net.sf.jolene.dom.Label;
 7  
 import net.sf.jolene.dom.Text;
 8  
 import net.sf.jolene.factories.DocumentFactory;
 9  
 import org.apache.log4j.LogManager;
 10  
 import org.apache.log4j.Logger;
 11  
 import org.apache.struts.action.ActionForm;
 12  
 import org.apache.struts.action.ActionForward;
 13  
 import org.apache.struts.action.ActionMapping;
 14  
 import org.apache.struts.actions.DispatchAction;
 15  
 import org.apache.struts.util.MessageResources;
 16  
 
 17  
 import javax.servlet.http.HttpServletRequest;
 18  
 import javax.servlet.http.HttpServletResponse;
 19  
 import java.io.IOException;
 20  
 
 21  
 /**
 22  
  * Default action servlet for jolene.  Implements a modified DispatchAction which can match request uri string
 23  
  * to the method name of the action class. The primary purpose is to associate a uri to a document object.
 24  
  * There are several ways to do this see the method descriptions.
 25  
  *
 26  
  * @author Dan Howard
 27  
  * @since Sep 23, 2006 5:40:15 PM
 28  
  */
 29  0
 public class DomletAction extends DispatchAction {
 30  
 
 31  0
     private static final Logger log = LogManager.getLogger(DomletAction.class);
 32  
 
 33  
     /**
 34  
      * Performs a basic dispatch of the request looking for the parameter from the struts mapping or by using
 35  
      * the struts action name itself.
 36  
      *
 37  
      * @param mapping  ActionMapping
 38  
      * @param form     ActionForm
 39  
      * @param request  HttpServletRequest
 40  
      * @param response HttpServletResponse
 41  
      * @return ActionForward
 42  
      * @throws Exception
 43  
      */
 44  
     @Override
 45  
     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 46  
 
 47  0
         log.info("DomletAction Initializing PrefsReader...");
 48  0
         PrefsReader.init();
 49  
 
 50  
         // Identify the request parameter containing the method name
 51  0
         log.debug("QUERY_STRING :" + request.getQueryString());
 52  
 
 53  
         // Identify the method name to be dispatched to.
 54  
         // Either it's the parameter or the path name itself minus .do etc.
 55  0
         String parameter = mapping.getParameter();
 56  
         String name;
 57  0
         if (parameter != null) {
 58  0
             name = request.getParameter(parameter);
 59  
         } else {
 60  0
             String path = mapping.getPath();
 61  0
             name = path;
 62  0
             if (path.lastIndexOf("/") != -1) {
 63  0
                 name = path.substring(path.lastIndexOf("/") + 1);
 64  
             }
 65  
         }
 66  
 
 67  
         // Invoke the named method, and return the result
 68  0
         return dispatchMethod(mapping, form, request, response, name);
 69  
 
 70  
     }
 71  
 
 72  
 
 73  
     /**
 74  
      * Retrives a document object based on the mapping input attribute and attaches it to the proper scope.
 75  
      * The reason for this method is to allow a default document to be associated with an action since
 76  
      * actions can have many forwards.
 77  
      * <p/>
 78  
      * For example, calling this method would get the document from the <code>/domlet/login.html<code> uri.
 79  
      * <p/>
 80  
      * <pre>
 81  
      * &lt;action
 82  
      * path="/login"
 83  
      * type="domlet.kbsample.web.actions.LoginAction"
 84  
      * name="LoginForm"
 85  
      * scope="session"
 86  
      * validate="false"
 87  
      * input="/domlet/login.html"&gt;
 88  
      * &nbsp;
 89  
      * &lt;forward name="success" path="/browse.do"/&gt;
 90  
      * &lt;forward name="fail" path="/domlet/error.html"/&gt;
 91  
      * &lt;forward name="login" path="/domlet/login.html"/&gt;
 92  
      * &lt;/action&gt;
 93  
      * </pre>
 94  
      *
 95  
      * @param mapping ActionMapping
 96  
      * @param request HttpServletRequest
 97  
      * @return Document or null if the uri for input mapping is undefined in struts-config.xml
 98  
      * @throws IOException if an IOException occurs
 99  
      */
 100  
     public Document getDocument(ActionMapping mapping, HttpServletRequest request) throws IOException {
 101  0
         String uri = mapping.getInput();
 102  0
         log.debug("mapping URI: " + uri);
 103  0
         if (uri == null) {
 104  0
             log.error("NO URI Specified in struts-config.xml for " + mapping.getPath());
 105  0
             return null;
 106  
         }
 107  0
         return getDocument(request, uri, mapping);
 108  
     }
 109  
 
 110  
     /**
 111  
      * Retrives a document object based on the specified forward.
 112  
      * <p/>
 113  
      * For example, using the following in struts-config.xml:
 114  
      * <p/>
 115  
      * <pre>
 116  
      * &lt;action
 117  
      * path="/login"
 118  
      * type="domlet.kbsample.web.actions.LoginAction"
 119  
      * name="LoginForm"
 120  
      * scope="session"
 121  
      * validate="false"
 122  
      * input="/domlet/login.html"&gt;
 123  
      * &nbsp;
 124  
      * &lt;forward name="success" path="/browse.do"/&gt;
 125  
      * &lt;forward name="fail" path="/domlet/error.html"/&gt;
 126  
      * &lt;forward name="login" path="/domlet/login.html"/&gt;
 127  
      * &lt;/action&gt;
 128  
      * </pre>
 129  
      * <p/>
 130  
      * <code>getDocument(mapping, "fail", request); </code> Returns document from /domlet/error.html <br>
 131  
      * <code>getDocument(mapping, "login", request);</code> Returns document from /domlet/login.html
 132  
      *
 133  
      * @param mapping ActionMapping
 134  
      * @param forward forward string
 135  
      * @param request HttpServletRequest
 136  
      * @return Document or null if the forward cannot be found in struts-config.xml
 137  
      * @throws IOException if an IOException occurs
 138  
      */
 139  
     public Document getDocument(ActionMapping mapping, String forward, HttpServletRequest request) throws IOException {
 140  
 
 141  0
         String uri = mapping.findForward(forward).getPath();
 142  0
         log.debug("Mapping URI from forward: " + forward + ": " + uri);
 143  0
         return getDocument(request, uri, mapping);
 144  
 
 145  
     }
 146  
 
 147  
     private Document getDocument(HttpServletRequest request, String uri, ActionMapping mapping) throws IOException {
 148  
         String file;
 149  0
         file = request.getSession().getServletContext().getRealPath(uri);
 150  
 
 151  
         Document document;
 152  0
         if ("request".equalsIgnoreCase(mapping.getScope())) {
 153  0
             if (request.getAttribute(uri) == null) {
 154  0
                 document = DocumentFactory.getInstance().getDocument(file, request.getContextPath(), uri);
 155  
             } else {
 156  0
                 document = (Document) request.getAttribute(uri);
 157  
             }
 158  0
             request.setAttribute(uri, document);
 159  
         } else {
 160  0
             if (request.getSession().getAttribute(uri) == null) {
 161  0
                 document = DocumentFactory.getInstance().getDocument(file, request.getContextPath(), uri);
 162  
             } else {
 163  0
                 document = (Document) request.getSession().getAttribute(uri);
 164  
             }
 165  0
             request.getSession().setAttribute(uri, document);
 166  
         }
 167  
 
 168  0
         translate(document, request);
 169  0
         return document;
 170  
     }
 171  
 
 172  
     /**
 173  
      * Translates labels texts and input type button/submit
 174  
      *
 175  
      * @param document
 176  
      * @param request
 177  
      */
 178  
     private void translate(Document document, HttpServletRequest request) {
 179  0
         if (document.isTranslated()) {
 180  0
             return;
 181  
         }
 182  
 
 183  0
         MessageResources messages = getResources(request);
 184  
 
 185  
         // Get the URI /somfolder/somehtml.html to convert to somefolder.somehtml as a start key for the resources
 186  0
         String docKey = document.getUri();
 187  0
         if (docKey.startsWith("/")) {
 188  0
             docKey = docKey.substring(1);
 189  
         }
 190  0
         int n = docKey.indexOf(".");
 191  0
         if (n > -1) {
 192  0
             docKey = docKey.substring(0, n);
 193  
         }
 194  0
         docKey = docKey.replace("/", ".");
 195  0
         docKey = docKey.replace("\\", ".");
 196  0
         docKey = docKey.toLowerCase();
 197  
         //---------------------------------
 198  0
         log.debug("docKey " + docKey);
 199  
 
 200  
         HTMLElement element;
 201  
         String formKey;
 202  0
         for (int j = 0; j < document.getFormCount(); j++) {
 203  0
             formKey = document.forms(j).getName();
 204  0
             if (formKey.trim().length() == 0) {
 205  0
                 formKey = "form" + (j + 1);
 206  
             }
 207  
 
 208  
             String elementName;
 209  0
             for (int k = 0; k < document.forms(j).getElementCount(); k++) {
 210  0
                 element = document.forms(j).elements(k);
 211  0
                 elementName = docKey + "." + formKey + "." + element.getName();
 212  0
                 log.debug("Element name " + elementName);
 213  
 
 214  
                 // Check for labels.
 215  0
                 if (element instanceof Label && !(element instanceof Text)) {
 216  
 
 217  0
                     if (messages.isPresent(elementName)) {
 218  0
                         element.setValue(messages.getMessage(elementName));
 219  
                     }
 220  
                 } else {
 221  
                     // Check for buttons - buttons display the value attribute
 222  0
                     if (element.hasAttribute("type") && (element.getAttribute("type").equalsIgnoreCase("button") || element.getAttribute("type").equalsIgnoreCase("submit"))) {
 223  
 
 224  0
                         if (messages.isPresent(elementName)) {
 225  0
                             log.debug("setting element " + elementName);
 226  0
                             element.setValue(messages.getMessage(elementName));
 227  
                         }
 228  
                     }
 229  
                 }
 230  
 
 231  
                 // check for title attribute
 232  0
                 if (element.hasAttribute("title")) {
 233  0
                     log.debug("Element name title: " + elementName + ".title");
 234  0
                     if (messages.isPresent(elementName + ".title")) {
 235  0
                         log.debug("setting title " + elementName);
 236  0
                         element.setAttribute("title", messages.getMessage(elementName + ".title"));
 237  
                     }
 238  
                 }
 239  
             }
 240  
         }
 241  0
         document.setTranslated(true);
 242  
 
 243  0
     }
 244  
 
 245  
 
 246  
 }