Example usage for org.apache.commons.httpclient.util URIUtil encodePath

List of usage examples for org.apache.commons.httpclient.util URIUtil encodePath

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.util URIUtil encodePath.

Prototype

public static String encodePath(String unescaped) throws URIException 

Source Link

Document

Escape and encode a string regarded as the path component of an URI with the default protocol charset.

Usage

From source file:davmail.http.DavGatewayHttpClientFacade.java

private static String getLocationValue(HttpMethod method) throws URIException {
    String locationValue = null;//from  w  ww  .ja  va  2  s  .  c  om
    Header location = method.getResponseHeader("Location");
    if (location != null && isRedirect(method.getStatusCode())) {
        locationValue = location.getValue();
        // Novell iChain workaround
        if (locationValue.indexOf('"') >= 0) {
            locationValue = URIUtil.encodePath(locationValue);
        }
        // workaround for invalid relative location
        if (locationValue.startsWith("./")) {
            locationValue = locationValue.substring(1);
        }
    }
    return locationValue;
}

From source file:davmail.caldav.TestCaldav.java

public void testRenameCalendar() throws IOException {
    String folderName = "testcalendarfolder";
    String encodedFolderpath = URIUtil
            .encodePath("/users/" + session.getEmail() + "/calendar/" + folderName + '/');
    // first delete calendar
    session.deleteFolder("calendar/" + folderName);
    String body = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + "   <C:mkcalendar xmlns:D=\"DAV:\"\n"
            + "                 xmlns:C=\"urn:ietf:params:xml:ns:caldav\">\n" + "     <D:set>\n"
            + "       <D:prop>\n" + "         <D:displayname>" + StringUtil.xmlEncode(folderName)
            + "</D:displayname>\n"
            + "         <C:calendar-description xml:lang=\"en\">Calendar description</C:calendar-description>\n"
            + "         <C:supported-calendar-component-set>\n" + "           <C:comp name=\"VEVENT\"/>\n"
            + "         </C:supported-calendar-component-set>\n" + "       </D:prop>\n" + "     </D:set>\n"
            + "   </C:mkcalendar>";

    SearchReportMethod method = new SearchReportMethod(encodedFolderpath, body) {
        @Override//from   w  w  w . j a v  a2 s.  c  om
        public String getName() {
            return "MKCALENDAR";
        }
    };
    httpClient.executeMethod(method);
    assertEquals(HttpStatus.SC_CREATED, method.getStatusCode());
    MoveMethod moveMethod = new MoveMethod(encodedFolderpath,
            "http://localhost:" + Settings.getProperty("davmail.caldavPort") + "/users/" + session.getEmail()
                    + "/movedcalendarfolder",
            true);
    httpClient.executeMethod(moveMethod);
}

From source file:davmail.exchange.dav.DavExchangeSession.java

protected void getWellKnownFolders() throws DavMailException {
    // Retrieve well known URLs
    MultiStatusResponse[] responses;/*from   w w  w .  j  av a2 s.  co  m*/
    try {
        responses = DavGatewayHttpClientFacade.executePropFindMethod(httpClient, URIUtil.encodePath(mailPath),
                0, WELL_KNOWN_FOLDERS);
        if (responses.length == 0) {
            throw new WebdavNotAvailableException("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath);
        }
        DavPropertySet properties = responses[0].getProperties(HttpStatus.SC_OK);
        inboxUrl = getURIPropertyIfExists(properties, "inbox");
        inboxName = getFolderName(inboxUrl);
        deleteditemsUrl = getURIPropertyIfExists(properties, "deleteditems");
        deleteditemsName = getFolderName(deleteditemsUrl);
        sentitemsUrl = getURIPropertyIfExists(properties, "sentitems");
        sentitemsName = getFolderName(sentitemsUrl);
        sendmsgUrl = getURIPropertyIfExists(properties, "sendmsg");
        sendmsgName = getFolderName(sendmsgUrl);
        draftsUrl = getURIPropertyIfExists(properties, "drafts");
        draftsName = getFolderName(draftsUrl);
        calendarUrl = getURIPropertyIfExists(properties, "calendar");
        calendarName = getFolderName(calendarUrl);
        tasksUrl = getURIPropertyIfExists(properties, "tasks");
        tasksName = getFolderName(tasksUrl);
        contactsUrl = getURIPropertyIfExists(properties, "contacts");
        contactsName = getFolderName(contactsUrl);
        outboxUrl = getURIPropertyIfExists(properties, "outbox");
        outboxName = getFolderName(outboxUrl);
        // junk folder not available over webdav

        LOGGER.debug("Inbox URL: " + inboxUrl + " Trash URL: " + deleteditemsUrl + " Sent URL: " + sentitemsUrl
                + " Send URL: " + sendmsgUrl + " Drafts URL: " + draftsUrl + " Calendar URL: " + calendarUrl
                + " Tasks URL: " + tasksUrl + " Contacts URL: " + contactsUrl + " Outbox URL: " + outboxUrl
                + " Public folder URL: " + publicFolderUrl);
    } catch (IOException e) {
        LOGGER.error(e.getMessage());
        throw new WebdavNotAvailableException("EXCEPTION_UNABLE_TO_GET_MAIL_FOLDER", mailPath);
    }
}

From source file:davmail.exchange.dav.DavExchangeSession.java

/**
 * @inheritDoc/*from www  .ja  va 2 s  . c  o  m*/
 */
@Override
protected Folder internalGetFolder(String folderPath) throws IOException {
    MultiStatusResponse[] responses = DavGatewayHttpClientFacade.executePropFindMethod(httpClient,
            URIUtil.encodePath(getFolderPath(folderPath)), 0, FOLDER_PROPERTIES_NAME_SET);
    Folder folder = null;
    if (responses.length > 0) {
        folder = buildFolder(responses[0]);
        folder.folderPath = folderPath;
    }
    return folder;
}

From source file:davmail.exchange.dav.DavExchangeSession.java

/**
 * @inheritDoc/*from   ww  w.  jav  a2  s. com*/
 */
@Override
public int createFolder(String folderPath, String folderClass, Map<String, String> properties)
        throws IOException {
    Set<PropertyValue> propertyValues = new HashSet<PropertyValue>();
    if (properties != null) {
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            propertyValues.add(Field.createPropertyValue(entry.getKey(), entry.getValue()));
        }
    }
    propertyValues.add(Field.createPropertyValue("folderclass", folderClass));

    // standard MkColMethod does not take properties, override PropPatchMethod instead
    ExchangePropPatchMethod method = new ExchangePropPatchMethod(URIUtil.encodePath(getFolderPath(folderPath)),
            propertyValues) {
        @Override
        public String getName() {
            return "MKCOL";
        }
    };
    int status = DavGatewayHttpClientFacade.executeHttpMethod(httpClient, method);
    if (status == HttpStatus.SC_MULTI_STATUS) {
        status = method.getResponseStatusCode();
    }
    return status;
}

From source file:davmail.exchange.dav.DavExchangeSession.java

/**
 * @inheritDoc//from  www  .  j a v  a  2s. com
 */
@Override
public int updateFolder(String folderPath, Map<String, String> properties) throws IOException {
    Set<PropertyValue> propertyValues = new HashSet<PropertyValue>();
    if (properties != null) {
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            propertyValues.add(Field.createPropertyValue(entry.getKey(), entry.getValue()));
        }
    }

    // standard MkColMethod does not take properties, override PropPatchMethod instead
    ExchangePropPatchMethod method = new ExchangePropPatchMethod(URIUtil.encodePath(getFolderPath(folderPath)),
            propertyValues);
    int status = DavGatewayHttpClientFacade.executeHttpMethod(httpClient, method);
    if (status == HttpStatus.SC_MULTI_STATUS) {
        status = method.getResponseStatusCode();
    }
    return status;
}

From source file:davmail.exchange.dav.DavExchangeSession.java

/**
 * @inheritDoc//from   www  .  ja  va 2 s  . c o  m
 */
@Override
public void deleteFolder(String folderPath) throws IOException {
    DavGatewayHttpClientFacade.executeDeleteMethod(httpClient, URIUtil.encodePath(getFolderPath(folderPath)));
}

From source file:davmail.exchange.dav.DavExchangeSession.java

/**
 * @inheritDoc/* www  .j  av  a2s  . co m*/
 */
@Override
public void moveFolder(String folderPath, String targetPath) throws IOException {
    MoveMethod method = new MoveMethod(URIUtil.encodePath(getFolderPath(folderPath)),
            URIUtil.encodePath(getFolderPath(targetPath)), false);
    try {
        int statusCode = httpClient.executeMethod(method);
        if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
            throw new HttpPreconditionFailedException(BundleMessage.format("EXCEPTION_UNABLE_TO_MOVE_FOLDER"));
        } else if (statusCode != HttpStatus.SC_CREATED) {
            throw DavGatewayHttpClientFacade.buildHttpException(method);
        } else if (folderPath.equalsIgnoreCase("/users/" + getEmail() + "/calendar")) {
            // calendar renamed, need to reload well known folders 
            getWellKnownFolders();
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:davmail.exchange.dav.DavExchangeSession.java

/**
 * @inheritDoc/* w w  w .  j  a v a  2  s.  c o  m*/
 */
@Override
public void moveItem(String sourcePath, String targetPath) throws IOException {
    MoveMethod method = new MoveMethod(URIUtil.encodePath(getFolderPath(sourcePath)),
            URIUtil.encodePath(getFolderPath(targetPath)), false);
    moveItem(method);
}

From source file:davmail.exchange.dav.DavExchangeSession.java

@Override
public Item getItem(String folderPath, String itemName) throws IOException {
    String emlItemName = convertItemNameToEML(itemName);
    String itemPath = getFolderPath(folderPath) + '/' + emlItemName;
    MultiStatusResponse[] responses = null;
    try {/*from   w  w w.  jav  a2  s.  c om*/
        try {
            responses = DavGatewayHttpClientFacade.executePropFindMethod(httpClient,
                    URIUtil.encodePath(itemPath), 0, EVENT_REQUEST_PROPERTIES_NAME_SET);
        } catch (HttpNotFoundException e) {
            // ignore
        }
        if (responses == null || responses.length == 0 && isMainCalendar(folderPath)) {
            if (itemName.endsWith(".ics")) {
                itemName = itemName.substring(0, itemName.length() - 3) + "EML";
            }
            // look for item in tasks folder
            responses = DavGatewayHttpClientFacade.executePropFindMethod(httpClient,
                    URIUtil.encodePath(getFolderPath(TASKS) + '/' + emlItemName), 0,
                    EVENT_REQUEST_PROPERTIES_NAME_SET);
        }
        if (responses == null || responses.length == 0) {
            throw new HttpNotFoundException(itemPath + " not found");
        }
    } catch (HttpNotFoundException e) {
        try {
            LOGGER.debug(itemPath + " not found, searching by urlcompname");
            // failover: try to get event by displayname
            responses = searchItems(folderPath, EVENT_REQUEST_PROPERTIES, isEqualTo("urlcompname", emlItemName),
                    FolderQueryTraversal.Shallow, 1);
            if (responses.length == 0 && isMainCalendar(folderPath)) {
                responses = searchItems(TASKS, EVENT_REQUEST_PROPERTIES, isEqualTo("urlcompname", emlItemName),
                        FolderQueryTraversal.Shallow, 1);
            }
            if (responses.length == 0) {
                throw new HttpNotFoundException(itemPath + " not found");
            }
        } catch (HttpNotFoundException e2) {
            LOGGER.debug("last failover: search all items");
            List<ExchangeSession.Event> events = getAllEvents(folderPath);
            for (ExchangeSession.Event event : events) {
                if (itemName.equals(event.getName())) {
                    responses = DavGatewayHttpClientFacade.executePropFindMethod(httpClient,
                            encodeAndFixUrl(((DavExchangeSession.Event) event).getPermanentUrl()), 0,
                            EVENT_REQUEST_PROPERTIES_NAME_SET);
                    break;
                }
            }
            if (responses == null || responses.length == 0) {
                throw new HttpNotFoundException(itemPath + " not found");
            }
            LOGGER.warn("search by urlcompname failed, actual value is "
                    + getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "urlcompname"));
        }
    }
    // build item
    String contentClass = getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "contentclass");
    String urlcompname = getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "urlcompname");
    if ("urn:content-classes:person".equals(contentClass)) {
        // retrieve Contact properties
        List<ExchangeSession.Contact> contacts = searchContacts(folderPath, CONTACT_ATTRIBUTES,
                isEqualTo("urlcompname", StringUtil.decodeUrlcompname(urlcompname)), 1);
        if (contacts.isEmpty()) {
            LOGGER.warn("Item found, but unable to build contact");
            throw new HttpNotFoundException(itemPath + " not found");
        }
        return contacts.get(0);
    } else if ("urn:content-classes:appointment".equals(contentClass)
            || "urn:content-classes:calendarmessage".equals(contentClass)
            || "urn:content-classes:task".equals(contentClass)) {
        return new Event(responses[0]);
    } else {
        LOGGER.warn("wrong contentclass on item " + itemPath + ": " + contentClass);
        // return item anyway
        return new Event(responses[0]);
    }

}