W3C home > Mailing lists > Public > www-validator-cvs@w3.org > August 2009

2006/unicorn/src/org/w3c/unicorn/action InitAction.java,NONE,1.1.2.1 IndexAction.java,1.1.2.5,1.1.2.6 ObserveAction.java,1.1.2.4,1.1.2.5

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Wed, 12 Aug 2009 13:15:23 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1MbDfn-0005oJ-EI@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/action
In directory hutz:/tmp/cvs-serv22119/src/org/w3c/unicorn/action

Modified Files:
      Tag: dev2
	IndexAction.java ObserveAction.java 
Added Files:
      Tag: dev2
	InitAction.java 
Log Message:
All initialization actions moved to Framework.java

Index: IndexAction.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/action/Attic/IndexAction.java,v
retrieving revision 1.1.2.5
retrieving revision 1.1.2.6
diff -u -d -r1.1.2.5 -r1.1.2.6
--- IndexAction.java	11 Aug 2009 16:05:42 -0000	1.1.2.5
+++ IndexAction.java	12 Aug 2009 13:15:21 -0000	1.1.2.6
@@ -34,6 +34,7 @@
 	@Override
 	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 		templateList.clear();
+		resp.setContentType("text/html");
 		
 		// Language negotiation
 		String langParameter = req.getParameter("lang");
@@ -57,8 +58,10 @@
 			}
 		}*/
 		
+		
+		
 		// Is response cached ?
-		File cachedIndex = new File(Property.get("PATH_TO_CACHE") + "index/index." + lang + ".html");
+		/*File cachedIndex = new File(Property.get("PATH_TO_CACHE") + "index/index." + lang + ".html");
 		logger.info(cachedIndex.getPath());
 		logger.info("exists ?"+cachedIndex.exists());
 		if (cachedIndex.exists()) {

--- NEW FILE: InitAction.java ---
package org.w3c.unicorn.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.unicorn.Framework;

/**
 * Servlet implementation class InitAction
 */
public class InitAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InitAction() {
        try {
			Framework.init();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (request.getRemoteAddr().equals("0:0:0:0:0:0:0:1") || request.getRemoteAddr().equals("127.0.0.1"))
			try {
				Framework.init();
			} catch (Exception e) {
				response.setContentType("text/plain");
				response.getWriter().print(e.getMessage() + "\n" + e.getStackTrace());
			}
		else
			response.getWriter().print(request.getRemoteAddr());
		response.getWriter().close();
	}



}

Index: ObserveAction.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/action/Attic/ObserveAction.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -d -r1.1.2.4 -r1.1.2.5
--- ObserveAction.java	11 Aug 2009 16:05:42 -0000	1.1.2.4
+++ ObserveAction.java	12 Aug 2009 13:15:21 -0000	1.1.2.5
@@ -1,19 +1,498 @@
+// $Id$
+// Author: Jean-Guilhem Rouel
+// (c) COPYRIGHT MIT, ERCIM and Keio, 2006.
+// Please first read the full copyright statement in file COPYRIGHT.html
 package org.w3c.unicorn.action;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-public class ObserveAction extends Action {
-	private static final long serialVersionUID = 1L;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+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.NoTaskException;
+import org.w3c.unicorn.index.IndexGenerator;
+import org.w3c.unicorn.output.OutputFactory;
+import org.w3c.unicorn.output.OutputFormater;
+import org.w3c.unicorn.output.OutputModule;
+import org.w3c.unicorn.util.LocalizedString;
+import org.w3c.unicorn.util.Property;
+
+/**
+ * FirstServlet<br />
+ * Created: Jun 26, 2006 2:04:11 PM<br />
+ * 
+ * @author Jean-Guilhem ROUEL
+ */
+public class ObserveAction extends HttpServlet {
+
+	private static final Log logger = LogFactory
+			.getLog("org.w3c.unicorn.servlet");
+
+	private static final long serialVersionUID = -1375355420965607571L;
+
+	private static final DiskFileItemFactory factory = new DiskFileItemFactory();
+
+	/**
+	 * Creates a new file upload handler.
+	 */
+	private static final ServletFileUpload upload = new ServletFileUpload(
+			ObserveAction.factory);
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.GenericServlet#init()
+	 */
+	@Override
+	public void init(final ServletConfig aServletConfig)
+			throws ServletException {
+		ObserveAction.logger.trace("init");
+
+		ObserveAction.factory.setRepository(new File(Property
+				.get("UPLOADED_FILES_REPOSITORY")));
+
+		try {
+			IndexGenerator.generateIndexes();
+		} catch (final ResourceNotFoundException e) {
+			ObserveAction.logger.error("ResourceNotFoundException : "
+					+ e.getMessage(), e);
+			e.printStackTrace();
+		} catch (final ParseErrorException e) {
+			ObserveAction.logger.error(
+					"ParseErrorException : " + e.getMessage(), e);
+			e.printStackTrace();
+		} catch (final Exception e) {
+			ObserveAction.logger.error("Exception : " + e.getMessage(), e);
+			e.printStackTrace();
+		}
+		ObserveAction.logger.info("End of initialisation of servlet.");
 
-	public ObserveAction() {
-		// TODO Auto-generated constructor stub
 	}
 
-	//@Override
-	public void execute(HttpServletRequest request, HttpServletResponse response) {
-		// TODO Auto-generated method stub
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
+	 *      javax.servlet.http.HttpServletResponse)
+	 */
+	@Override
+	protected void doGet(final HttpServletRequest aHttpServletRequest,
+			final HttpServletResponse aHttpServletResponse)
+			throws ServletException, IOException {
+		ObserveAction.logger.trace("doGet");
 
+		// 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");
+
+		// Returns the preferred Locale that the client will accept content in,
+		// based on the Accept-Language header
+		final String aLocale = convertEnumerationToString(aHttpServletRequest
+				.getLocales());
+		final UnicornCall aUnicornCall = new UnicornCall();
+
+		// Language of the template
+		// ucn_lang is a parameter which is define in xx_index.html.vm.
+		// It is an hidden parameter of a form.
+		String templateLang = null;
+		if (aHttpServletRequest.getParameterValues(Property
+				.get("UNICORN_PARAMETER_PREFIX")
+				+ "lang") != null) {
+			templateLang = aHttpServletRequest.getParameterValues(Property
+					.get("UNICORN_PARAMETER_PREFIX")
+					+ "lang")[0];
+		} else {
+			templateLang = chooseTemplateLang(aLocale);
+		}
+
+		mapOfOutputParameter.put("lang", templateLang);
+
+		if (null == aLocale) {
+			aUnicornCall.setLang(LocalizedString.DEFAULT_LANGUAGE);
+		} else {
+			aUnicornCall.setLang(templateLang + "," + aLocale);
+		}
+
+		for (final Enumeration<?> aEnumParamName = aHttpServletRequest
+				.getParameterNames(); aEnumParamName.hasMoreElements();) {
+			final String sParamName = (String) aEnumParamName.nextElement();
+			final String[] tStringParamValue = aHttpServletRequest
+					.getParameterValues(sParamName);
+
+			this.addParameter(sParamName, tStringParamValue, aUnicornCall,
+					mapOfSpecificParameter, mapOfOutputParameter);
+		} // For
+
+		if (aUnicornCall.getTask() == null) {
+			ObserveAction.logger.error("No task selected.");
+			this.createError(aHttpServletResponse, new NoTaskException(),
+					mapOfSpecificParameter, mapOfOutputParameter);
+			return;
+		}
+
+		try {
+			aUnicornCall.doTask();
+
+			this.createOutput(aHttpServletResponse, aUnicornCall,
+					mapOfSpecificParameter, mapOfOutputParameter);
+		} catch (final Exception aException) {
+			ObserveAction.logger.error("Exception : " + aException.getMessage(),
+					aException);
+			this.createError(aHttpServletResponse, 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 aHttpServletRequest,
+			final HttpServletResponse aHttpServletResponse)
+			throws ServletException, IOException {
+		ObserveAction.logger.trace("doPost");
+
+		// Check that we have a file upload request
+		final boolean bIsMultipart = ServletFileUpload
+				.isMultipartContent(new ServletRequestContext(
+						aHttpServletRequest));
+
+		if (!bIsMultipart) {
+			this.doGet(aHttpServletRequest, aHttpServletResponse);
+			return;
+		}
+
+		// Parse the request
+		final List<?> listOfItem;
+
+		final UnicornCall aUnicornCall = new UnicornCall();
+
+		// 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");
+
+		// Returns the preferred Locale that the client will accept content in,
+		// based on the Accept-Language header
+		final String aLocale = convertEnumerationToString(aHttpServletRequest
+				.getLocales());
+
+		// Language of the template
+		// ucn_lang is a parameter which is define in xx_index.html.vm.
+		// It is an hidden parameter of a form.
+		String templateLang = null;
+
+		ObserveAction.logger.trace("doPost");
+
+		final Map<String, String[]> mapOfSpecificParameter = new Hashtable<String, String[]>();
+
+		FileItem aFileItemUploaded = null;
+
+		try {
+			listOfItem = ObserveAction.upload.parseRequest(aHttpServletRequest);
+
+			// Process the uploaded items
+			for (final Iterator<?> aIterator = listOfItem.iterator(); aIterator
+					.hasNext();) {
+				final FileItem aFileItem = (FileItem) aIterator.next();
+				if (aFileItem.isFormField()) {
+
+					if (aFileItem.getFieldName().equals("ucn_lang")) {
+						templateLang = aFileItem.getString();
+					}
+
+					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 (templateLang == null) {
+				templateLang = chooseTemplateLang(aLocale);
+			}
+
+			if (null == aLocale) {
+				aUnicornCall.setLang(LocalizedString.DEFAULT_LANGUAGE);
+			} else {
+				aUnicornCall.setLang(templateLang + "," + aLocale);
+			}
+			mapOfOutputParameter.put("lang", templateLang);
+
+		}
+
+		catch (final FileUploadException aFileUploadException) {
+			ObserveAction.logger.error("FileUploadException : "
+					+ aFileUploadException.getMessage(), aFileUploadException);
+			this.createError(aHttpServletResponse, aFileUploadException,
+					mapOfSpecificParameter, mapOfOutputParameter);
+		}
+
+		try {
+			aUnicornCall.doTask();
+
+			this.createOutput(aHttpServletResponse, aUnicornCall,
+					mapOfSpecificParameter, mapOfOutputParameter);
+		} catch (final Exception aException) {
+			ObserveAction.logger.error("Exception : " + aException.getMessage(),
+					aException);
+			this.createError(aHttpServletResponse, aException,
+					mapOfSpecificParameter, mapOfOutputParameter);
+		} finally {
+			if ("true".equals(Property.get("DELETE_UPLOADED_FILES"))
+					&& aFileItemUploaded != null
+					&& aFileItemUploaded instanceof FileItem) {
+				aFileItemUploaded.delete();
+			}
+		}
+	}
+
+	/**
+	 * Adds a parameter at the correct call.
+	 * 
+	 * @param sParamName
+	 *            Name of the parameter.
+	 * @param sParamValue
+	 *            Value of the parameter.
+	 * @param aUnicornCall
+	 * @param mapOfSpecificParameter
+	 * @param mapOfOutputParameter
+	 */
+	private void addParameter(final String sParamName,
+			final String sParamValue, final UnicornCall aUnicornCall,
+			final Map<String, String[]> mapOfSpecificParameter,
+			final Map<String, String> mapOfOutputParameter) {
+		final String[] tStringValues = { sParamValue };
+		this.addParameter(sParamName, tStringValues, aUnicornCall,
+				mapOfSpecificParameter, mapOfOutputParameter);
+	}
+
+	/**
+	 * 
+	 * @param sParamName
+	 * @param tStringParamValue
+	 * @param aUnicornCall
+	 * @param mapOfSpecificParameter
+	 * @param mapOfOutputParameter
+	 */
+	private void addParameter(String sParamName,
+			final String[] tStringParamValue, final UnicornCall aUnicornCall,
+			final Map<String, String[]> mapOfSpecificParameter,
+			final Map<String, String> mapOfOutputParameter) {
+
+		if (null == tStringParamValue || 0 == tStringParamValue.length) {
+			// no value for this parameter
+			// TODO log this info if necessary
+			return;
+		}
+
+		if (!sParamName.startsWith(Property.get("UNICORN_PARAMETER_PREFIX"))) {
+			// task parameter
+			aUnicornCall.addParameter(sParamName, tStringParamValue);
+			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"))) {
+			sParamName = sParamName.substring(Property.get(
+					"UNICORN_PARAMETER_OUTPUT_PREFIX").length());
+			mapOfSpecificParameter.put(sParamName, tStringParamValue);
+			return;
+		}
+
+		if (sParamName.equals("lang")) {
+			aUnicornCall.addParameter(Property.get("UNICORN_PARAMETER_PREFIX")
+					+ "lang", tStringParamValue);
+		}
+
+		// Global Unicorn parameter
+		if (sParamName.equals("task")) {
+			// FirstServlet.logger.debug("");
+			aUnicornCall.setTask(tStringParamValue[0]);
+		} else if (sParamName.equals("uri")) {
+			aUnicornCall.setEnumInputMethod(EnumInputMethod.URI);
+
+			if (!tStringParamValue[0].substring(0, 7).equals("http://")) {
+				ObserveAction.logger.info("URI missing protocol : "
+						+ tStringParamValue[0]);
+				tStringParamValue[0] = "http://" + tStringParamValue[0];
+				ObserveAction.logger.info("URI modified to : "
+						+ tStringParamValue[0]);
+			}
+
+			aUnicornCall.setDocumentName(tStringParamValue[0]);
+			aUnicornCall.setInputParameterValue(tStringParamValue[0]);
+		} else if (sParamName.equals("text")) {
+			aUnicornCall.setEnumInputMethod(EnumInputMethod.DIRECT);
+			aUnicornCall.setDocumentName(tStringParamValue[0]);
+			aUnicornCall.setInputParameterValue(tStringParamValue[0]);
+		}
+		// TODO add upload handle when it work
+		else if (sParamName.equals("output") || sParamName.equals("format")
+				|| sParamName.equals("charset")
+				|| sParamName.equals("mimetype") || sParamName.equals("lang")) {
+			mapOfOutputParameter.put(sParamName, tStringParamValue[0]);
+		} else if (sParamName.equals("text_mime")) {
+			aUnicornCall.addParameter(Property.get("UNICORN_PARAMETER_PREFIX")
+					+ "mime", tStringParamValue);
+		}
+	}
+
+	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
+					.getOutputFormater(mapOfOutputParameter.get("format"),
+							mapOfOutputParameter.get("lang"),
+							mapOfOutputParameter.get("mimetype"));
+			final OutputModule aOutputModule = OutputFactory
+					.getOutputModule(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 createOutput(final HttpServletResponse aHttpServletResponse,
+			final UnicornCall aUnicornCall,
+			final Map<String, String[]> mapOfSpecificParameter,
+			final Map<String, String> mapOfOutputParameter) throws IOException {
+		aHttpServletResponse.setContentType(mapOfOutputParameter
+				.get("mimetype")
+				+ "; charset=" + mapOfOutputParameter.get("charset"));
+		try {
+			Map<String, Object> mapOfStringObject = new LinkedHashMap<String, Object>();
+			mapOfStringObject.put("unicorncall", aUnicornCall);
+
+			final OutputFormater aOutputFormater = OutputFactory
+					.getOutputFormater(mapOfOutputParameter.get("format"),
+							mapOfOutputParameter.get("lang"),
+							mapOfOutputParameter.get("mimetype"));
+			final OutputModule aOutputModule = OutputFactory
+					.getOutputModule(mapOfOutputParameter.get("output"));
+			aOutputModule.produceOutput(aOutputFormater, mapOfStringObject,
+					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>");
+		}
+	}
+
+	/**
+	 * This method returns the first language of the accept language list which
+	 * is equal to one of available index template language
+	 * 
+	 * @param aLocale
+	 * @return The selected language or the default language.
+	 */
+	private String chooseTemplateLang(String aLocale) {
+		String[] tabLang = aLocale.split(";|,");
+		for (int i = 0; i < tabLang.length; i++) {
+			if (Framework.outputLang.contains(tabLang[i])) {
+				return tabLang[i];
+			} else if (Framework.outputLang.contains(tabLang[i].split("-")[0])) {
+				return tabLang[i].split("-")[0];
+			}
+		}
+
+		return LocalizedString.DEFAULT_LANGUAGE;
+	}
+
+	/**
+	 * Converts an Enumeration object to a string, the terms being separated by
+	 * a coma.
+	 * 
+	 * @param myEnum
+	 *            The enumeration to convert.
+	 * @return The converted string.
+	 */
+	private String convertEnumerationToString(Enumeration<?> myEnum) {
+		String ret = "";
+		while (myEnum.hasMoreElements()) {
+			ret += myEnum.nextElement().toString() + ",";
+		}
+		return ret.substring(0, ret.length() - 1);
 	}
 
 }
Received on Wednesday, 12 August 2009 13:15:33 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 15:17:38 UTC