// Check for valid attributes
XdmSequenceIterator iter = start.axisIterator(Axis.ATTRIBUTE);
boolean ok = true;
while (iter.hasNext()) {
XdmNode attr = (XdmNode) iter.next();
QName name = attr.getNodeName();
if (_method.equals(name) || _href.equals(name) || _detailed.equals(name)
|| _status_only.equals(name) || _username.equals(name) || _password.equals(name)
|| _auth_method.equals(name) || _send_authorization.equals(name)
|| _override_content_type.equals(name)) {
// nop
} else {
if (XMLConstants.DEFAULT_NS_PREFIX.equals(name.getNamespaceURI())) {
throw new XProcException(step.getNode(), "Unsupported attribute on c:request for p:http-request: " + name);
}
}
}
String send = step.getExtensionAttribute(cx_send_binary);
encodeBinary = !"true".equals(send);
method = start.getAttributeValue(_method);
statusOnly = "true".equals(start.getAttributeValue(_status_only));
detailed = "true".equals(start.getAttributeValue(_detailed));
overrideContentType = start.getAttributeValue(_override_content_type);
if (method == null) {
throw XProcException.stepError(6);
}
if (statusOnly && !detailed) {
throw XProcException.stepError(4);
}
if (start.getAttributeValue(_href) == null) {
throw new XProcException(step.getNode(), "The 'href' attribute must be specified on c:request for p:http-request");
}
requestURI = start.getBaseURI().resolve(start.getAttributeValue(_href));
String scheme = requestURI.getScheme();
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
doFile(start.getAttributeValue(_href), start.getBaseURI().toASCIIString());
return;
}
HttpParams params = new BasicHttpParams();
HttpContext localContext = new BasicHttpContext();
// The p:http-request step should follow redirect requests if they are returned by the server.
params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
// What about cookies
String saveCookieKey = step.getExtensionAttribute(cx_save_cookies);
String useCookieKeys = step.getExtensionAttribute(cx_use_cookies);
String cookieKey = step.getExtensionAttribute(cx_cookies);
if (saveCookieKey == null) {
saveCookieKey = cookieKey;
}
if (useCookieKeys == null) {
useCookieKeys = cookieKey;
}
// If a redirect response includes cookies, those cookies should be forwarded
// as appropriate to the redirected location when the redirection is followed.
CookieStore cookieStore = new BasicCookieStore();
if (useCookieKeys != null && useCookieKeys.equals(saveCookieKey)) {
cookieStore = runtime.getCookieStore(useCookieKeys);
} else if (useCookieKeys != null) {
CookieStore useCookieStore = runtime.getCookieStore(useCookieKeys);
for (Cookie cookie : useCookieStore.getCookies()) {
cookieStore.addCookie(cookie);
}
}
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// FIXME: Is browser compatability the right thing? It's the right thing for my unit test...
params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
String timeOutStr = step.getExtensionAttribute(cx_timeout);
if (timeOutStr != null) {
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, Integer.parseInt(timeOutStr));
}
if (start.getAttributeValue(_username) != null) {
String user = start.getAttributeValue(_username);
String pass = start.getAttributeValue(_password);
String meth = start.getAttributeValue(_auth_method);
List<String> authpref;
if ("basic".equalsIgnoreCase(meth)) {
authpref = Collections.singletonList(AuthPolicy.BASIC);
} else if ("digest".equalsIgnoreCase(meth)) {
authpref = Collections.singletonList(AuthPolicy.DIGEST);
} else {
throw XProcException.stepError(3, "Unsupported auth-method: " + meth);
}
String host = requestURI.getHost();
int port = requestURI.getPort();
AuthScope scope = new AuthScope(host,port);
UsernamePasswordCredentials cred = new UsernamePasswordCredentials(user, pass);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(scope, cred);
localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
params.setBooleanParameter(ClientPNames.HANDLE_AUTHENTICATION, true);
params.setParameter(AuthPNames.TARGET_AUTH_PREF, authpref);
}
iter = start.axisIterator(Axis.CHILD);
XdmNode body = null;
while (iter.hasNext()) {
XdmNode event = (XdmNode) iter.next();
// FIXME: What about non-whitespace text nodes?
if (event.getNodeKind() == XdmNodeKind.ELEMENT) {
if (body != null) {
throw new UnsupportedOperationException("Elements follow c:multipart or c:body");
}
if (XProcConstants.c_header.equals(event.getNodeName())) {
String name = event.getAttributeValue(_name);
if (name == null) {
continue; // this can't happen, right?
}
if (name.toLowerCase().equals("content-type")) {
// We'll deal with the content-type header later
headerContentType = event.getAttributeValue(_value).toLowerCase();
} else {
headers.add(new BasicHeader(event.getAttributeValue(_name), event.getAttributeValue(_value)));
}