parameters my contain String or String[]
[ical-validator.git] / src / main / java / net / bzzt / ical / UrlValidationPage.java
blobe0a96553e7964c9c21798c7d6737f097bd8aad11
1 package net.bzzt.ical;
3 import net.bzzt.ical.services.IcalendarValidationService;
5 import org.apache.commons.httpclient.Header;
6 import org.apache.commons.httpclient.HeaderElement;
7 import org.apache.commons.httpclient.HttpClient;
8 import org.apache.commons.httpclient.NameValuePair;
9 import org.apache.commons.httpclient.URI;
10 import org.apache.commons.httpclient.URIException;
11 import org.apache.commons.httpclient.methods.GetMethod;
12 import org.apache.commons.lang.StringUtils;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.wicket.PageParameters;
16 import org.apache.wicket.markup.html.WebMarkupContainer;
17 import org.apache.wicket.markup.html.WebPage;
18 import org.wicketstuff.annotation.mount.MountPath;
19 import org.wicketstuff.annotation.strategy.MountMixedParam;
20 import org.wicketstuff.annotation.strategy.MountQueryString;
22 @MountPath(path = "urlvalidate")
23 //MountMixedParams didn't work behind apache proxypass when the url contained a %2F
24 @MountQueryString
25 public class UrlValidationPage extends ValidatorLayoutPage {
26 private static final Log LOG = LogFactory.getLog(UrlValidationPage.class);
28 public UrlValidationPage(PageParameters parameters)
30 try {
31 String url = getUrl(parameters);
32 if (url != null)
34 init(new URI(url, true));
36 else
38 init(null);
41 } catch (URIException e) {
42 e.printStackTrace();
46 private String getUrl(PageParameters parameters) {
47 Object url = parameters.get("url");
48 if (url == null)
50 return null;
52 else if (url instanceof String)
54 return (String) url;
56 else if (url instanceof String[])
58 String[] urls = (String[]) url;
59 if (urls != null && urls.length > 0)
61 return urls[0];
64 return null;
67 private void init(URI uri) {
68 GetMethod method = new GetMethod();
69 try {
70 method.setURI(uri);
71 } catch (Exception e) {
72 error(e);
75 HttpClient client = new HttpClient();
76 try {
77 client.executeMethod(method);
78 } catch (Exception e) {
79 error(e);
82 add(new UrlFormPanel("urlForm", uri));
84 method.getResponseCharSet();
86 try {
87 LOG.info("Validating url " + uri);
88 Header contentType = method.getResponseHeader("Content-Type");
89 String charSet = null;
90 if (contentType != null)
92 HeaderElement[] headerElements = contentType.getElements();
93 if (headerElements.length > 0)
95 NameValuePair parameter = headerElements[0].getParameterByName("charset");
96 if (parameter != null)
98 charSet = parameter.getValue();
103 if (StringUtils.isBlank(charSet))
105 charSet = "utf-8";
108 add(IcalendarValidationService.getValidationResult("result", method.getResponseBodyAsStream(), charSet));
109 } catch (Exception e) {
110 add(new WebMarkupContainer("result"));
111 error(e);
112 LOG.error(e.getMessage(), e);