try {
URI uri = new URI(fullCommand);
url = uri.toURL();
} catch (URISyntaxException e) {
throw new EchoNestException(
EchoNestException.CLIENT_SERVER_INCONSISTENCY,
"Bad URL " + e);
} catch (MalformedURLException e) {
throw new EchoNestException(
EchoNestException.CLIENT_SERVER_INCONSISTENCY,
"Bad URL " + e);
}
if (traceSends) {
System.out.println("Sending--> " + url);
}
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setAllowUserInteraction(false);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + BOUNDARY);
DataOutputStream dostream = new DataOutputStream(conn.getOutputStream());
//DataOutputStream dostream = new DataOutputStream(System.out);
OutputStreamWriter out = new OutputStreamWriter(dostream, "utf-8");
Map<String, Object> p = new HashMap<String, Object>(params);
for (Entry<String, Object> kv : standardParams.getMap().entrySet()) {
p.put(kv.getKey(), kv.getValue());
}
if (false) {
for (String s : p.keySet()) {
System.out.printf(" %s=%s\n", s, p.get(s));
}
}
for (String key : p.keySet()) {
out.write(PREFIX);
out.write(BOUNDARY);
out.write(NEWLINE);
Object val = p.get(key);
if (val instanceof File) {
File file = (File) val;
out
.write("Content-Disposition: form-data; name=\"file\";"
+ " filename=\"" + file.getName() + "\"");
out.write(NEWLINE);
out.write("Content-Type: application/octet-stream");
out.write(NEWLINE);
out.write(NEWLINE);
out.flush();
InputStream is = new FileInputStream(file);
int r = 0;
byte[] data = new byte[1024];
while ((r = is.read(data, 0, data.length)) != -1) {
dostream.write(data, 0, r);
}
is.close();
out.write(NEWLINE);
} else {
out.write("Content-Disposition: form-data; name=\""
+ key + "\"");
out.write(NEWLINE);
out.write(NEWLINE);
out.write(val.toString());
//dos.writeUTF(val.toString());
out.write(NEWLINE);
}
}
out.write(PREFIX);
out.write(BOUNDARY);
out.write(PREFIX);
out.write(NEWLINE);
// close streams
out.flush();
out.close();
int code = conn.getResponseCode();
InputStream is = null;
if (code >= 300) {
is = conn.getErrorStream();
} else {
is = conn.getInputStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(is,
Charset.forName("UTF-8")));
String line = null;
StringBuilder results = new StringBuilder();
while ((line = in.readLine()) != null) {
results.append(line);
}
if (traceRecvs) {
System.out.println("received--> " + results.toString());
}
in.close();
String resultString = results.toString();
try {
JSONObject jobj = (JSONObject) parser.parse(resultString);
checkStatus(jobj);
return (Map) jobj;
} catch (ParseException e) {
throw new IOException("Parse Exception", e);
}
} catch (IOException ioe) {
throw new EchoNestException(ioe);
}
}