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

2006/unicorn/src/org/w3c/unicorn/input URIInputParameter.java,NONE,1.1 DirectInputParameter.java,NONE,1.1 InputParameter.java,NONE,1.1 UploadInputParameter.java,NONE,1.1

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Thu, 17 Sep 2009 15:39:12 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1MoJ4i-0004SJ-EC@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/input
In directory hutz:/tmp/cvs-serv17097/src/org/w3c/unicorn/input

Added Files:
	URIInputParameter.java DirectInputParameter.java 
	InputParameter.java UploadInputParameter.java 
Log Message:
new classes representing the input parameter object of a request (uri, input, upload). each class implements, among others, the methods check(), getMimeType() and getDocumentName()

--- NEW FILE: UploadInputParameter.java ---
package org.w3c.unicorn.input;

import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;

import org.apache.commons.fileupload.FileItem;
import org.w3c.unicorn.contract.EnumInputMethod;

import org.w3c.unicorn.exceptions.UnicornException;

public class UploadInputParameter extends InputParameter {
	
	private FileItem file;
	
	public UploadInputParameter(FileItem file) {
		this.file = file;
	}
	
	@Override
	public void check() throws UnicornException {
		if (file.getName() == null || file.getName().equals("")) {
			//throw new NoDocumentException("No document provided");
			throw new UnicornException("");
		}
		if (file.getSize() == 0) {
			//throw new EmptyDocumentException("Empty document provided");
			throw new UnicornException("");
		}
		String sMimeType = file.getContentType();
		if (null == sMimeType || "".equals(sMimeType)) {
			//throw new NoMimeTypeException("Mimetype not found");
			throw new UnicornException("");
		}
		try {
			mimeType = new MimeType(sMimeType);
		} catch (MimeTypeParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		inputModule = new FileItemInputModule(mimeType, file);
	}

	@Override
	public String getDocumentName() {
		return file.getName();
	}

	@Override
	public EnumInputMethod getInputMethod() {
		return EnumInputMethod.UPLOAD;
	}

}

--- NEW FILE: DirectInputParameter.java ---
package org.w3c.unicorn.input;

import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;

import org.w3c.unicorn.UnicornCall;
import org.w3c.unicorn.contract.EnumInputMethod;
import org.w3c.unicorn.exceptions.UnicornException;
import org.w3c.unicorn.util.Property;

public class DirectInputParameter extends InputParameter {
	
	private String document;
	
	private String sMimeType;
	
	public DirectInputParameter(String document, String sMimeType) {
		this.document = document;
		this.sMimeType = sMimeType;
	}

	@Override
	public void check() throws UnicornException {
		if (null == sMimeType || "".equals(sMimeType)) {
			//throw new NoMimeTypeException("Mimetype not found.");
			throw new UnicornException("");
		}
		try {
			mimeType = new MimeType(sMimeType);
		} catch (MimeTypeParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		inputModule = new DirectInputModule(mimeType, document);
	}

	@Override
	public String getDocumentName() {
		//TODO
		return "Direct Input Document";
	}

	@Override
	public EnumInputMethod getInputMethod() {
		return EnumInputMethod.DIRECT;
	}

}

--- NEW FILE: InputParameter.java ---
package org.w3c.unicorn.input;

import javax.activation.MimeType;

import org.w3c.unicorn.contract.EnumInputMethod;
import org.w3c.unicorn.exceptions.UnicornException;

public abstract class InputParameter {
	
	protected EnumInputMethod inputMethod;

	protected InputModule inputModule;
	
	protected MimeType mimeType;
	
	protected String documentName;
	
	public abstract void check() throws UnicornException;

	public InputModule getInputModule() {
		return inputModule;
	}

	public void setInputModule(InputModule inputModule) {
		this.inputModule = inputModule;
	}
	
	public MimeType getMimeType() {
		return mimeType;
	}
	
	public void setMimeType(MimeType mimeType) {
		this.mimeType = mimeType;
	}

	public abstract String getDocumentName();

	public void setDocumentName(String documentName) {
		this.documentName = documentName;
	}

	public abstract EnumInputMethod getInputMethod();

	public void setInputMethod(EnumInputMethod inputMethod) {
		this.inputMethod = inputMethod;
	}
	
}

--- NEW FILE: URIInputParameter.java ---
package org.w3c.unicorn.input;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;

import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.net.ssl.SSLException;

import org.w3c.unicorn.contract.EnumInputMethod;
import org.w3c.unicorn.exceptions.UnicornException;
import org.w3c.unicorn.util.Property;

public class URIInputParameter extends InputParameter {
	
	private String uri;
	
	private int connectTimeOut = Integer.parseInt(Property.get("DOCUMENT_CONNECT_TIMEOUT"));
	
	public URIInputParameter(String uri) {
		this.uri = uri;
	}
	
	@Override
	public void check() throws UnicornException {
		URL docUrl = null;
		try {
			docUrl = new URL(uri);
			URLConnection con = docUrl.openConnection();
			con.setConnectTimeout(connectTimeOut);
			String sMimeType = con.getContentType();
			sMimeType = sMimeType.split(";")[0];
			mimeType = new MimeType(sMimeType);
			inputModule = new URIInputModule(mimeType, uri);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (MimeTypeParseException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) { 
			e.printStackTrace();
		} catch (SSLException e) {
			e.printStackTrace();
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
			if (e.getMessage().contains("Read timed out"))
				System.out.println("a");
			if (e.getMessage().contains("connect timed out"))
				System.out.println("b");
		} catch (IOException e) {
			try {
				int responseCode;
				if (docUrl != null) {
					responseCode = ((HttpURLConnection) docUrl.openConnection()).getResponseCode();
					switch (responseCode) {
					case HttpURLConnection.HTTP_UNAUTHORIZED:
						System.out.println("HTTP_UNAUTHORIZED");
						break;
					}
				}
			} catch (Exception e2) {
				System.out.println("e2");
				e2.printStackTrace();
			}
			e.printStackTrace();
		}
	}

	@Override
	public String getDocumentName() {
		return uri;
	}

	@Override
	public EnumInputMethod getInputMethod() {
		return EnumInputMethod.URI;
	}

}
Received on Thursday, 17 September 2009 15:39:21 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Thursday, 26 April 2012 12:55:14 GMT