RepositoryAddress.java 7.18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Credentials
 *  javax.jcr.SimpleCredentials
 */
package com.day.jcr.vault.fs.api;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.BitSet;
import javax.jcr.Credentials;
import javax.jcr.SimpleCredentials;

public class RepositoryAddress {
    public static final String JCR_ROOT = "/jcr:root";
    private final URI uri;
    private final URI specific;
    private final String workspace;
    private final String path;
    private static BitSet URISaveEx;
    private static final char[] hexTable;

    public RepositoryAddress(String uri) throws URISyntaxException {
        this(new URI(uri));
    }

    public RepositoryAddress(URI uri) throws URISyntaxException {
        int idx1;
        String workspace;
        String path = uri.getPath();
        String prefix = "/";
        String localPath = "/";
        if (path.length() == 0 || path.equals("/")) {
            workspace = "-";
            localPath = "/";
        } else if (!uri.isAbsolute()) {
            idx1 = path.indexOf(47, 1);
            if (idx1 < 0) {
                workspace = path.substring(1);
            } else {
                workspace = path.substring(1, idx1);
                localPath = path.substring(idx1);
            }
        } else {
            String segment;
            if (path.charAt(path.length() - 1) != '/') {
                path = path + "/";
            }
            idx1 = -1;
            int idx2 = 0;
            int idx3 = path.indexOf(47, 1);
            while (idx3 > 0 && !(segment = path.substring(idx2, idx3)).equals("/jcr:root")) {
                idx1 = idx2;
                idx2 = idx3;
                idx3 = path.indexOf(47, idx3 + 1);
            }
            if (idx3 < 0) {
                if (uri.getScheme() != null && uri.getScheme().equals("rmi")) {
                    idx1 = path.indexOf(47, 1);
                    idx2 = path.indexOf(47, idx1 + 1);
                    if (idx2 < 0) {
                        workspace = "-";
                        prefix = path.substring(0, idx1);
                        localPath = "/";
                    } else {
                        workspace = path.substring(idx1 + 1, idx2);
                        prefix = path.substring(0, idx1);
                        int end = path.length();
                        if (end != idx2 + 1) {
                            --end;
                        }
                        localPath = path.substring(idx2, end);
                    }
                } else {
                    workspace = idx1 < 0 ? "-" : path.substring(idx1 + 1, idx2);
                    prefix = idx1 <= 0 ? "/" : path.substring(0, idx1);
                    localPath = "/";
                }
            } else {
                workspace = path.substring(idx1 + 1, idx2);
                prefix = path.substring(0, idx1);
                int end = path.length();
                if (end - idx3 > 1) {
                    --end;
                }
                localPath = path.substring(idx3, end);
            }
        }
        if (uri.getScheme() != null && uri.getScheme().startsWith("http") && (prefix.equals("/") || prefix.equals("/crx"))) {
            prefix = "/crx/server";
            workspace = "-";
        }
        if (prefix.length() == 0) {
            prefix = "/";
        }
        this.path = localPath;
        this.workspace = workspace;
        this.specific = uri.resolve(prefix);
        StringBuffer buf = new StringBuffer(this.specific.toString());
        if (buf.charAt(buf.length() - 1) != '/') {
            buf.append('/');
        }
        buf.append(workspace);
        buf.append("/jcr:root");
        if (!localPath.equals("/")) {
            buf.append(RepositoryAddress.escapePath(localPath));
        }
        this.uri = new URI(buf.toString());
    }

    private RepositoryAddress(URI uri, URI specific, String workspace, String path) {
        this.uri = uri;
        this.specific = specific;
        this.workspace = workspace;
        this.path = path;
    }

    public URI getURI() {
        return this.uri;
    }

    public RepositoryAddress resolve(String path) {
        if (path == null || path.length() == 0 || path.equals(".") || path.equals("./")) {
            return this;
        }
        StringBuffer newPath = new StringBuffer(this.specific.getPath());
        newPath.append("/");
        newPath.append(this.workspace);
        newPath.append("/jcr:root");
        if (path.charAt(0) != '/') {
            path = this.path.endsWith("/") ? this.path + path : this.path + "/" + path;
        }
        newPath.append(RepositoryAddress.escapePath(path));
        URI uri = this.specific.resolve(newPath.toString());
        return new RepositoryAddress(uri, this.specific, this.workspace, path);
    }

    public String getWorkspace() {
        return "-".equals(this.workspace) ? null : this.workspace;
    }

    public URI getSpecificURI() {
        return this.specific;
    }

    public String getPath() {
        return this.path;
    }

    public Credentials getCredentials() {
        String userinfo = this.uri.getUserInfo();
        if (userinfo == null) {
            return null;
        }
        int idx = userinfo.indexOf(58);
        if (idx < 0) {
            return new SimpleCredentials(userinfo, new char[0]);
        }
        return new SimpleCredentials(userinfo.substring(0, idx), userinfo.substring(idx + 1).toCharArray());
    }

    public String toString() {
        return this.getURI().toString();
    }

    public int hashCode() {
        return this.getURI().hashCode();
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof RepositoryAddress) {
            return this.getURI().equals(((RepositoryAddress)obj).getURI());
        }
        return false;
    }

    private static String escapePath(String string) {
        try {
            byte[] bytes = string.getBytes("utf-8");
            StringBuffer out = new StringBuffer(bytes.length);
            for (byte aByte : bytes) {
                int c = aByte & 255;
                if (URISaveEx.get(c) && c != 37) {
                    out.append((char)c);
                    continue;
                }
                out.append('%');
                out.append(hexTable[c >> 4 & 15]);
                out.append(hexTable[c & 15]);
            }
            return out.toString();
        }
        catch (UnsupportedEncodingException e) {
            throw new InternalError(e.toString());
        }
    }

    static {
        int i;
        URISaveEx = new BitSet(256);
        for (i = 97; i <= 122; ++i) {
            URISaveEx.set(i);
        }
        for (i = 65; i <= 90; ++i) {
            URISaveEx.set(i);
        }
        for (i = 48; i <= 57; ++i) {
            URISaveEx.set(i);
        }
        URISaveEx.set(45);
        URISaveEx.set(95);
        URISaveEx.set(46);
        URISaveEx.set(33);
        URISaveEx.set(126);
        URISaveEx.set(42);
        URISaveEx.set(39);
        URISaveEx.set(40);
        URISaveEx.set(41);
        URISaveEx.set(47);
        hexTable = "0123456789abcdef".toCharArray();
    }
}