use of org.apache.commons.httpclient.methods.PutMethod in project zm-mailbox by Zimbra.
the class UserServlet method putMailItem.
public static Pair<Header[], HttpInputStream> putMailItem(ZAuthToken authToken, String url, MailItem item) throws ServiceException, IOException {
if (item instanceof Document) {
Document doc = (Document) item;
StringBuilder u = new StringBuilder(url);
u.append("?").append(QP_AUTH).append('=').append(AUTH_COOKIE);
if (doc.getType() == MailItem.Type.WIKI) {
u.append("&fmt=wiki");
}
PutMethod method = new PutMethod(u.toString());
String contentType = doc.getContentType();
method.addRequestHeader("Content-Type", contentType);
method.setRequestEntity(new InputStreamRequestEntity(doc.getContentStream(), doc.getSize(), contentType));
method = HttpClientUtil.addInputStreamToHttpMethod(method, doc.getContentStream(), doc.getSize(), contentType);
method.addRequestHeader("X-Zimbra-Description", doc.getDescription());
method.setRequestEntity(new InputStreamRequestEntity(doc.getContentStream(), doc.getSize(), contentType));
Pair<Header[], HttpMethod> pair = doHttpOp(authToken, method);
return new Pair<Header[], HttpInputStream>(pair.getFirst(), new HttpInputStream(pair.getSecond()));
}
return putRemoteResource(authToken, url, item.getContentStream(), null);
}
use of org.apache.commons.httpclient.methods.PutMethod in project zm-mailbox by Zimbra.
the class UserServlet method doHttpOp.
private static Pair<Header[], HttpMethod> doHttpOp(ZAuthToken authToken, HttpMethod method) throws ServiceException {
// create an HTTP client with the same cookies
String url = "";
String hostname = "";
try {
url = method.getURI().toString();
hostname = method.getURI().getHost();
} catch (IOException e) {
log.warn("can't parse target URI", e);
}
HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
Map<String, String> cookieMap = authToken.cookieMap(false);
if (cookieMap != null) {
HttpState state = new HttpState();
for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
state.addCookie(new org.apache.commons.httpclient.Cookie(hostname, ck.getKey(), ck.getValue(), "/", null, false));
}
client.setState(state);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
}
if (method instanceof PutMethod) {
long contentLength = ((PutMethod) method).getRequestEntity().getContentLength();
if (contentLength > 0) {
// 100kbps in millis
int timeEstimate = Math.max(10000, (int) (contentLength / 100));
// cannot set connection time using our ZimbrahttpConnectionManager,
// see comments in ZimbrahttpConnectionManager.
// actually, length of the content to Put should not be a factor for
// establishing a connection, only read time out matter, which we set
// client.getHttpConnectionManager().getParams().setConnectionTimeout(timeEstimate);
method.getParams().setSoTimeout(timeEstimate);
}
}
try {
int statusCode = HttpClientUtil.executeMethod(client, method);
if (statusCode == HttpStatus.SC_NOT_FOUND || statusCode == HttpStatus.SC_FORBIDDEN)
throw MailServiceException.NO_SUCH_ITEM(-1);
else if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_NO_CONTENT)
throw ServiceException.RESOURCE_UNREACHABLE(method.getStatusText(), null, new ServiceException.InternalArgument(HTTP_URL, url, ServiceException.Argument.Type.STR), new ServiceException.InternalArgument(HTTP_STATUS_CODE, statusCode, ServiceException.Argument.Type.NUM));
List<Header> headers = new ArrayList<Header>(Arrays.asList(method.getResponseHeaders()));
headers.add(new Header("X-Zimbra-Http-Status", "" + statusCode));
return new Pair<Header[], HttpMethod>(headers.toArray(new Header[0]), method);
} catch (HttpException e) {
throw ServiceException.RESOURCE_UNREACHABLE("HttpException while fetching " + url, e);
} catch (IOException e) {
throw ServiceException.RESOURCE_UNREACHABLE("IOException while fetching " + url, e);
}
}
use of org.apache.commons.httpclient.methods.PutMethod in project zm-mailbox by Zimbra.
the class TestCalDav method testAppleStyleGroup.
@Test
public void testAppleStyleGroup() throws ServiceException, IOException {
Account dav1 = users[1].create();
String contactsFolderUrl = getFolderUrl(dav1, "Contacts");
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(contactsFolderUrl);
addBasicAuthHeaderForUser(postMethod, dav1);
postMethod.addRequestHeader("Content-Type", "text/vcard");
postMethod.setRequestEntity(new ByteArrayRequestEntity(rachelVcard.getBytes(), MimeConstants.CT_TEXT_VCARD));
HttpMethodExecutor.execute(client, postMethod, HttpStatus.SC_CREATED);
postMethod = new PostMethod(contactsFolderUrl);
addBasicAuthHeaderForUser(postMethod, dav1);
postMethod.addRequestHeader("Content-Type", "text/vcard");
postMethod.setRequestEntity(new ByteArrayRequestEntity(blueGroupCreate.getBytes(), MimeConstants.CT_TEXT_VCARD));
HttpMethodExecutor exe = HttpMethodExecutor.execute(client, postMethod, HttpStatus.SC_CREATED);
String groupLocation = null;
for (Header hdr : exe.respHeaders) {
if ("Location".equals(hdr.getName())) {
groupLocation = hdr.getValue();
}
}
assertNotNull("Location Header returned when creating Group", groupLocation);
postMethod = new PostMethod(contactsFolderUrl);
addBasicAuthHeaderForUser(postMethod, dav1);
postMethod.addRequestHeader("Content-Type", "text/vcard");
postMethod.setRequestEntity(new ByteArrayRequestEntity(parisVcard.getBytes(), MimeConstants.CT_TEXT_VCARD));
HttpMethodExecutor.execute(client, postMethod, HttpStatus.SC_CREATED);
String url = String.format("%s%s", contactsFolderUrl, "F53A6F96-566F-46CC-8D48-A5263FAB5E38.vcf");
PutMethod putMethod = new PutMethod(url);
addBasicAuthHeaderForUser(putMethod, dav1);
putMethod.addRequestHeader("Content-Type", "text/vcard");
putMethod.setRequestEntity(new ByteArrayRequestEntity(blueGroupModify.getBytes(), MimeConstants.CT_TEXT_VCARD));
HttpMethodExecutor.execute(client, putMethod, HttpStatus.SC_NO_CONTENT);
GetMethod getMethod = new GetMethod(url);
addBasicAuthHeaderForUser(getMethod, dav1);
getMethod.addRequestHeader("Content-Type", "text/vcard");
exe = HttpMethodExecutor.execute(client, getMethod, HttpStatus.SC_OK);
String respBody = new String(exe.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
String[] expecteds = { "X-ADDRESSBOOKSERVER-KIND:group", "X-ADDRESSBOOKSERVER-MEMBER:urn:uuid:BE43F16D-336E-4C3E-BAE6-22B8F245A986", "X-ADDRESSBOOKSERVER-MEMBER:urn:uuid:07139DE2-EA7B-46CB-A970-C4DF7F72D9AE" };
for (String expected : expecteds) {
assertTrue(String.format("GET should contain '%s'\nBODY=%s", expected, respBody), respBody.contains(expected));
}
// members are actually stored in a different way. Make sure it isn't a fluke
// that the GET response contained the correct members by checking that the members
// appear where expected in a search hit.
SearchRequest searchRequest = new SearchRequest();
searchRequest.setSortBy("dateDesc");
searchRequest.setLimit(8);
searchRequest.setSearchTypes("contact");
searchRequest.setQuery("in:Contacts");
ZMailbox mbox = users[1].getZMailbox();
SearchResponse searchResp = mbox.invokeJaxb(searchRequest);
assertNotNull("JAXB SearchResponse object", searchResp);
List<SearchHit> hits = searchResp.getSearchHits();
assertNotNull("JAXB SearchResponse hits", hits);
assertEquals("JAXB SearchResponse hits", 3, hits.size());
boolean seenGroup = false;
for (SearchHit hit : hits) {
ContactInfo contactInfo = (ContactInfo) hit;
if ("BlueGroup".equals(contactInfo.getFileAs())) {
seenGroup = true;
assertEquals("Number of members of group in search hit", 2, contactInfo.getContactGroupMembers().size());
}
ZimbraLog.test.info("Hit %s class=%s", hit, hit.getClass().getName());
}
assertTrue("Seen group", seenGroup);
}
use of org.apache.commons.httpclient.methods.PutMethod in project zm-mailbox by Zimbra.
the class TestCalDav method testAndroidMeetingSeries.
@Test
public void testAndroidMeetingSeries() throws Exception {
Account dav1 = users[1].create();
Account dav2 = users[2].create();
// Force creation of mailbox - shouldn't be needed
users[2].getZMailbox();
String calFolderUrl = getFolderUrl(dav1, "Calendar").replaceAll("@", "%40");
String url = String.format("%s%s.ics", calFolderUrl, androidSeriesMeetingUid);
HttpClient client = new HttpClient();
PutMethod putMethod = new PutMethod(url);
addBasicAuthHeaderForUser(putMethod, dav1);
putMethod.addRequestHeader("Content-Type", "text/calendar");
String body = androidSeriesMeetingTemplate.replace("%%ORG%%", dav1.getName()).replace("%%ATT%%", dav2.getName()).replace("%%UID%%", androidSeriesMeetingUid);
putMethod.setRequestEntity(new ByteArrayRequestEntity(body.getBytes(), MimeConstants.CT_TEXT_CALENDAR));
HttpMethodExecutor.execute(client, putMethod, HttpStatus.SC_CREATED);
String inboxhref = TestCalDav.waitForNewSchedulingRequestByUID(dav2, androidSeriesMeetingUid);
assertTrue("Found meeting request for newly created item", inboxhref.contains(androidSeriesMeetingUid));
GetMethod getMethod = new GetMethod(url);
addBasicAuthHeaderForUser(getMethod, dav1);
HttpMethodExecutor exe = HttpMethodExecutor.execute(client, getMethod, HttpStatus.SC_OK);
String etag = null;
for (Header hdr : exe.respHeaders) {
if (DavProtocol.HEADER_ETAG.equals(hdr.getName())) {
etag = hdr.getValue();
}
}
assertNotNull("ETag from get", etag);
// Check that we fail if the etag is wrong
putMethod = new PutMethod(url);
addBasicAuthHeaderForUser(putMethod, dav1);
putMethod.addRequestHeader("Content-Type", "text/calendar");
putMethod.addRequestHeader(DavProtocol.HEADER_IF_MATCH, "willNotMatch");
putMethod.setRequestEntity(new ByteArrayRequestEntity(body.getBytes(), MimeConstants.CT_TEXT_CALENDAR));
HttpMethodExecutor.execute(client, putMethod, HttpStatus.SC_PRECONDITION_FAILED);
PropFindMethod propFindMethod = new PropFindMethod(getFolderUrl(dav1, "Calendar"));
addBasicAuthHeaderForUser(propFindMethod, dav1);
TestCalDav.HttpMethodExecutor executor;
String respBody;
Element respElem;
propFindMethod.addRequestHeader("Content-Type", MimeConstants.CT_TEXT_XML);
propFindMethod.addRequestHeader("Depth", "1");
propFindMethod.setRequestEntity(new ByteArrayRequestEntity(propFindEtagResType.getBytes(), MimeConstants.CT_TEXT_XML));
executor = new TestCalDav.HttpMethodExecutor(client, propFindMethod, HttpStatus.SC_MULTI_STATUS);
respBody = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
respElem = Element.XMLElement.parseXML(respBody);
assertEquals("name of top element in propfind response", DavElements.P_MULTISTATUS, respElem.getName());
assertTrue("propfind response should have child elements", respElem.hasChildren());
Iterator<Element> iter = respElem.elementIterator();
boolean hasCalendarHref = false;
boolean hasCalItemHref = false;
while (iter.hasNext()) {
Element child = iter.next();
if (DavElements.P_RESPONSE.equals(child.getName())) {
Iterator<Element> hrefIter = child.elementIterator(DavElements.P_HREF);
while (hrefIter.hasNext()) {
Element href = hrefIter.next();
calFolderUrl.endsWith(href.getText());
hasCalendarHref = hasCalendarHref || calFolderUrl.endsWith(href.getText());
hasCalItemHref = hasCalItemHref || url.endsWith(href.getText());
}
}
}
assertTrue("propfind response contained entry for calendar", hasCalendarHref);
assertTrue("propfind response contained entry for calendar entry ", hasCalItemHref);
DeleteMethod deleteMethod = new DeleteMethod(url);
addBasicAuthHeaderForUser(deleteMethod, dav1);
HttpMethodExecutor.execute(client, deleteMethod, HttpStatus.SC_NO_CONTENT);
}
use of org.apache.commons.httpclient.methods.PutMethod in project zm-mailbox by Zimbra.
the class TestCalDav method doIcalPut.
public HttpMethodExecutor doIcalPut(String url, Account authAcct, byte[] vcalendar, int expected) throws IOException {
HttpClient client = new HttpClient();
PutMethod putMethod = new PutMethod(url);
addBasicAuthHeaderForUser(putMethod, authAcct);
putMethod.addRequestHeader("Content-Type", "text/calendar");
putMethod.setRequestEntity(new ByteArrayRequestEntity(vcalendar, MimeConstants.CT_TEXT_CALENDAR));
return HttpMethodExecutor.execute(client, putMethod, expected);
}
Aggregations