- From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
- Date: Fri, 18 Sep 2009 14:56:08 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/input In directory hutz:/tmp/cvs-serv1827/src/org/w3c/unicorn/input Modified Files: URIInputParameter.java DirectInputParameter.java UploadInputParameter.java Log Message: implemented input verification messages. Made use of the new UnicornException constructor. Index: UploadInputParameter.java =================================================================== RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/input/UploadInputParameter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- UploadInputParameter.java 17 Sep 2009 15:39:10 -0000 1.1 +++ UploadInputParameter.java 18 Sep 2009 14:56:06 -0000 1.2 @@ -2,11 +2,10 @@ 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; +import org.w3c.unicorn.util.Message; public class UploadInputParameter extends InputParameter { @@ -19,23 +18,20 @@ @Override public void check() throws UnicornException { if (file.getName() == null || file.getName().equals("")) { - //throw new NoDocumentException("No document provided"); - throw new UnicornException(""); + throw new UnicornException(Message.Level.ERROR, "$message_no_uploaded_file", null); } if (file.getSize() == 0) { - //throw new EmptyDocumentException("Empty document provided"); - throw new UnicornException(""); + throw new UnicornException(Message.Level.ERROR, "$message_empty_uploaded_file", null); } String sMimeType = file.getContentType(); if (null == sMimeType || "".equals(sMimeType)) { - //throw new NoMimeTypeException("Mimetype not found"); - throw new UnicornException(""); + // TODO Is there another solution here to find the mime-type ? + throw new UnicornException(Message.Level.ERROR, "$message_not_found_mime_type", null); } try { mimeType = new MimeType(sMimeType); } catch (MimeTypeParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new UnicornException(Message.Level.ERROR, "$message_invalid_mime_type", null); } inputModule = new FileItemInputModule(mimeType, file); } Index: DirectInputParameter.java =================================================================== RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/input/DirectInputParameter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- DirectInputParameter.java 17 Sep 2009 15:39:10 -0000 1.1 +++ DirectInputParameter.java 18 Sep 2009 14:56:06 -0000 1.2 @@ -3,10 +3,9 @@ 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; +import org.w3c.unicorn.util.Message; public class DirectInputParameter extends InputParameter { @@ -21,15 +20,14 @@ @Override public void check() throws UnicornException { - if (null == sMimeType || "".equals(sMimeType)) { - //throw new NoMimeTypeException("Mimetype not found."); - throw new UnicornException(""); - } + if (document == null || document.equals("")) + throw new UnicornException(Message.Level.ERROR, "$message_empty_direct_input", null); + if (sMimeType == null || sMimeType.equals("")) + throw new UnicornException(Message.Level.ERROR, "$message_missing_mime_type", null); try { mimeType = new MimeType(sMimeType); } catch (MimeTypeParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new UnicornException(Message.Level.ERROR, "$message_invalid_mime_type", null); } inputModule = new DirectInputModule(mimeType, document); } Index: URIInputParameter.java =================================================================== RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/input/URIInputParameter.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- URIInputParameter.java 17 Sep 2009 17:06:48 -0000 1.5 +++ URIInputParameter.java 18 Sep 2009 14:56:06 -0000 1.6 @@ -7,6 +7,7 @@ import java.net.SocketTimeoutException; import java.net.URL; import java.net.UnknownHostException; +import java.util.regex.Pattern; import javax.activation.MimeType; import javax.activation.MimeTypeParseException; @@ -21,60 +22,65 @@ private String uri; - private int connectTimeOut = Integer.parseInt(Property.get("DOCUMENT_CONNECT_TIMEOUT")); + private int connectTimeOut; public URIInputParameter(String uri) { this.uri = uri; + if (Property.get("DOCUMENT_CONNECT_TIMEOUT") != null) + connectTimeOut = Integer.parseInt(Property.get("DOCUMENT_CONNECT_TIMEOUT")); + else + connectTimeOut = 0; } @Override public void check() throws UnicornException { URL docUrl = null; try { + if (uri.equals("")) + throw new UnicornException(Message.Level.ERROR, "$message_empty_uri", null); + + Pattern urlPattern = Pattern.compile("^(https?)://([A-Z0-9][A-Z0-9_-]*)(\\.[A-Z0-9][A-Z0-9_-]*)*(:(\\d+))?([/#]\\p{ASCII}*)?", Pattern.CASE_INSENSITIVE); + if (!urlPattern.matcher(uri).matches()) { + if (!uri.contains("://")) + uri = "http://" + uri; + if (!urlPattern.matcher(uri).matches()) + throw new UnicornException(Message.Level.ERROR, "$message_invalid_url_syntax " + uri, null); + } docUrl = new URL(uri); + if (!docUrl.getProtocol().equals("http") && !docUrl.getProtocol().equals("https")) + throw new UnicornException(Message.Level.ERROR, "$message_unsupported_protocol " + docUrl.getProtocol(), null); HttpURLConnection con = (HttpURLConnection) docUrl.openConnection(); con.setConnectTimeout(connectTimeOut); con.connect(); - Message message; int responseCode = con.getResponseCode(); switch (responseCode) { case HttpURLConnection.HTTP_UNAUTHORIZED: - message = new Message(Message.Level.ERROR, "$message_unauthorized_access", null); - throw new UnicornException(message); + throw new UnicornException(Message.Level.ERROR, "$message_unauthorized_access", null); case HttpURLConnection.HTTP_NOT_FOUND: - message = new Message(Message.Level.ERROR, "$message_document_not_found", null); - throw new UnicornException(message); + throw new UnicornException(Message.Level.ERROR, "$message_document_not_found", null); } String sMimeType = con.getContentType(); sMimeType = sMimeType.split(";")[0]; mimeType = new MimeType(sMimeType); inputModule = new URIInputModule(mimeType, uri); } catch (MalformedURLException e) { - Message message = new Message(Message.Level.ERROR, "$message_invalid_url_syntax " + uri, null); - throw new UnicornException(message); + throw new UnicornException(Message.Level.ERROR, "$message_invalid_url_syntax " + uri, null); } catch (MimeTypeParseException e) { - Message message = new Message(Message.Level.ERROR, "$message_invalid_mime_type", null); - throw new UnicornException(message); + throw new UnicornException(Message.Level.ERROR, "$message_invalid_mime_type", null); } catch (UnknownHostException e) { - Message message = new Message(Message.Level.ERROR, "$message_unknown_host", null); - throw new UnicornException(message); + throw new UnicornException(Message.Level.ERROR, "$message_unknown_host", null); } catch (SSLException e) { - Message message = new Message(Message.Level.ERROR, "$message_ssl_exception", null); - throw new UnicornException(message); + throw new UnicornException(Message.Level.ERROR, "$message_ssl_exception", null); } catch (ConnectException e) { - Message message = new Message(Message.Level.ERROR, "$message_connect_exception", null); - throw new UnicornException(message); + throw new UnicornException(Message.Level.ERROR, "$message_connect_exception", null); } catch (SocketTimeoutException e) { if (e.getMessage().contains("connect timed out")) { - Message message = new Message(Message.Level.ERROR, "$message_connect_exception", null); - throw new UnicornException(message); + throw new UnicornException(Message.Level.ERROR, "$message_connect_exception", null); } else { - Message message = new Message(e); - throw new UnicornException(message); + throw new UnicornException(new Message(e)); } } catch (IOException e) { - Message message = new Message(e); - throw new UnicornException(message); + throw new UnicornException(new Message(e)); } }
Received on Friday, 18 September 2009 14:56:18 UTC