/** * Creates a new XMLCall for the given url. * * @param url the target URL to call * @param contentType the Content-Type to use * @return a new instance to perform the xml call * @throws IOException in case of an IO error */ public static XMLCall to(URL url, String contentType) throws IOException { XMLCall result = new XMLCall(); result.outcall = new Outcall(url); result.outcall.setRequestProperty("Content-Type", contentType); return result; }
/** * Returns the result of the call as String. * * @return a String containing the complete result of the call * @throws IOException in case of any IO error */ public String getData() throws IOException { StringWriter writer = new StringWriter(); InputStreamReader reader = new InputStreamReader(getInput(), getContentEncoding()); CharStreams.copy(reader, writer); reader.close(); return writer.toString(); }
/** * Adds a custom header field to the call * * @param name name of the field * @param value value of the field * @return returns the XML call itself for fluent method calls */ public XMLCall addHeader(String name, String value) { outcall.setRequestProperty(name, value); return this; }
/** * Can be used to generate the XML request. * * @return the an input which can be used to generate an XML document which is sent to the URL * @throws IOException in case of an IO error while sending the XML document */ public XMLStructuredOutput getOutput() throws IOException { return new XMLStructuredOutput(outcall.getOutput()); }
/** * Provides access to the XML answer of the call. * * @return the XML result of the call * @throws IOException in case of an IO error while receiving the result */ public XMLStructuredInput getInput() throws IOException { return new XMLStructuredInput(outcall.getInput(), true); } }
/** * Sets the HTTP Authorization header. * * @param user the username to use * @param password the password to use * @throws IOException in case of any IO error */ public void setAuthParams(String user, String password) throws IOException { if (Strings.isEmpty(user)) { return; } try { String userAndPassword = user + ":" + password; String encodedAuthorization = BaseEncoding.base64().encode(userAndPassword.getBytes(charset.name())); setRequestProperty("Authorization", "Basic " + encodedAuthorization); } catch (UnsupportedEncodingException e) { throw new IOException(e); } }
/** * Sents the given context as POST to the designated server. * * @param params the data to POST * @param charset the charset to use when encoding the post data * @return the outcall itself for fluent method calls * @throws IOException in case of any IO error */ public Outcall postData(Context params, Charset charset) throws IOException { connection.setRequestMethod(REQUEST_METHOD_POST); connection.setRequestProperty(HEADER_CONTENT_TYPE, CONTENT_TYPE_FORM_URLENCODED); this.charset = charset; OutputStreamWriter writer = new OutputStreamWriter(getOutput(), charset.name()); StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<String, Object> entry : params.entrySet()) { if (!first) { sb.append("&"); } first = false; sb.append(URLEncoder.encode(entry.getKey(), charset.name())); sb.append("="); sb.append(URLEncoder.encode(NLS.toMachineString(entry.getValue()), charset.name())); } writer.write(sb.toString()); writer.flush(); return this; }
/** * Sets a HTTP cookie * * @param name name of the cookie * @param value value of the cookie */ public void setCookie(String name, String value) { if (Strings.isFilled(name) && Strings.isFilled(value)) { setRequestProperty("Cookie", name + "=" + value); } } }