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:org.nuxeo.ecm.webdav.resource.FolderResource.java

@PROPFIND
public Response propfind(@Context UriInfo uriInfo, @HeaderParam("depth") String depth) throws Exception {

    if (depth == null) {
        depth = "1";
    }/* w  ww. j  av a  2 s  . c om*/

    Unmarshaller u = Util.getUnmarshaller();

    Prop prop = null;
    if (request.getInputStream() != null && request.getContentLength() > 0) {
        PropFind propFind;
        try {
            propFind = (PropFind) u.unmarshal(request.getInputStream());
        } catch (JAXBException e) {
            log.error(e);
            // FIXME: check this is the right response code
            return Response.status(400).build();
        }
        prop = propFind.getProp();
        // Util.printAsXml(prop);
    }

    // Get key properties from doc
    Date lastModified = getTimePropertyWrapper(doc, "dc:modified");
    Date creationDate = getTimePropertyWrapper(doc, "dc:created");

    final net.java.dev.webdav.jaxrs.xml.elements.Response response = new net.java.dev.webdav.jaxrs.xml.elements.Response(
            new HRef(uriInfo.getRequestUri()), null, null, null,
            new PropStat(
                    new Prop(new DisplayName("nuxeo"), new LockDiscovery(), new SupportedLock(),
                            new IsFolder("t"), new IsCollection(1), new IsHidden(0),
                            new GetContentType("application/octet-stream"), new GetContentLength(0),
                            new CreationDate(creationDate), new GetLastModified(lastModified), COLLECTION),
                    new Status(OK)));

    if (!doc.isFolder() || depth.equals("0")) {
        return Response.status(207).entity(new MultiStatus(response)).build();
    }

    List<net.java.dev.webdav.jaxrs.xml.elements.Response> responses = new ArrayList<net.java.dev.webdav.jaxrs.xml.elements.Response>();
    responses.add(response);

    List<DocumentModel> children = backend.getChildren(doc.getRef());
    for (DocumentModel child : children) {
        lastModified = getTimePropertyWrapper(child, "dc:modified");
        creationDate = getTimePropertyWrapper(child, "dc:created");
        String childName = URIUtil.encodePath(backend.getDisplayName(child));
        PropStatBuilderExt props = new PropStatBuilderExt();
        props.lastModified(lastModified).creationDate(creationDate).displayName(childName).status(OK);
        if (child.isFolder()) {
            props.isCollection();
        } else {
            String mimeType = "application/octet-stream";
            long size = 0;
            BlobHolder bh = child.getAdapter(BlobHolder.class);
            if (bh != null) {
                try {
                    Blob blob = bh.getBlob();
                    if (blob != null) {
                        size = blob.getLength();
                        mimeType = blob.getMimeType();
                    }
                } catch (ClientException e) {
                    log.error("Unable to get blob Size", e);
                }
            }
            if (StringUtils.isEmpty(mimeType) || "???".equals(mimeType)) {
                mimeType = "application/octet-stream";
            }
            props.isResource(size, mimeType);
        }

        PropStat found = props.build();
        PropStat notFound = null;
        if (prop != null) {
            // props.isHidden(false);
            // props.lastAccessed(lastModified);
            notFound = props.notFound(prop);
        }

        net.java.dev.webdav.jaxrs.xml.elements.Response childResponse;
        URI childUri = uriInfo.getRequestUriBuilder().path(childName).build();
        if (notFound != null) {
            childResponse = new net.java.dev.webdav.jaxrs.xml.elements.Response(new HRef(childUri), null, null,
                    null, found, notFound);
        } else {
            childResponse = new net.java.dev.webdav.jaxrs.xml.elements.Response(new HRef(childUri), null, null,
                    null, found);
        }

        responses.add(childResponse);
    }

    MultiStatus st = new MultiStatus(
            responses.toArray(new net.java.dev.webdav.jaxrs.xml.elements.Response[responses.size()]));
    //printXml(st);
    return Response.status(207).entity(st).build();
}

From source file:org.pengyou.client.lib.DavResource.java

public void setPath(String path) {
    path = ((path.length() > 0) && path.charAt(0) == '/' ? path.substring(1) : path);
    try {//w  w  w. j  a  va2s.  c  o m
        this.path = URIUtil.encodePath(path);
    } catch (URIException e) {
        this.path = path;
        log.warn("can't encode the path ", e);
    }
}

From source file:org.pentaho.di.core.refinery.model.ModelServerFetcher.java

public String downloadAnalysisFile(String analysisId)
        throws KettleException, AuthorizationException, ServerException, URIException {
    String encodedId = URIUtil.encodePath(analysisId);
    ClientResponse response = getResource(DataSourceType.ANALYSIS.getDownloadPath(encodedId))
            .get(ClientResponse.class);
    if (isSuccess(response)) {
        if (response.getType().toString().equals("application/zip")) {
            try (ZipInputStream zipInputStream = extractFromZip("schema.xml", response)) {
                return IOUtils.toString(zipInputStream);
            } catch (IOException e) {
                throw new KettleException(e);
            }//from   www  .  j  a v  a 2 s . c o  m
        } else {
            return response.getEntity(String.class);
        }
    } else {
        switch (response.getStatus()) {
        case 401:
            throw new AuthorizationException();
        case 500:
        default:
            throw new ServerException();
        }
    }
}

From source file:org.pentaho.di.core.refinery.model.ModelServerFetcher.java

/**
 * Fetches and parses a DSW model//  ww w . j ava 2 s. c o  m
 * @param dswId
 * @return
 * @throws AuthorizationException 
 * @throws ServerException 
 */
public Domain downloadDswFile(String dswId)
        throws KettleException, AuthorizationException, ServerException, URIException {
    String encodedId = URIUtil.encodePath(dswId);
    ClientResponse response = getResource(DataSourceType.DSW.getDownloadPath(encodedId))
            .get(ClientResponse.class);
    if (isSuccess(response)) {
        try (ZipInputStream zipInputStream = extractFromZip(dswId, response)) {
            XmiParser parser = new XmiParser();
            return parser.parseXmi(zipInputStream);
        } catch (Exception e) {
            throw new KettleException(e);
        }
    } else {
        switch (response.getStatus()) {
        case 401:
            throw new AuthorizationException();
        case 500:
        default:
            throw new ServerException();
        }
    }
}

From source file:org.sonatype.flexmojos.htmlwrapper.HtmlWrapperMojo.java

private void extractTemplate() throws MojoExecutionException {
    getLog().info("Extracting template");
    templateOutputDirectory.mkdirs();//from ww  w  . ja  v a  2  s .c  o m

    URI uri;
    try {
        if (MavenUtils.isWindows()) {
            // Shake bars to avoid URI syntax problems
            templateURI = templateURI.replace('\\', '/');
        }
        templateURI = URIUtil.encodePath(templateURI);
        uri = new URI(templateURI);
    } catch (Exception e) {
        throw new MojoExecutionException("Invalid template URI.", e);
    }

    String scheme = uri.getScheme();
    if ("embed".equals(scheme)) {
        copyEmbedTemplate(uri.getSchemeSpecificPart());
    } else if ("zip".equals(scheme)) {
        copyZipTemplate(uri.getSchemeSpecificPart());
    } else if ("folder".equals(scheme)) {
        copyFolderTemplate(uri.getSchemeSpecificPart());
    } else {
        throw new MojoExecutionException("Invalid URI scheme: " + scheme);
    }

}

From source file:org.uberfire.backend.server.util.Paths.java

public org.kie.commons.java.nio.file.Path convert(final Path path) {
    if (path == null) {
        return null;
    }/*from  w w  w  .ja va  2  s.  com*/

    try {
        return ioService.get(URI.create(path.toURI()));
    } catch (IllegalArgumentException e) {
        try {
            return ioService.get(URI.create(URIUtil.encodePath(path.toURI())));
        } catch (URIException ex) {
            return null;
        }
    }
}

From source file:org.uberfire.java.nio.base.AbstractPath.java

private String encodePath(final String s) {
    try {/*from   w w w.jav  a2s. c  om*/
        return URIUtil.encodePath(s);
    } catch (final URIException e) {
    }
    return null;
}

From source file:org.xwiki.rest.internal.DomainObjectFactory.java

/**
 * Create attachment URI with correctly encoded path portions.
 *
 * @param useVersion Signal to generate a Uri at the attachment version number
 *///ww  w  .  j a  v  a  2s.c  om
protected static String createAttachmentUri(URI baseUri, com.xpn.xwiki.api.Attachment xwikiAttachment,
        Document doc, boolean useVersion) throws IllegalArgumentException {
    String attachmentUri = "";

    try {
        String eWiki = URIUtil.encodePath(doc.getWiki());
        String eSpaceName = URIUtil.encodePath(doc.getSpace());
        String eDocName = URIUtil.encodePath(doc.getName());
        String eAttachName = URIUtil.encodePath(xwikiAttachment.getFilename());
        String eVersionNum = URIUtil.encodePath(xwikiAttachment.getVersion());

        java.lang.Class<?> pathClass = (useVersion) ? AttachmentVersionResource.class
                : AttachmentResource.class;

        UriBuilder uriBuilder = UriBuilder.fromUri(baseUri).path(pathClass);

        if (useVersion) {
            attachmentUri = uriBuilder.buildFromEncoded(eWiki, eSpaceName, eDocName, eAttachName, eVersionNum)
                    .toString();
        } else {
            attachmentUri = uriBuilder.buildFromEncoded(eWiki, eSpaceName, eDocName, eAttachName).toString();
        }
    } catch (URIException ue) {
        throw new IllegalArgumentException("could not create attachmentUri", ue);
    }
    return attachmentUri;
}