1 package com.hammurapi.jcapture;
2 
3 import org.apache.commons.codec.DecoderException;
4 import org.apache.commons.codec.binary.Hex;
5 import org.apache.http.client.methods.HttpPost;
6 import org.apache.http.client.methods.HttpUriRequest;
7 import org.apache.http.entity.mime.MultipartEntity;
8 import org.apache.http.entity.mime.content.InputStreamBody;
9 import org.apache.http.entity.mime.content.StringBody;
10 
11 public class JCaptureApplet extends AbstractCaptureApplet {
12 
13 	private static final String HTTPS_PREFIX = "https://";
14 
createRequest(String fileName, InputStreamBody bin)15 	protected HttpUriRequest createRequest(String fileName, InputStreamBody bin) throws Exception {
16 		String uploadUrl = getParameter("uploadUrl");
17 		if (uploadUrl==null || uploadUrl.trim().length()==0) {
18 			String host = getParameter("host");
19 			String dokuHost = host;
20 
21 			if (dokuHost.toLowerCase().startsWith(HTTPS_PREFIX)) {
22 				if (dokuHost.lastIndexOf(":")<HTTPS_PREFIX.length()) { // No port number
23 					dokuHost+=":443";
24 				}
25 			} else if (dokuHost.endsWith(":80")) {
26 				dokuHost = dokuHost.substring(0, dokuHost.length()-3);
27 			}
28 			System.out.println("DokuHost: "+dokuHost);
29 			String dokuBase = getDokuBase();
30 			System.out.println("DokuBase: "+dokuBase);
31 
32 			StringBuilder uploadUrlBuilder = new StringBuilder(dokuHost);
33 
34 			if (dokuBase.startsWith(host)) {
35 				dokuBase = dokuBase.substring(host.length());
36 			}
37 			uploadUrlBuilder.append(dokuBase);
38 			uploadUrlBuilder.append("lib/exe/mediamanager.php");
39 			uploadUrl = uploadUrlBuilder.toString();
40 		}
41 		System.out.println("Uploading to "+uploadUrl);
42         HttpPost httppost = new HttpPost(uploadUrl);
43 
44         if (!httppost.containsHeader("Cookie")) {
45         	httppost.setHeader("Cookie", getCookies());
46         }
47 
48         httppost.setHeader("Pragma", "No-cache");
49 
50 	    MultipartEntity reqEntity = new MultipartEntity();
51 	    String sectok = getParameter("sectok");
52 	    if (sectok!=null && sectok.trim().length()>0) {
53 			reqEntity.addPart("sectok", new StringBody(sectok));
54 	    }
55 	    reqEntity.addPart("ow", new StringBody("1"));
56 
57 	    String opaque = getParameter("opaque");
58 	    if (opaque!=null && opaque.trim().length()>0) {
59 			reqEntity.addPart("opaque", new StringBody(opaque));
60 	    }
61 
62 	    reqEntity.addPart("Filename", new StringBody(fileName));
63 
64 	    int nsIdx = fileName.lastIndexOf(":");
65 	    String namespace;
66 	    if (nsIdx==-1) {
67 	    	namespace = ":";
68 	    } else {
69 	    	namespace = ":"+fileName.substring(0, nsIdx);
70 	    	fileName = fileName.substring(nsIdx+1);
71 	    }
72 
73 	    if (namespace!=null) {
74 		    reqEntity.addPart("ns", new StringBody(namespace));
75 	    }
76 
77 	    reqEntity.addPart("Filedata", bin);
78 
79 	    httppost.setEntity(reqEntity);
80 		return httppost;
81 	}
82 
getDokuBase()83 	String getDokuBase() throws DecoderException {
84 		return new String(Hex.decodeHex(getParameter("dokuBase").toCharArray()));
85 	}
86 
87 	@Override
bodyName(String fileName)88 	protected String bodyName(String fileName) {
89 	    return fileName.substring(fileName.lastIndexOf(":")+1);
90 	}
91 
92 }
93