PackageShareSessionImpl.java 7.14 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.http.auth.Credentials
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.day.crx.packaging.impl;

import com.day.crx.packaging.HtmlStatusResponseParser;
import com.day.crx.packaging.JSONResponse;
import com.day.crx.packaging.PackageInfo;
import com.day.crx.packaging.PackageShare;
import com.day.crx.packaging.PackageShareSession;
import com.day.crx.packaging.Proxy;
import com.day.crx.packaging.ProxyTracker;
import com.day.crx.packaging.impl.PackageShareImpl;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.http.auth.Credentials;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

public class PackageShareSessionImpl
implements PackageShareSession {
    private final PackageShareImpl svc;
    private final String id;
    private final long creationTime;
    private Data data;
    private Credentials creds;
    private CloseableHttpClient httpClient;

    public PackageShareSessionImpl(PackageShareImpl svc, String id, CloseableHttpClient httpClient) {
        this.svc = svc;
        this.id = id;
        this.httpClient = httpClient;
        this.creationTime = System.currentTimeMillis();
    }

    public void setUser(String userId, Credentials creds, JSONObject data) {
        this.creds = creds;
        this.data = new Data(userId, data);
    }

    @Override
    public boolean isNew() {
        return this.data == null;
    }

    @Override
    public boolean isAnonymous() {
        return this.creds == null;
    }

    @Override
    public PackageShare getService() {
        return this.svc;
    }

    public CloseableHttpClient getHttpClient() {
        return this.httpClient;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public long getCreationTime() {
        return this.creationTime;
    }

    @Override
    public String getUserId() {
        return this.data == null ? "anonymous" : this.data.userId;
    }

    @Override
    public String getUserName() {
        if (this.data == null) {
            return null;
        }
        try {
            return this.data.data.getJSONObject("user").optString("name");
        }
        catch (JSONException e) {
            return null;
        }
    }

    @Override
    public Set<String> getSharedGroups() {
        return this.data == null ? null : this.data.getSharedGroups();
    }

    @Override
    public JSONObject getSessionInfo() {
        return this.data == null ? null : this.data.data;
    }

    @Override
    public Set<PackageInfo> getAvailablePackages() {
        return null;
    }

    @Override
    public void logout() {
        this.data = null;
        this.creds = null;
        this.svc.discardSession(this.id);
    }

    @Override
    public Proxy createProxy(boolean flag) {
        Proxy p = this.svc.createProxy(flag, this.httpClient);
        if (this.creds != null) {
            p.setCredentials(this.creds);
        }
        return p;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public JSONResponse uploadPackage(Node fileNode, ProxyTracker tracker, String group) throws IOException, RepositoryException {
        String title;
        Property prop = fileNode.getProperty("{http://www.jcp.org/jcr/1.0}content/{http://www.jcp.org/jcr/1.0}data");
        try {
            title = fileNode.getProperty("{http://www.jcp.org/jcr/1.0}content/{http://www.jcp.org/jcr/1.0}title").getString();
        }
        catch (RepositoryException e) {
            title = fileNode.getName();
        }
        Binary binary = prop.getBinary();
        Proxy proxy = this.createProxy(false);
        proxy.setTracker(tracker);
        proxy.addExtraFile("file", binary, title);
        proxy.addExtraParameter("cmd", "promote");
        if (group != null && group.length() > 0) {
            proxy.addExtraParameter("group", group);
        }
        try {
            int status = proxy.doPost("/apps/packageshare/content/service.html");
            String data = proxy.fetch();
            try {
                JSONResponse jSONResponse = HtmlStatusResponseParser.parse(data);
                return jSONResponse;
            }
            catch (IOException e) {
                JSONResponse r = new JSONResponse(false, "Unknown Error");
                r.setStatus(status);
                JSONResponse jSONResponse = r;
                proxy.close();
                binary.dispose();
                return jSONResponse;
            }
        }
        finally {
            proxy.close();
            binary.dispose();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public JSONResponse uploadPackage(File file, String fileName, ProxyTracker tracker, String group) throws IOException, RepositoryException {
        Proxy proxy = this.createProxy(false);
        proxy.setTracker(tracker);
        proxy.addExtraFile("file", file, fileName == null ? "unknown" : fileName);
        proxy.addExtraParameter("cmd", "promote");
        if (group != null && group.length() > 0) {
            proxy.addExtraParameter("group", group);
        }
        try {
            int status = proxy.doPost("/apps/packageshare/content/service.html");
            String data = proxy.fetch();
            try {
                JSONResponse jSONResponse = HtmlStatusResponseParser.parse(data);
                return jSONResponse;
            }
            catch (IOException e) {
                JSONResponse r = new JSONResponse(false, "Unknown Error");
                r.setStatus(status);
                JSONResponse jSONResponse = r;
                proxy.close();
                return jSONResponse;
            }
        }
        finally {
            proxy.close();
        }
    }

    private static final class Data {
        private final String userId;
        private final JSONObject data;
        private Set<String> shares;

        private Data(String userId, JSONObject data) {
            this.userId = userId;
            this.data = data;
        }

        public Set<String> getSharedGroups() {
            if (this.shares == null) {
                this.shares = new HashSet<String>();
                try {
                    JSONArray names = this.data.getJSONObject("shares").names();
                    for (int i = 0; i < names.length(); ++i) {
                        this.shares.add(names.getString(i));
                    }
                }
                catch (JSONException e) {
                    // empty catch block
                }
            }
            return this.shares;
        }
    }

}