use of org.apache.commons.httpclient.Cookie in project jaxrs-api by eclipse-ee4j.
the class AdaptiveHttpRequest method createCookie.
/*
* private methods
* ========================================================================
*/
private void createCookie(String cookieHeader) {
String cookieLine = cookieHeader.substring(cookieHeader.indexOf(':') + 1).trim();
StringTokenizer st = new StringTokenizer(cookieLine, " ;");
Cookie cookie = new Cookie();
cookie.setVersion(1);
getState();
if (cookieLine.indexOf("$Version") == -1) {
cookie.setVersion(0);
_method.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);
}
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.charAt(0) != '$' && !token.startsWith("Domain") && !token.startsWith("Path")) {
cookie.setName(token.substring(0, token.indexOf('=')));
cookie.setValue(token.substring(token.indexOf('=') + 1));
} else if (token.indexOf("Domain") > -1) {
cookie.setDomainAttributeSpecified(true);
cookie.setDomain(token.substring(token.indexOf('=') + 1));
} else if (token.indexOf("Path") > -1) {
cookie.setPathAttributeSpecified(true);
cookie.setPath(token.substring(token.indexOf('=') + 1));
}
}
_state.addCookie(cookie);
}
use of org.apache.commons.httpclient.Cookie in project zm-mailbox by Zimbra.
the class SoapDebugListener method sendSoapMessage.
@Override
public void sendSoapMessage(PostMethod postMethod, Element envelope, HttpState httpState) {
if (level == Level.OFF) {
return;
}
printer.println();
printer.println("=== Request ===");
if (Level.needsHeader(level)) {
try {
URI uri = postMethod.getURI();
printer.println(uri.toString());
} catch (URIException e) {
e.printStackTrace();
}
// headers
Header[] headers = postMethod.getRequestHeaders();
for (Header header : headers) {
// trim the ending crlf
printer.println(header.toString().trim());
}
printer.println();
//cookies
if (httpState != null) {
Cookie[] cookies = httpState.getCookies();
for (Cookie cookie : cookies) {
printer.println("Cookie: " + cookie.toString());
}
}
printer.println();
}
if (Level.needsBody(level)) {
printer.println(envelope.prettyPrint());
}
}
use of org.apache.commons.httpclient.Cookie in project zm-mailbox by Zimbra.
the class ZimbraServlet method proxyServletRequest.
public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, HttpMethod method, HttpState state) throws IOException, ServiceException {
// create an HTTP client with the same cookies
javax.servlet.http.Cookie[] cookies = req.getCookies();
String hostname = method.getURI().getHost();
boolean hasZMAuth = hasZimbraAuthCookie(state);
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN) && hasZMAuth)
continue;
state.addCookie(new Cookie(hostname, cookies[i].getName(), cookies[i].getValue(), "/", null, false));
}
}
HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
if (state != null)
client.setState(state);
int hopcount = 0;
for (Enumeration<?> enm = req.getHeaderNames(); enm.hasMoreElements(); ) {
String hname = (String) enm.nextElement(), hlc = hname.toLowerCase();
if (hlc.equals("x-zimbra-hopcount"))
try {
hopcount = Math.max(Integer.parseInt(req.getHeader(hname)), 0);
} catch (NumberFormatException e) {
}
else if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.equals("authorization"))
method.addRequestHeader(hname, req.getHeader(hname));
}
if (hopcount >= MAX_PROXY_HOPCOUNT)
throw ServiceException.TOO_MANY_HOPS(HttpUtil.getFullRequestURL(req));
method.addRequestHeader("X-Zimbra-Hopcount", Integer.toString(hopcount + 1));
if (method.getRequestHeader("X-Zimbra-Orig-Url") == null)
method.addRequestHeader("X-Zimbra-Orig-Url", req.getRequestURL().toString());
String ua = req.getHeader("User-Agent");
if (ua != null)
method.setRequestHeader("User-Agent", ua);
// dispatch the request and copy over the results
int statusCode = -1;
for (int retryCount = 3; statusCode == -1 && retryCount > 0; retryCount--) {
statusCode = HttpClientUtil.executeMethod(client, method);
}
if (statusCode == -1) {
resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "retry limit reached");
return;
} else if (statusCode >= 300) {
resp.sendError(statusCode, method.getStatusText());
return;
}
Header[] headers = method.getResponseHeaders();
for (int i = 0; i < headers.length; i++) {
String hname = headers[i].getName(), hlc = hname.toLowerCase();
if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.startsWith("www-"))
resp.addHeader(hname, headers[i].getValue());
}
InputStream responseStream = method.getResponseBodyAsStream();
if (responseStream == null || resp.getOutputStream() == null)
return;
ByteUtil.copy(method.getResponseBodyAsStream(), false, resp.getOutputStream(), false);
}
use of org.apache.commons.httpclient.Cookie in project iaf by ibissource.
the class SsoUtil method addSsoCredential.
public static void addSsoCredential(HttpMethod method, HttpState state, String defaultForwardHost) {
try {
String name = SsoUtil.getSsoTokenName();
String value = SsoUtil.getSsoToken();
if (StringUtils.isEmpty(value)) {
if (log.isDebugEnabled())
log.debug("no value for SsoCredential [" + name + "]");
} else {
if (log.isDebugEnabled())
log.debug("constructing SsoCredentialCookie [" + name + "]");
Cookie ssoCookie = new Cookie();
ssoCookie.setName(name);
ssoCookie.setValue(value);
String forwardHost;
try {
URI uri = method.getURI();
forwardHost = uri.getHost();
if (StringUtils.isEmpty(forwardHost)) {
if (log.isDebugEnabled())
log.debug("did not find host from URI [" + uri.getURI() + "], will use default [" + defaultForwardHost + "] for SSO credential cookie");
forwardHost = defaultForwardHost;
}
} catch (Throwable t) {
log.warn("could not extract host from URI", t);
forwardHost = defaultForwardHost;
}
ssoCookie.setDomain(forwardHost);
// path must have a value, otherwise cookie is not appended to request
ssoCookie.setPath("/");
if (log.isDebugEnabled())
log.debug("set SSOcookie attributes: domain [" + ssoCookie.getDomain() + "] path [" + ssoCookie.getPath() + "]");
state.addCookie(ssoCookie);
}
} catch (Exception e) {
log.warn("could not obtain SsoToken: " + e.getMessage());
}
}
use of org.apache.commons.httpclient.Cookie in project ecf by eclipse.
the class PHPBBCookies method detectCookies.
public static Map<String, String> detectCookies(Cookie[] cookies) {
Map<String, String> detected = new HashMap<String, String>();
for (Cookie cookie : cookies) {
if (PAT_SESS_ID.matcher(cookie.getValue()).matches()) {
// detected session id
detected.put(KEY_SESS_ID, cookie.getValue());
} else {
// try to detect user id
try {
String value = URLDecoder.decode(cookie.getValue(), "UTF-8");
Map<String, String> map = PHPUtil.deserializeStringArray(value);
if (map != null && map.containsKey("userid")) {
detected.put(KEY_USER_ID, map.get("userid"));
}
} catch (UnsupportedEncodingException e) {
// do nothing
}
}
}
return detected;
}
Aggregations