Changeset 286:001ecebe946a

Show
Ignore:
Timestamp:
09/04/09 17:59:43 (4 years ago)
Author:
tgambet
Branch:
default
convert_revision:
svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@287
Message:

refactoring in progress

Location:
src/org/w3c/unicorn/action
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • src/org/w3c/unicorn/action/Action.java

    r214 r286  
    11package org.w3c.unicorn.action; 
    22 
     3import java.io.IOException; 
     4import java.net.URLConnection; 
     5import java.util.ArrayList; 
     6 
     7import javax.servlet.ServletException; 
    38import javax.servlet.http.HttpServlet; 
     9import javax.servlet.http.HttpServletRequest; 
     10import javax.servlet.http.HttpServletResponse; 
     11 
     12import org.w3c.unicorn.Framework; 
     13import org.w3c.unicorn.util.Language; 
     14import org.w3c.unicorn.util.Message; 
     15import org.w3c.unicorn.util.Property; 
    416 
    517public abstract class Action extends HttpServlet { 
    618         
    719        private static final long serialVersionUID = 1L; 
     20         
     21        @Override 
     22        protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
     23                        throws ServletException, IOException { 
     24                 
     25                if (!Framework.isUcnInitialized) { 
     26                        resp.sendError(500, "Unicorn is not initialized properly. Check logs."); 
     27                        return; 
     28                } 
     29                 
     30                ArrayList<Message> messages = new ArrayList<Message>(); 
     31                 
     32                String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang"); 
     33                if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter)) 
     34                        langParameter = Language.negociate(req.getLocales()); 
     35                 
     36                if (!langParameter.equals(req.getLocale().getLanguage())) 
     37                        messages.add(new Message(Message.Level.INFO, "$message_unavailable_language (" + req.getLocale().getDisplayLanguage(req.getLocale()) + "). $message_translation", null)); 
     38                 
     39                if (!Language.isComplete(langParameter)) 
     40                        messages.add(new Message(Message.Level.INFO, "$message_incomplete_language. $message_translation", null)); 
     41                 
     42                 
     43                 
     44                 
     45        } 
     46 
     47        protected String getQueryStringWithout(String parameterName, HttpServletRequest req) { 
     48                String query = req.getQueryString(); 
     49                String queryString; 
     50                if (query == null) { 
     51                        queryString = "./?"; 
     52                } else { 
     53                        queryString = "?"; 
     54                        queryString += query.replaceAll("&?" + parameterName + "=[^&]*", ""); 
     55                        if (!queryString.equals("?")) 
     56                                queryString += "&"; 
     57                } 
     58                return queryString; 
     59        } 
     60 
     61        @Override 
     62        protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
     63                        throws ServletException, IOException { 
     64                // TODO Auto-generated method stub 
     65                super.doPost(req, resp); 
     66        } 
     67 
     68        @Override 
     69        public void init() throws ServletException { 
     70                // TODO Auto-generated method stub 
     71                super.init(); 
     72        } 
     73         
     74         
    875 
    976} 
  • src/org/w3c/unicorn/action/IndexAction.java

    r265 r286  
     1// $Id: IndexAction.java,v 1.11 2009-09-04 17:59:43 tgambet Exp $Id $ 
     2// Author: Thomas Gambet 
     3// (c) COPYRIGHT MIT, ERCIM and Keio, 2009. 
     4// Please first read the full copyright statement in file COPYRIGHT.html 
    15package org.w3c.unicorn.action; 
    26 
     
    812import javax.servlet.http.HttpServletResponse; 
    913 
     14import org.apache.commons.logging.Log; 
     15import org.apache.commons.logging.LogFactory; 
    1016import org.apache.velocity.VelocityContext; 
    1117import org.w3c.unicorn.Framework; 
     
    1723public class IndexAction extends Action { 
    1824 
    19         private static final long serialVersionUID = 1L; 
     25        private static final long serialVersionUID = 599055553694915687L; 
     26         
     27        private static Log logger = LogFactory.getLog(IndexAction.class); 
     28         
    2029        private VelocityContext velocityContext; 
    2130         
    2231        @Override 
    2332        public void init() throws ServletException { 
    24                 super.init(); 
     33                //logger.trace("Init IndexAction"); 
     34                //super.init(); 
    2535        } 
    2636         
     
    4151                if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter)) { 
    4252                        langParameter = Language.negociate(req.getLocales()); 
    43                         if (!langParameter.equals(req.getLocale().getLanguage())) { 
    44                                 messages.add(new Message(Message.Level.INFO, "$message_unavailable_language (" + req.getLocale().getDisplayLanguage(req.getLocale()) + "). $message_translation", null)); 
    45                         } else { 
    46                                 String requested_parameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang"); 
    47                                 if (requested_parameter != null && !Framework.getLanguageProperties().containsKey(requested_parameter))  
    48                                         messages.add(new Message(Message.Level.INFO, "$message_unavailable_requested_language. $message_translation", null)); 
    49                         } 
     53                } 
     54                 
     55                if (!langParameter.equals(req.getLocale().getLanguage())) { 
     56                        messages.add(new Message(Message.Level.INFO, "$message_unavailable_language (" + req.getLocale().getDisplayLanguage(req.getLocale()) + "). $message_translation", null)); 
    5057                } 
    5158                 
     
    5562                velocityContext = new VelocityContext(Language.getContext(langParameter)); 
    5663                 
    57                 String query = req.getQueryString(); 
     64                velocityContext.put("queryString", getQueryStringWithout(Property.get("UNICORN_PARAMETER_PREFIX") + "lang", req)); 
     65                 
     66                /*String query = req.getQueryString(); 
    5867                String queryString; 
    5968                if (query == null) { 
     
    7281                if (!queryString.equals("?")) 
    7382                        queryString += "&";*/ 
    74                 velocityContext.put("queryString", queryString); 
     83                //velocityContext.put("queryString", queryString); 
    7584                 
    7685                /*messages.add(new Message(Message.Level.WARNING, "un warning", null)); 
  • src/org/w3c/unicorn/action/ObserveAction.java

    r265 r286  
    1 // $Id: ObserveAction.java,v 1.11 2009-09-03 14:04:12 tgambet Exp $ 
     1// $Id: ObserveAction.java,v 1.12 2009-09-04 17:59:43 tgambet Exp $ 
    22// Author: Jean-Guilhem Rouel 
    33// (c) COPYRIGHT MIT, ERCIM and Keio, 2006. 
     
    3333import org.w3c.unicorn.UnicornCall; 
    3434import org.w3c.unicorn.contract.EnumInputMethod; 
     35import org.w3c.unicorn.exceptions.UnsupportedMimeTypeException; 
    3536import org.w3c.unicorn.output.OutputFactory; 
    3637import org.w3c.unicorn.output.OutputFormater; 
     
    4647 * @author Jean-Guilhem ROUEL 
    4748 */ 
    48 public class ObserveAction extends HttpServlet { 
    49  
    50         private static final Log logger = LogFactory.getLog(ObserveAction.class); 
     49public class ObserveAction extends Action { 
    5150 
    5251        private static final long serialVersionUID = -1375355420965607571L; 
    53  
    54         private static final DiskFileItemFactory factory = new DiskFileItemFactory(); 
     52         
     53        private static Log logger = LogFactory.getLog(ObserveAction.class); 
     54 
     55        private static DiskFileItemFactory factory; 
    5556 
    5657        /** 
    5758         * Creates a new file upload handler. 
    5859         */ 
    59         private static final ServletFileUpload upload = new ServletFileUpload( 
    60                         ObserveAction.factory); 
     60        private static ServletFileUpload upload; 
    6161 
    6262        /* 
     
    6666         */ 
    6767        @Override 
    68         public void init(final ServletConfig aServletConfig) 
    69                         throws ServletException { 
    70                 ObserveAction.logger.trace("init"); 
    71  
    72                 ObserveAction.factory.setRepository(new File(Property 
    73                                 .get("UPLOADED_FILES_REPOSITORY"))); 
    74  
     68        public void init(final ServletConfig aServletConfig) throws ServletException { 
     69                logger.trace("Init ObserverAction"); 
     70                super.init(); 
     71                 
     72                factory = new DiskFileItemFactory(); 
     73                factory.setRepository(new File(Property.get("UPLOADED_FILES_REPOSITORY"))); 
     74                upload = new ServletFileUpload(factory); 
     75                logger.debug("Created a ServletFileUpload with repository set to: " + Property.get("UPLOADED_FILES_REPOSITORY")); 
    7576        } 
    7677 
     
    166167                        this.createOutput(resp, aUnicornCall, 
    167168                                        mapOfSpecificParameter, mapOfOutputParameter, mapOfStringObject); 
     169                } catch (final UnsupportedMimeTypeException aException) { 
     170                        if (mapOfOutputParameter.get("mimetype").equals("text/html")) { 
     171                                Message mess = new Message(Message.Level.ERROR, "$message_unsupported_mime_type", null); 
     172                                req.setAttribute("unicorn_message", mess); 
     173                                (new IndexAction()).doGet(req, resp); 
     174                                 
     175                        } 
    168176                } catch (final Exception aException) { 
    169177                        ObserveAction.logger.error("Exception : " + aException.getMessage(), 
     
    291299                        this.createOutput(resp, aUnicornCall, 
    292300                                        mapOfSpecificParameter, mapOfOutputParameter, mapOfStringObject); 
    293                 } catch (final IOException aException) { 
    294                  
     301                } catch (final UnsupportedMimeTypeException aException) { 
     302                        if (mapOfOutputParameter.get("mimetype").equals("text/html")) { 
     303                                Message mess = new Message(Message.Level.ERROR, "$message_unsupported_mime_type", null); 
     304                                req.setAttribute("unicorn_message", mess); 
     305                                (new IndexAction()).doGet(req, resp); 
     306                                 
     307                        } 
    295308                } catch (final Exception aException) { 
    296309                        ObserveAction.logger.error("Exception : " + aException.getMessage(),