Mapper mapper = new Mapper() {
public MediaResource handle(String relPath, HttpServletRequest hreq) {
MediaResource mr = null;
String method = hreq.getMethod();
String uri = relPath;
HttpMethod meth = null;
if (uri == null) uri = (startUri == null) ? "" : startUri;
if (uri.length() > 0 && uri.charAt(0) != '/') uri = "/" + uri;
//String contentType = hreq.getContentType();
// if (allowedToSendPersonalHeaders) {
String userName = ident.getName();
User u = ident.getUser();
String lastName = u.getProperty(UserConstants.LASTNAME, loc);
String firstName = u.getProperty(UserConstants.FIRSTNAME, loc);
String email = u.getProperty(UserConstants.EMAIL, loc);
if (method.equals("GET")) {
GetMethod cmeth = new GetMethod(uri);
String queryString = hreq.getQueryString();
if (queryString != null) cmeth.setQueryString(queryString);
meth = cmeth;
// if response is a redirect, follow it
if (meth == null) return null;
meth.setFollowRedirects(true);
} else if (method.equals("POST")) {
//if (contentType == null || contentType.equals("application/x-www-form-urlencoded")) {
// regular post, no file upload
//}
Map params = hreq.getParameterMap();
PostMethod pmeth = new PostMethod(uri);
Set postKeys = params.keySet();
for (Iterator iter = postKeys.iterator(); iter.hasNext();) {
String key = (String) iter.next();
String vals[] = (String[]) params.get(key);
for (int i = 0; i < vals.length; i++) {
pmeth.addParameter(key, vals[i]);
}
meth = pmeth;
}
if (meth == null) return null;
// Redirects are not supported when using POST method!
// See RFC 2616, section 10.3.3, page 62
}
// Add olat specific headers to the request, can be used by external
// applications to identify user and to get other params
// test page e.g. http://cgi.algonet.se/htbin/cgiwrap/ug/test.py
meth.addRequestHeader("X-OLAT-USERNAME", userName);
meth.addRequestHeader("X-OLAT-LASTNAME", lastName);
meth.addRequestHeader("X-OLAT-FIRSTNAME", firstName);
meth.addRequestHeader("X-OLAT-EMAIL", email);
boolean ok = false;
try {
httpClientInstance.executeMethod(meth);
ok = true;
} catch (Exception e) {
// handle error later
}
if (!ok) {
// error
meth.releaseConnection();
return new NotFoundMediaResource(relPath);
}
// get or post successfully
Header responseHeader = meth.getResponseHeader("Content-Type");
if (responseHeader == null) {
// error
return new NotFoundMediaResource(relPath);
}
mr = new HttpRequestMediaResource(meth);