use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.
the class ExchangeFreeBusyProvider method basicAuth.
private boolean basicAuth(HttpClient client, ServerInfo info) {
HttpState state = new HttpState();
Credentials cred = new UsernamePasswordCredentials(info.authUsername, info.authPassword);
state.setCredentials(AuthScope.ANY, cred);
client.setState(state);
ArrayList<String> authPrefs = new ArrayList<String>();
authPrefs.add(AuthPolicy.BASIC);
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
return true;
}
use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.
the class ExchangeFreeBusyProvider method formAuth.
private boolean formAuth(HttpClient client, ServerInfo info) throws IOException {
StringBuilder buf = new StringBuilder();
buf.append("destination=");
buf.append(URLEncoder.encode(info.url, "UTF-8"));
buf.append("&username=");
buf.append(info.authUsername);
buf.append("&password=");
buf.append(URLEncoder.encode(info.authPassword, "UTF-8"));
buf.append("&flags=0");
buf.append("&SubmitCreds=Log On");
buf.append("&trusted=0");
String url = info.url + LC.calendar_exchange_form_auth_url.value();
PostMethod method = new PostMethod(url);
ByteArrayRequestEntity re = new ByteArrayRequestEntity(buf.toString().getBytes(), "x-www-form-urlencoded");
method.setRequestEntity(re);
HttpState state = new HttpState();
client.setState(state);
try {
int status = HttpClientUtil.executeMethod(client, method);
if (status >= 400) {
ZimbraLog.fb.error("form auth to Exchange returned an error: " + status);
return false;
}
} finally {
method.releaseConnection();
}
return true;
}
use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.
the class TestCollectConfigServletsAccess method testLDAPConfigDelegatedAdmin.
/**
* Verify that global admin can access servlet at /service/collectldapconfig/
* @throws Exception
*/
/* @Test
public void testLDAPConfigGlobalAdmin() throws Exception {
ZAuthToken at = TestUtil.getAdminSoapTransport().getAuthToken();
URI servletURI = new URI(getLDAPConfigServletUrl());
HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
restClient.setState(initialState);
restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
GetMethod get = new GetMethod(servletURI.toString());
int statusCode = HttpClientUtil.executeMethod(restClient, get);
if(statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
fail("collectldapconfig servlet is failing. Likely Zimbra SSH access is not properly configured. " + get.getResponseHeader("X-Zimbra-Fault-Message").getValue());
} else {
assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK,statusCode);
}
}*/
/**
* Verify that delegated admin canNOT access servlet at /service/collectldapconfig/
* @throws Exception
*/
@Test
public void testLDAPConfigDelegatedAdmin() throws Exception {
ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
URI servletURI = new URI(getLDAPConfigServletUrl());
HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
restClient.setState(initialState);
restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
GetMethod get = new GetMethod(servletURI.toString());
int statusCode = HttpClientUtil.executeMethod(restClient, get);
assertEquals("This request should NOT succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.
the class TestCollectConfigServletsAccess method testConfigDelegatedAdmin.
/**
* Verify that global admin can access servlet at /service/collectconfig/
* @throws Exception
*/
/* @Test
public void testConfigGlobalAdmin() throws Exception {
ZAuthToken at = TestUtil.getAdminSoapTransport().getAuthToken();
URI servletURI = new URI(getConfigServletUrl());
HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
restClient.setState(initialState);
restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
GetMethod get = new GetMethod(servletURI.toString());
int statusCode = HttpClientUtil.executeMethod(restClient, get);
if(statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
fail("collectconfig servlet is failing. Likely Zimbra SSH access is not properly configured. " + get.getResponseHeader("X-Zimbra-Fault-Message").getValue());
} else {
assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK,statusCode);
}
}*/
/**
* Verify that delegated admin canNOT access servlet at /service/collectconfig/
* @throws Exception
*/
@Test
public void testConfigDelegatedAdmin() throws Exception {
ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
URI servletURI = new URI(getConfigServletUrl());
HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
restClient.setState(initialState);
restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
GetMethod get = new GetMethod(servletURI.toString());
int statusCode = HttpClientUtil.executeMethod(restClient, get);
assertEquals("This request should NOT succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.
the class StatsImageServlet method doGet.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
AuthToken authToken = getAdminAuthTokenFromCookie(req, resp);
if (authToken == null)
return;
String imgName = null;
InputStream is = null;
boolean imgAvailable = true;
boolean localServer = false;
boolean systemWide = false;
String serverAddr = "";
String noDefaultImg = req.getParameter("nodef");
boolean noDefault = false;
if (noDefaultImg != null && !noDefaultImg.equals("") && noDefaultImg.equals("1")) {
noDefault = true;
}
String reqPath = req.getRequestURI();
try {
//check if this is the logger host, otherwise proxy the request to the logger host
String serviceHostname = Provisioning.getInstance().getLocalServer().getAttr(Provisioning.A_zimbraServiceHostname);
String logHost = Provisioning.getInstance().getConfig().getAttr(Provisioning.A_zimbraLogHostname);
if (!serviceHostname.equalsIgnoreCase(logHost)) {
StringBuffer url = new StringBuffer("https");
url.append("://").append(logHost).append(':').append(LC.zimbra_admin_service_port.value());
url.append(reqPath);
String queryStr = req.getQueryString();
if (queryStr != null)
url.append('?').append(queryStr);
// create an HTTP client with the same cookies
HttpState state = new HttpState();
try {
state.addCookie(new org.apache.commons.httpclient.Cookie(logHost, ZimbraCookie.COOKIE_ZM_ADMIN_AUTH_TOKEN, authToken.getEncoded(), "/", null, false));
} catch (AuthTokenException ate) {
throw ServiceException.PROXY_ERROR(ate, url.toString());
}
HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
client.setState(state);
GetMethod get = new GetMethod(url.toString());
try {
int statusCode = HttpClientUtil.executeMethod(client, get);
if (statusCode != HttpStatus.SC_OK)
throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), null);
resp.setContentType("image/gif");
ByteUtil.copy(get.getResponseBodyAsStream(), true, resp.getOutputStream(), false);
return;
} catch (HttpException e) {
throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), e);
} catch (IOException e) {
throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), e);
} finally {
get.releaseConnection();
}
}
} catch (Exception ex) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Image not found");
return;
}
try {
if (reqPath == null || reqPath.length() == 0) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (mLog.isDebugEnabled())
mLog.debug("received request to:(" + reqPath + ")");
String[] reqParts = reqPath.split("/");
String reqFilename = reqParts[3];
imgName = LC.stats_img_folder.value() + File.separator + reqFilename;
try {
is = new FileInputStream(imgName);
} catch (FileNotFoundException ex) {
//unlikely case - only if the server's files are broken
if (is != null)
is.close();
if (!noDefault) {
imgName = LC.stats_img_folder.value() + File.separator + IMG_NOT_AVAIL;
is = new FileInputStream(imgName);
} else {
resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Image not found");
return;
}
}
} catch (Exception ex) {
if (is != null)
is.close();
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "FNF image File not found");
return;
}
resp.setContentType("image/gif");
ByteUtil.copy(is, true, resp.getOutputStream(), false);
}
Aggregations