- From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
- Date: Mon, 07 Sep 2009 16:33:51 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/action In directory hutz:/tmp/cvs-serv9160/src/org/w3c/unicorn/action Modified Files: IndexAction.java Action.java ObserveAction.java Log Message: done some refactoring Index: IndexAction.java =================================================================== RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/action/IndexAction.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- IndexAction.java 4 Sep 2009 17:59:43 -0000 1.11 +++ IndexAction.java 7 Sep 2009 16:33:49 -0000 1.12 @@ -29,58 +29,25 @@ private VelocityContext velocityContext; @Override - public void init() throws ServletException { - //logger.trace("Init IndexAction"); - //super.init(); - } - - @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - if (!Framework.isUcnInitialized) { - resp.sendError(500, "Unicorn is not initialized properly. Check logs."); - return; - } + super.doGet(req, resp); resp.setContentType("text/html; charset=UTF-8"); ArrayList<Message> messages = new ArrayList<Message>(); + String paramPrefix = Property.get("UNICORN_PARAMETER_PREFIX"); + String lang = getLanguage(req, messages); + String task = getTask(req, lang, null); + String queryString = getQueryStringWithout(paramPrefix + "lang", req); - // Language negotiation - String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang"); - if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter)) { - langParameter = Language.negociate(req.getLocales()); - } - - if (!langParameter.equals(req.getLocale().getLanguage())) { - messages.add(new Message(Message.Level.INFO, "$message_unavailable_language (" + req.getLocale().getDisplayLanguage(req.getLocale()) + "). $message_translation", null)); - } - - if (!Language.isComplete(langParameter)) - messages.add(new Message(Message.Level.INFO, "$message_incomplete_language. $message_translation", null)); - - velocityContext = new VelocityContext(Language.getContext(langParameter)); - - velocityContext.put("queryString", getQueryStringWithout(Property.get("UNICORN_PARAMETER_PREFIX") + "lang", req)); - - /*String query = req.getQueryString(); - String queryString; - if (query == null) { - queryString = "./?"; - } else { - queryString = "?"; - queryString += query.replaceAll("&?ucn_lang=[^&]*", ""); - if (!queryString.equals("?")) - queryString += "&"; - } + if (req.getAttribute("unicorn_message") != null) + messages.add((Message) req.getAttribute("unicorn_message")); - /*String query = req.getQueryString(); - String queryString = "?"; - if (query != null) - queryString += query.replaceAll("&?ucn_lang=[^&]*", ""); - if (!queryString.equals("?")) - queryString += "&";*/ - //velocityContext.put("queryString", queryString); + velocityContext = new VelocityContext(Language.getContext(lang)); + velocityContext.put("queryString", queryString); + velocityContext.put("messages", messages); + velocityContext.put("current_task", Framework.mapOfTask.get(task)); /*messages.add(new Message(Message.Level.WARNING, "un warning", null)); messages.add(new Message(Message.Level.ERROR, "une error", null)); @@ -89,29 +56,7 @@ messages.add(new Message(Message.Level.ERROR, "une error avec long message", "le long message\nle long message\nle long message\nle long message\nle long message\nle long message\n")); messages.add(new Message(Message.Level.INFO, "une info avec long message", "le long message\nle long message\nle long message\nle long message\nle long message\nle long message\nle long message\n"));*/ - - - if (req.getAttribute("unicorn_message") != null) - messages.add((Message) req.getAttribute("unicorn_message")); - - velocityContext.put("messages", messages); - - String taskParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "task"); - if (taskParameter == null || !Framework.mapOfTask.containsKey(taskParameter)) - taskParameter = Framework.mapOfTask.getDefaultTaskId(); - - velocityContext.put("current_task", Framework.mapOfTask.get(taskParameter)); - - - if (req.getHeader("X-Requested-With") != null && req.getHeader("X-Requested-With").equals("XMLHttpRequest")) { - //for JavaScript testing purposes - /*long s = System.currentTimeMillis(); - long t; - do { - t = System.currentTimeMillis(); - } while ((t - s) < 3000);*/ - // ----------------- Templates.write("parameters.vm", velocityContext, resp.getWriter()); resp.getWriter().close(); } else { Index: ObserveAction.java =================================================================== RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/action/ObserveAction.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- ObserveAction.java 4 Sep 2009 17:59:43 -0000 1.12 +++ ObserveAction.java 7 Sep 2009 16:33:49 -0000 1.13 @@ -16,7 +16,6 @@ import javax.servlet.ServletConfig; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -27,16 +26,12 @@ import org.apache.commons.fileupload.servlet.ServletRequestContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.velocity.exception.ParseErrorException; -import org.apache.velocity.exception.ResourceNotFoundException; -import org.w3c.unicorn.Framework; import org.w3c.unicorn.UnicornCall; import org.w3c.unicorn.contract.EnumInputMethod; import org.w3c.unicorn.exceptions.UnsupportedMimeTypeException; import org.w3c.unicorn.output.OutputFactory; import org.w3c.unicorn.output.OutputFormater; import org.w3c.unicorn.output.OutputModule; -import org.w3c.unicorn.util.Language; import org.w3c.unicorn.util.Message; import org.w3c.unicorn.util.Property; @@ -47,274 +42,109 @@ * @author Jean-Guilhem ROUEL */ public class ObserveAction extends Action { - + private static final long serialVersionUID = -1375355420965607571L; private static Log logger = LogFactory.getLog(ObserveAction.class); - + private static DiskFileItemFactory factory; - - /** - * Creates a new file upload handler. - */ + private static ServletFileUpload upload; - - /* - * (non-Javadoc) - * - * @see javax.servlet.GenericServlet#init() - */ + @Override public void init(final ServletConfig aServletConfig) throws ServletException { logger.trace("Init ObserverAction"); - super.init(); - factory = new DiskFileItemFactory(); factory.setRepository(new File(Property.get("UPLOADED_FILES_REPOSITORY"))); upload = new ServletFileUpload(factory); logger.debug("Created a ServletFileUpload with repository set to: " + Property.get("UPLOADED_FILES_REPOSITORY")); } - - /* - * (non-Javadoc) - * - * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, - * javax.servlet.http.HttpServletResponse) - */ + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - if (!Framework.isUcnInitialized) { - resp.sendError(500, "Unicorn is not initialized properly. Check logs."); - return; - } + super.doGet(req, resp); Map<String, Object> mapOfStringObject = new LinkedHashMap<String, Object>(); + Map<String, String[]> mapOfSpecificParameter = new Hashtable<String, String[]>(); + Map<String, String> mapOfOutputParameter = new Hashtable<String, String>(); + ArrayList<Message> messages = new ArrayList<Message>(); - ObserveAction.logger.trace("doGet"); - - // Language negotiation - String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang"); - if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter)) - langParameter = Language.negociate(req.getLocales()); - - logger.debug("Lang Parameter: " + langParameter); - - // Add query string - + String paramPrefix = Property.get("UNICORN_PARAMETER_PREFIX"); + String lang = getLanguage(req, null); + String task = getTask(req, lang, messages); + String queryString = getQueryStringWithout(paramPrefix + "lang", req); - String query = req.getQueryString(); - String queryString; - if (query == null) { - queryString = "./?"; - } else { - queryString = "?"; - queryString += query.replaceAll("&?ucn_lang=[^&]*", ""); - if (!queryString.equals("?")) - queryString += "&"; - } mapOfStringObject.put("queryString", queryString); + mapOfStringObject.put("messages", messages); - - // Variables related to the output - final Map<String, String[]> mapOfSpecificParameter = new Hashtable<String, String[]>(); - final Map<String, String> mapOfOutputParameter = new Hashtable<String, String>(); mapOfOutputParameter.put("output", "simple"); mapOfOutputParameter.put("format", "xhtml10"); mapOfOutputParameter.put("charset", "UTF-8"); mapOfOutputParameter.put("mimetype", "text/html"); - mapOfOutputParameter.put("lang", langParameter); - - final UnicornCall aUnicornCall = new UnicornCall(); - final String aLocale = convertEnumerationToString(req.getLocales()); - if (null == aLocale) { - aUnicornCall.setLang(langParameter + "," + Property.get("DEFAULT_LANGUAGE")); - } else { - aUnicornCall.setLang(langParameter + "," + aLocale); - } - - - for (final Enumeration<?> aEnumParamName = req - .getParameterNames(); aEnumParamName.hasMoreElements();) { - final String sParamName = (String) aEnumParamName.nextElement(); - final String[] tStringParamValue = req - .getParameterValues(sParamName); - - this.addParameter(sParamName, tStringParamValue, aUnicornCall, - mapOfSpecificParameter, mapOfOutputParameter); - } - - if (aUnicornCall.getTask() == null) { - aUnicornCall.setTask(Framework.mapOfTask.getDefaultTaskId()); - Message mess = new Message(Message.Level.WARNING, "$message_no_task " + Framework.mapOfTask.get(Framework.mapOfTask.getDefaultTaskId()).getLongName(langParameter), null); - ArrayList<Message> messages = new ArrayList<Message>(); - messages.add(mess); - mapOfStringObject.put("messages", messages); - } - - /*if (aUnicornCall.getTask() == null) { - ObserveAction.logger.error("No task selected."); - this.createError(resp, new NoTaskException(), - mapOfSpecificParameter, mapOfOutputParameter); - return; - }*/ - - - try { - aUnicornCall.doTask(); - - this.createOutput(resp, aUnicornCall, - mapOfSpecificParameter, mapOfOutputParameter, mapOfStringObject); - } catch (final UnsupportedMimeTypeException aException) { - if (mapOfOutputParameter.get("mimetype").equals("text/html")) { - Message mess = new Message(Message.Level.ERROR, "$message_unsupported_mime_type", null); - req.setAttribute("unicorn_message", mess); - (new IndexAction()).doGet(req, resp); - - } - } catch (final Exception aException) { - ObserveAction.logger.error("Exception : " + aException.getMessage(), - aException); - - if (mapOfOutputParameter.get("mimetype").equals("text/html")) { - String errorContent = ""; - errorContent += aException.getMessage() + "\n"; - for (StackTraceElement stackTraceElement : aException.getStackTrace()) { - errorContent += stackTraceElement.toString() + "\n"; - } - Message mess = new Message(Message.Level.ERROR, "$stack_trace_text", errorContent); - req.setAttribute("unicorn_message", mess); - (new IndexAction()).doGet(req, resp); - - } else { - this.createError(resp, aException, mapOfSpecificParameter, mapOfOutputParameter); - } - - } - } - - /* - * (non-Javadoc) - * - * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, - * javax.servlet.http.HttpServletResponse) - */ - @Override - protected void doPost(final HttpServletRequest req, - final HttpServletResponse resp) - throws ServletException, IOException { - ObserveAction.logger.trace("doPost"); + mapOfOutputParameter.put("lang", lang); - Map<String, Object> mapOfStringObject = new LinkedHashMap<String, Object>(); + UnicornCall aUnicornCall = new UnicornCall(); + String aLocale = convertEnumerationToString(req.getLocales()); + if (null == aLocale) + aUnicornCall.setLang(lang + "," + Property.get("DEFAULT_LANGUAGE")); + else + aUnicornCall.setLang(lang + "," + aLocale); - // Check that we have a file upload request - final boolean bIsMultipart = ServletFileUpload.isMultipartContent(new ServletRequestContext(req)); - - if (!bIsMultipart) { - this.doGet(req, resp); - return; + for (Object param : req.getParameterMap().keySet()) { + String sParamName = (String) param; + String[] tStringParamValue = req.getParameterValues(sParamName); + addParameter(sParamName, tStringParamValue, aUnicornCall, mapOfSpecificParameter, mapOfOutputParameter); } - // Language negotiation - String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang"); - if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter)) - langParameter = Language.negociate(req.getLocales()); - - //String query = req.getQueryString(); - String queryString = "./?"; - //if (query != null) - // queryString += query.replaceAll("&?ucn_lang=[^&]*", ""); - //if (!queryString.equals("?")) - // queryString += "&"; - mapOfStringObject.put("queryString", queryString); - - - // Variables related to the output - final Map<String, String> mapOfOutputParameter = new Hashtable<String, String>(); - mapOfOutputParameter.put("output", "simple"); - mapOfOutputParameter.put("format", "xhtml10"); - mapOfOutputParameter.put("charset", "UTF-8"); - mapOfOutputParameter.put("mimetype", "text/html"); - mapOfOutputParameter.put("lang", langParameter); - - final UnicornCall aUnicornCall = new UnicornCall(); - final String aLocale = convertEnumerationToString(req.getLocales()); - if (null == aLocale) { - aUnicornCall.setLang(langParameter + "," + Property.get("DEFAULT_LANGUAGE")); - } else { - aUnicornCall.setLang(langParameter + "," + aLocale); - } + addParameter(paramPrefix+ "task", task, aUnicornCall, mapOfSpecificParameter, mapOfOutputParameter); - // Parse the request - final List<?> listOfItem; - - ObserveAction.logger.trace("doPost"); - - final Map<String, String[]> mapOfSpecificParameter = new Hashtable<String, String[]>(); - + // POST operations FileItem aFileItemUploaded = null; - - try { - listOfItem = ObserveAction.upload.parseRequest(req); - - // Process the uploaded items - for (final Iterator<?> aIterator = listOfItem.iterator(); aIterator - .hasNext();) { - final FileItem aFileItem = (FileItem) aIterator.next(); - if (aFileItem.isFormField()) { - - addParameter(aFileItem.getFieldName(), aFileItem - .getString(), aUnicornCall, mapOfSpecificParameter, - mapOfOutputParameter); - - } else if (aFileItem.getFieldName().equals( - Property.get("UNICORN_PARAMETER_PREFIX") + "file")) { - aFileItemUploaded = aFileItem; - aUnicornCall.setDocumentName(aFileItemUploaded.getName()); - aUnicornCall.setInputParameterValue(aFileItemUploaded); - aUnicornCall.setEnumInputMethod(EnumInputMethod.UPLOAD); + if (req.getMethod().equals("POST") && ServletFileUpload.isMultipartContent(new ServletRequestContext(req))) { + messages.clear(); + try { + List<?> listOfItem = upload.parseRequest(req); + // Process the uploaded items + for (Iterator<?> aIterator = listOfItem.iterator(); aIterator.hasNext();) { + FileItem aFileItem = (FileItem) aIterator.next(); + logger.error("TOM: " + aFileItem); + if (aFileItem.isFormField()) { + addParameter(aFileItem.getFieldName(), aFileItem.getString(), + aUnicornCall, mapOfSpecificParameter, mapOfOutputParameter); + } else if (aFileItem.getFieldName().equals(paramPrefix + "file")) { + aFileItemUploaded = aFileItem; + aUnicornCall.setDocumentName(aFileItemUploaded.getName()); + aUnicornCall.setInputParameterValue(aFileItemUploaded); + aUnicornCall.setEnumInputMethod(EnumInputMethod.UPLOAD); + break; + } } + } catch (final FileUploadException aFileUploadException) { + logger.error("FileUploadException : " + aFileUploadException.getMessage(), aFileUploadException); + //TODO + Message mess = new Message(); + createError(req, resp, mess,mapOfSpecificParameter, mapOfOutputParameter); } } - - catch (final FileUploadException aFileUploadException) { - ObserveAction.logger.error("FileUploadException : " - + aFileUploadException.getMessage(), aFileUploadException); - this.createError(resp, aFileUploadException, - mapOfSpecificParameter, mapOfOutputParameter); - } - - if (aUnicornCall.getTask() == null) { - aUnicornCall.setTask(Framework.mapOfTask.getDefaultTaskId()); - Message mess = new Message(Message.Level.WARNING, "$message_no_task " + Framework.mapOfTask.get(Framework.mapOfTask.getDefaultTaskId()).getLongName(langParameter), null); - ArrayList<Message> messages = new ArrayList<Message>(); - messages.add(mess); - mapOfStringObject.put("messages", messages); - } try { aUnicornCall.doTask(); - - this.createOutput(resp, aUnicornCall, - mapOfSpecificParameter, mapOfOutputParameter, mapOfStringObject); + createOutput(req, resp, mapOfStringObject, aUnicornCall, mapOfSpecificParameter, mapOfOutputParameter); } catch (final UnsupportedMimeTypeException aException) { - if (mapOfOutputParameter.get("mimetype").equals("text/html")) { - Message mess = new Message(Message.Level.ERROR, "$message_unsupported_mime_type", null); - req.setAttribute("unicorn_message", mess); - (new IndexAction()).doGet(req, resp); - - } + Message mess = new Message(Message.Level.ERROR, "$message_unsupported_mime_type", null); + createError(req, resp, mess, mapOfSpecificParameter, mapOfOutputParameter); } catch (final Exception aException) { - ObserveAction.logger.error("Exception : " + aException.getMessage(), - aException); + logger.error("Exception : " + aException.getMessage(), aException); - if (mapOfOutputParameter.get("format").equals("xhtml10")) { - (new IndexAction()).doGet(req, resp); - } else { - this.createError(resp, aException, mapOfSpecificParameter, mapOfOutputParameter); + String errorContent = ""; + errorContent += aException.getMessage() + "\n"; + for (StackTraceElement stackTraceElement : aException.getStackTrace()) { + errorContent += stackTraceElement.toString() + "\n"; } - + Message mess = new Message(Message.Level.ERROR, "$stack_trace_text", errorContent); + createError(req, resp, mess, mapOfSpecificParameter, mapOfOutputParameter); } finally { if ("true".equals(Property.get("DELETE_UPLOADED_FILES")) && aFileItemUploaded != null @@ -324,6 +154,12 @@ } } + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doGet(req, resp); + } + /** * Adds a parameter at the correct call. * @@ -369,11 +205,6 @@ return; } - // Unicorn parameter - // TODO: Why is it here? - sParamName = sParamName.substring(Property.get( - "UNICORN_PARAMETER_PREFIX").length()); - // Output specific parameter if (sParamName.startsWith(Property .get("UNICORN_PARAMETER_OUTPUT_PREFIX"))) { @@ -382,6 +213,11 @@ mapOfSpecificParameter.put(sParamName, tStringParamValue); return; } + + // Unicorn parameter + // TODO: Why is it here? + sParamName = sParamName.substring(Property.get( + "UNICORN_PARAMETER_PREFIX").length()); if (sParamName.equals("lang")) { aUnicornCall.addParameter(Property.get("UNICORN_PARAMETER_PREFIX") @@ -394,7 +230,6 @@ aUnicornCall.setTask(tStringParamValue[0]); } else if (sParamName.equals("uri")) { aUnicornCall.setEnumInputMethod(EnumInputMethod.URI); - // TODO First check that tStringParamValue[0] is at least 7 characters long if (tStringParamValue[0].length() < 7 || !tStringParamValue[0].substring(0, 7).equals("http://")) { ObserveAction.logger.info("URI missing protocol : " + tStringParamValue[0]); @@ -403,8 +238,6 @@ + tStringParamValue[0]); } - - aUnicornCall.setDocumentName(tStringParamValue[0]); aUnicornCall.setInputParameterValue(tStringParamValue[0]); } else if (sParamName.equals("text")) { @@ -422,88 +255,46 @@ } } - private void createError(final HttpServletResponse aHttpServletResponse, - final Exception aExceptionError, - final Map<String, String[]> mapOfSpecificParameter, - final Map<String, String> mapOfOutputParameter) throws IOException { - aHttpServletResponse.setContentType(mapOfOutputParameter - .get("mimetype") - + "; charset=" + mapOfOutputParameter.get("charset")); - - try { - final OutputFormater aOutputFormater = OutputFactory - .createOutputFormater(mapOfOutputParameter.get("format"), - mapOfOutputParameter.get("lang"), - mapOfOutputParameter.get("mimetype")); - final OutputModule aOutputModule = OutputFactory - .createOutputModule(mapOfOutputParameter.get("output")); - aOutputModule.produceError(aOutputFormater, aExceptionError, - mapOfSpecificParameter, aHttpServletResponse.getWriter()); - } catch (final ResourceNotFoundException e) { - ObserveAction.logger.error("ResourceNotFoundException : " - + e.getMessage(), e); - aHttpServletResponse.getWriter().println("<pre>"); - e.printStackTrace(aHttpServletResponse.getWriter()); - aHttpServletResponse.getWriter().println("</pre>"); - } catch (final ParseErrorException e) { - ObserveAction.logger.error( - "ParseErrorException : " + e.getMessage(), e); - aHttpServletResponse.getWriter().println("<pre>"); - e.printStackTrace(aHttpServletResponse.getWriter()); - aHttpServletResponse.getWriter().println("</pre>"); - } catch (final Exception e) { - ObserveAction.logger.error("Exception : " + e.getMessage(), e); - aHttpServletResponse.getWriter().println("<pre>"); - e.printStackTrace(aHttpServletResponse.getWriter()); - aHttpServletResponse.getWriter().println("</pre>"); + private void createError(HttpServletRequest req, HttpServletResponse resp, + Message mess, Map<String, String[]> mapOfSpecificParameter, + Map<String, String> mapOfOutputParameter) throws IOException, ServletException { + + // If text/html is the mime-type the error will be displayed directly on index + if (mapOfOutputParameter.get("mimetype").equals("text/html")) { + req.setAttribute("unicorn_message", mess); + (new IndexAction()).doGet(req, resp); + return; } + + resp.setContentType(mapOfOutputParameter.get("mimetype") + "; charset=" + mapOfOutputParameter.get("charset")); + OutputFormater aOutputFormater = OutputFactory + .createOutputFormater(mapOfOutputParameter.get("format"), + mapOfOutputParameter.get("lang"), + mapOfOutputParameter.get("mimetype")); + OutputModule aOutputModule = OutputFactory.createOutputModule(mapOfOutputParameter.get("output")); + aOutputModule.produceError(aOutputFormater, mess, mapOfSpecificParameter, resp.getWriter()); } - private void createOutput(final HttpServletResponse aHttpServletResponse, - final UnicornCall aUnicornCall, - final Map<String, String[]> mapOfSpecificParameter, - final Map<String, String> mapOfOutputParameter, - Map<String, Object> mapOfStringObject) throws IOException { - aHttpServletResponse.setContentType(mapOfOutputParameter - .get("mimetype") - + "; charset=" + mapOfOutputParameter.get("charset")); - try { - //Map<String, Object> mapOfStringObject = new LinkedHashMap<String, Object>(); - mapOfStringObject.put("unicorncall", aUnicornCall); + private void createOutput(HttpServletRequest req, HttpServletResponse resp, + Map<String, Object> mapOfStringObject, UnicornCall aUnicornCall, + Map<String, String[]> mapOfSpecificParameter, Map<String, String> mapOfOutputParameter) throws IOException { + + resp.setContentType(mapOfOutputParameter.get("mimetype") + "; charset=" + mapOfOutputParameter.get("charset")); - logger.debug("Request output formater with parameters: " - + mapOfOutputParameter.get("format") + " " - + mapOfOutputParameter.get("lang") + " " - + mapOfOutputParameter.get("mimetype")); - - final OutputFormater aOutputFormater = OutputFactory - .createOutputFormater(mapOfOutputParameter.get("format"), - mapOfOutputParameter.get("lang"), - mapOfOutputParameter.get("mimetype")); - final OutputModule aOutputModule = OutputFactory - .createOutputModule(mapOfOutputParameter.get("output")); - aOutputModule.produceOutput(aOutputFormater, mapOfStringObject, - mapOfSpecificParameter, aHttpServletResponse.getWriter()); - } + mapOfStringObject.put("unicorncall", aUnicornCall); - catch (final ResourceNotFoundException e) { - ObserveAction.logger.error("ResourceNotFoundException : " - + e.getMessage(), e); - aHttpServletResponse.getWriter().println("<pre>"); - e.printStackTrace(aHttpServletResponse.getWriter()); - aHttpServletResponse.getWriter().println("</pre>"); - } catch (final ParseErrorException e) { - ObserveAction.logger.error( - "ParseErrorException : " + e.getMessage(), e); - aHttpServletResponse.getWriter().println("<pre>"); - e.printStackTrace(aHttpServletResponse.getWriter()); - aHttpServletResponse.getWriter().println("</pre>"); - } catch (final Exception e) { - ObserveAction.logger.error("Exception : " + e.getMessage(), e); - aHttpServletResponse.getWriter().println("<pre>"); - e.printStackTrace(aHttpServletResponse.getWriter()); - aHttpServletResponse.getWriter().println("</pre>"); - } + logger.debug("Request output formater with parameters: " + + mapOfOutputParameter.get("format") + " " + + mapOfOutputParameter.get("lang") + " " + + mapOfOutputParameter.get("mimetype")); + + OutputFormater aOutputFormater = OutputFactory + .createOutputFormater(mapOfOutputParameter.get("format"), + mapOfOutputParameter.get("lang"), + mapOfOutputParameter.get("mimetype")); + OutputModule aOutputModule = OutputFactory + .createOutputModule(mapOfOutputParameter.get("output")); + aOutputModule.produceOutput(aOutputFormater, mapOfStringObject, mapOfSpecificParameter, resp.getWriter()); } /** Index: Action.java =================================================================== RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/action/Action.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Action.java 4 Sep 2009 17:59:43 -0000 1.3 +++ Action.java 7 Sep 2009 16:33:49 -0000 1.4 @@ -1,7 +1,6 @@ package org.w3c.unicorn.action; import java.io.IOException; -import java.net.URLConnection; import java.util.ArrayList; import javax.servlet.ServletException; @@ -9,6 +8,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.w3c.unicorn.Framework; import org.w3c.unicorn.util.Language; import org.w3c.unicorn.util.Message; @@ -16,7 +17,9 @@ public abstract class Action extends HttpServlet { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = -7503310240481494239L; + + private static Log logger = LogFactory.getLog(Action.class); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) @@ -26,22 +29,12 @@ resp.sendError(500, "Unicorn is not initialized properly. Check logs."); return; } - - ArrayList<Message> messages = new ArrayList<Message>(); - - String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang"); - if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter)) - langParameter = Language.negociate(req.getLocales()); - - if (!langParameter.equals(req.getLocale().getLanguage())) - messages.add(new Message(Message.Level.INFO, "$message_unavailable_language (" + req.getLocale().getDisplayLanguage(req.getLocale()) + "). $message_translation", null)); - - if (!Language.isComplete(langParameter)) - messages.add(new Message(Message.Level.INFO, "$message_incomplete_language. $message_translation", null)); - - - - + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doGet(req, resp); } protected String getQueryStringWithout(String parameterName, HttpServletRequest req) { @@ -57,20 +50,49 @@ } return queryString; } - - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - // TODO Auto-generated method stub - super.doPost(req, resp); + + public String getLanguage(HttpServletRequest req, ArrayList<Message> messages) { + + String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang"); + String lang; + if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter)) + lang = Language.negociate(req.getLocales()); + else + lang = langParameter; + + if (messages == null) + return lang; + + if (!Language.isComplete(lang)) + messages.add(new Message(Message.Level.INFO, "$message_incomplete_language. $message_translation", null)); + else if (!Framework.getLanguageProperties().containsKey(req.getLocale().getLanguage()) && Property.get("SHOW_LANGUAGE_UNAVAILABLE_MESSAGE") == "true") + messages.add(new Message(Message.Level.INFO, "$message_unavailable_language (" + req.getLocale().getDisplayLanguage(req.getLocale()) + "). $message_translation", null)); + + return lang; } - @Override - public void init() throws ServletException { - // TODO Auto-generated method stub - super.init(); + public String getTask(HttpServletRequest req, String lang, ArrayList<Message> messages) { + + String taskParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "task"); + String task; + if (taskParameter == null || !Framework.mapOfTask.containsKey(taskParameter)) + task = Framework.mapOfTask.getDefaultTaskId(); + else + task = taskParameter; + + if (messages == null) + return task; + + if (taskParameter == null) { + Message mess = new Message(Message.Level.WARNING, "$message_no_task " + Framework.mapOfTask.get(Framework.mapOfTask.getDefaultTaskId()).getLongName(lang), null); + messages.add(mess); + } else if (!Framework.mapOfTask.containsKey(taskParameter)) { + System.out.println(taskParameter); + Message mess = new Message(Message.Level.WARNING, "$message_unknown_task " + Framework.mapOfTask.get(Framework.mapOfTask.getDefaultTaskId()).getLongName(lang), null); + messages.add(mess); + } + + return task; } - - }
Received on Monday, 7 September 2009 16:34:01 UTC