cmailleux 2005/03/30 16:07:20 CEST
Modified files:
war/src/java/com/codeva/webapps/webclipping/servlet
WebClippingServlet.java
Log:
Fully manage redirection
Revision Changes Path
1.6 +44 -26
webclipping/war/src/java/com/codeva/webapps/webclipping/servlet/WebClippingServlet.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclipping/war/src/java/com/codeva/webapps/webclipping/servlet/WebClippingServlet.java.diff?r1=1.5&r2=1.6&f=h
Index: WebClippingServlet.java
===================================================================
RCS file:
/home/cvs/repository/webclipping/war/src/java/com/codeva/webapps/webclipping/servlet/WebClippingServlet.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- WebClippingServlet.java 30 Mar 2005 09:15:21 -0000 1.5
+++ WebClippingServlet.java 30 Mar 2005 14:07:20 -0000 1.6
@@ -1,24 +1,23 @@
package com.codeva.webapps.webclipping.servlet;
import au.id.jericho.lib.html.*;
+import com.codeva.webapps.webclipping.Rewriter;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
-import com.codeva.webapps.webclipping.Rewriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
+import java.net.URLEncoder;
import java.util.Iterator;
-import java.util.Map;
import java.util.List;
+import java.util.Map;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
-import java.net.URL;
-import java.net.URLEncoder;
/**
* Created by IntelliJ IDEA.
@@ -38,7 +37,8 @@
static final String APPPARAMS_PARAMS = "appparams";
static final String RESET_PARAMS = "resetAppSession";
private static final MultiThreadedHttpConnectionManager
HTPP_CONNECTION_MANAGER = new MultiThreadedHttpConnectionManager();
- public WebClippingServlet() {
+
+ public WebClippingServlet() {
}
/**
@@ -97,7 +97,7 @@
throws IOException {
// Get the httpClient
HttpClient httpClient = getHttpClient(httpServletRequest);
-
httpServletRequest.setAttribute("org.jahia.portletapi.CacheExpirationDelay",new
Long(0));
+
httpServletRequest.setAttribute("org.jahia.portletapi.CacheExpirationDelay",
new Long(0));
// Create a get method for accessing the url.
StringBuffer params = new StringBuffer(4096);
Map parameters = httpServletRequest.getParameterMap();
@@ -115,7 +115,7 @@
!RESET_PARAMS.equalsIgnoreCase(paramName) &&
!"matrix".equalsIgnoreCase(paramName)) {
final Object value = entry.getValue();
- final String characterEncoding =
(httpServletRequest.getCharacterEncoding()!=null)?httpServletRequest.getCharacterEncoding():"UTF-8";
+ final String characterEncoding =
(httpServletRequest.getCharacterEncoding() != null) ?
httpServletRequest.getCharacterEncoding() : "UTF-8";
if (value instanceof String[]) {
String[] strings = (String[]) value;
StringBuffer buffer = new StringBuffer(4096);
@@ -123,11 +123,14 @@
String string = strings[i];
buffer.append((i != 0) ? "," : "").append(string);
}
- params.append((index == 0 ? "?" : "&") +
entry.getKey().toString() + "=" +
URLEncoder.encode(buffer.toString(),characterEncoding));
+ params.append(
+ (index == 0 ? "?" : "&") +
entry.getKey().toString() + "=" +
+ URLEncoder.encode(buffer.toString(),
characterEncoding));
index++;
- }
- else {
- params.append((index == 0 ? "?" : "&") +
entry.getKey().toString() + "=" +
URLEncoder.encode(value.toString(),characterEncoding));
+ } else {
+ params.append(
+ (index == 0 ? "?" : "&") +
entry.getKey().toString() + "=" +
+ URLEncoder.encode(value.toString(),
characterEncoding));
index++;
}
}
@@ -146,10 +149,11 @@
HttpClient httpClient = (HttpClient)
httpServletRequest.getSession(true).getAttribute(httpClientName);
if (httpClient == null) {
httpClient = new HttpClient(HTPP_CONNECTION_MANAGER);
-
httpClient.getParams().setParameter("http.useragent",httpServletRequest.getHeader("User-Agent"));
+ httpClient.getParams().setParameter("http.useragent",
httpServletRequest.getHeader("User-Agent"));
final String characterEncoding =
httpServletRequest.getCharacterEncoding();
-
httpClient.getParams().setParameter("http.protocol.content-charset",
(characterEncoding!=null)?characterEncoding:"UTF-8");
-
httpServletRequest.getSession().setAttribute(httpClientName,httpClient);
+
httpClient.getParams().setParameter("http.protocol.content-charset",
+ (characterEncoding != null)
? characterEncoding : "UTF-8");
+ httpServletRequest.getSession().setAttribute(httpClientName,
httpClient);
}
return httpClient;
}
@@ -158,7 +162,7 @@
HttpServletResponse httpServletResponse)
throws ServletException, IOException {
try {
-
httpServletRequest.setAttribute("org.jahia.portletapi.CacheExpirationDelay",new
Long(0));
+
httpServletRequest.setAttribute("org.jahia.portletapi.CacheExpirationDelay",
new Long(0));
String opMode = (String)
httpServletRequest.getAttribute("org.jahia.operationmode");
if (checkInit(httpServletRequest)) {
// Reconstruct the url
@@ -226,7 +230,6 @@
// Execute the method and check the status of the result
int statusCode = 0;
try {
- httpMethod.setFollowRedirects(true);
statusCode = httpClient.executeMethod(httpMethod);
} catch (IOException e) {
StringBuffer buffer = new StringBuffer("<html>\n<body>");
@@ -235,6 +238,19 @@
rewriteBody(buffer.toString(), httpServletRequest,
httpServletResponse);
return;
}
+ if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY ||
+ statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
+ statusCode == HttpStatus.SC_SEE_OTHER ||
+ statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
+ String redirectLocation;
+ Header locationHeader = httpMethod.getResponseHeader("location");
+ if (locationHeader != null) {
+ redirectLocation = locationHeader.getValue();
+ httpMethod.setPath(redirectLocation);
+ getResponse(httpClient, httpMethod, httpServletResponse,
httpServletRequest);
+ return;
+ }
+ }
if (statusCode != HttpStatus.SC_OK) {
// status not ok so let us throw a beautiful exception
StringBuffer buffer = new StringBuffer("<html>\n<body>");
@@ -247,29 +263,31 @@
String[] type =
httpMethod.getResponseHeader("Content-Type").getValue().split(";");
String contentType = type[0];
String contentCharset = "ISO-8859-1";
- if(type.length==2)
- contentCharset = type[1].split("=")[1];
+ if (type.length == 2) {
+ contentCharset = type[1].split("=")[1];
+ }
httpServletResponse.setContentType(contentType);
url = httpMethod.getURI().getURIReference();
final byte[] responseBodyAsBytes = httpMethod.getResponseBody();
if (contentType.startsWith("text")) {
- if(type.length==1) {
- String responseBody = new
String(responseBodyAsBytes,"US-ASCII");
+ if (type.length == 1) {
+ String responseBody = new String(responseBodyAsBytes,
"US-ASCII");
Source source = new Source(responseBody);
List list = source.findAllStartTags(Tag.META);
for (int i = 0; i < list.size(); i++) {
StartTag startTag = (StartTag) list.get(i);
Attributes attributes = startTag.getAttributes();
final Attribute attribute = attributes.get("http-equiv");
- if(attribute!=null &&
attribute.getValue().equalsIgnoreCase("content-type")) {
+ if (attribute != null &&
attribute.getValue().equalsIgnoreCase("content-type")) {
type =
attributes.get("content").getValue().split(";");
- if(type.length==2)
+ if (type.length == 2) {
contentCharset = type[1].split("=")[1];
+ }
}
}
}
- rewriteBody(new String(responseBodyAsBytes,contentCharset),
httpServletRequest, httpServletResponse);
+ rewriteBody(new String(responseBodyAsBytes, contentCharset),
httpServletRequest, httpServletResponse);
} else {
httpServletResponse.getOutputStream().write(responseBodyAsBytes);
}
@@ -281,7 +299,8 @@
httpServletRequest,
httpServletResponse);
final String characterEncoding =
httpServletRequest.getCharacterEncoding();
-
httpServletResponse.getOutputStream().write(document.toString().getBytes(httpServletResponse.getCharacterEncoding()));
+ httpServletResponse.getOutputStream().write(
+
document.toString().getBytes(httpServletResponse.getCharacterEncoding()));
}
@@ -309,8 +328,7 @@
if (!"".equals(siteUrl.trim())) {
url = siteUrl;
return true;
- }
- else {
+ } else {
return false;
}
} else {