com.archivas.clienttools.arcutils.impl.adapter.Hcp5AuthNamespaceAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.archivas.clienttools.arcutils.impl.adapter.Hcp5AuthNamespaceAdapter.java

Source

// Copyright 2007 Hitachi Data Systems
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package com.archivas.clienttools.arcutils.impl.adapter;

import com.archivas.clienttools.arcutils.model.ACLMetadata;
import com.archivas.clienttools.arcutils.model.ArcMoverDirectory;
import com.archivas.clienttools.arcutils.model.FileMetadata;
import com.archivas.clienttools.arcutils.model.Owner;
import com.archivas.clienttools.arcutils.profile.Hcp5AuthNamespaceProfile;
import com.archivas.clienttools.arcutils.utils.Base64Utils;
import com.archivas.clienttools.arcutils.utils.StringUtils;
import com.archivas.clienttools.arcutils.utils.net.SSLCertificateCallback;
import com.archivas.clienttools.arcutils.config.HCPMoverConstants;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

import javax.xml.stream.XMLStreamReader;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Hcp5AuthNamespaceAdapter extends Hcp3AuthNamespaceAdapter {

    public static Logger LOG = Logger.getLogger(Hcp5AuthNamespaceAdapter.class.getName());
    private final String ACL_TYPE_STR = "type=acl";

    public Hcp5AuthNamespaceAdapter(Hcp5AuthNamespaceProfile profile, SSLCertificateCallback sslExceptionCallback,
            AbstractHttpClient httpClient) throws StorageAdapterException {
        super(profile, sslExceptionCallback, httpClient);
    }

    @Override
    protected boolean addAdditionalQueryParams(final FileMetadata md, List<NameValuePair> params,
            boolean isCreate) {
        boolean addedParam = false;
        if (md.hasOwner()) {
            if (!isCreate && !md.getOwner().getOwnerType().equals(Owner.OwnerType.EXTERNAL)) {
                // we're modifying the value, so clear out the domain in case it is already set
                params.add(new BasicNameValuePair(HttpGatewayConstants.PARAM_DOMAIN, ""));
                addedParam = true;
            }
            if (md.getOwner().getOwnerType().equals(Owner.OwnerType.LOCAL)) {
                params.add(new BasicNameValuePair(HttpGatewayConstants.PARAM_OWNER, md.getOwner().getOwnerName()));
                addedParam = true;
            } else if (md.getOwner().getOwnerType().equals(Owner.OwnerType.PUBLIC)) {
                params.add(new BasicNameValuePair(HttpGatewayConstants.PARAM_OWNER, ""));
                addedParam = true;
            } else if (md.getOwner().getOwnerType().equals(Owner.OwnerType.EXTERNAL)) {
                params.add(new BasicNameValuePair(HttpGatewayConstants.PARAM_OWNER,
                        "" + md.getOwner().getOwnerName()));
                params.add(
                        new BasicNameValuePair(HttpGatewayConstants.PARAM_DOMAIN, "" + md.getOwner().getDomain()));
                addedParam = true;
            } else if (md.getOwner().getOwnerType().equals(Owner.OwnerType.PROFILE) && !isCreate) {
                // this is free on ingest, but NOT free on update, so we have to set it
                params.add(new BasicNameValuePair(HttpGatewayConstants.PARAM_OWNER, getProfile().getUsername()));
                addedParam = true;
            }
        } else if (md.hasUid() && md.hasGid()) {
            params.add(new BasicNameValuePair(HttpGatewayConstants.PARAM_OWNER, "")); // Secret
                                                                                      // handshake
                                                                                      // includes a
                                                                                      // blank owner
            params.add(new BasicNameValuePair(HttpGatewayConstants.PARAM_PERMS_UID, "" + md.getUid()));
            params.add(new BasicNameValuePair(HttpGatewayConstants.PARAM_PERMS_GID, "" + md.getGid()));
        } else if (md.hasUid() || md.hasGid()) {
            LOG.log(Level.SEVERE,
                    "Object from HCP has either a UID or a GID but not both -- this will not migrate this metadata correctly");
        }

        return addedParam;
    }

    @Override
    protected boolean getAdditionalMetadata(final XMLStreamReader xmlr, FileMetadata metadata, String filePath) {
        boolean hasACL = false;
        try {
            hasACL = Boolean.parseBoolean(xmlr.getAttributeValue(null, HttpGatewayConstants.MD_HAS_ACL));
        } catch (NullPointerException e) {
            LOG.log(Level.WARNING, "Exception parsing metadata for: " + filePath, e);
        }

        String owner = xmlr.getAttributeValue(null, HttpGatewayConstants.PARAM_OWNER);
        String domain = xmlr.getAttributeValue(null, HttpGatewayConstants.PARAM_DOMAIN);

        return handleAdditionalMetadata(metadata, hasACL, owner, domain, null, null, filePath);
    }

    @Override
    protected boolean getAdditionalMetadata(final HttpResponse response, FileMetadata metadata, String filePath) {
        boolean hasACL = false;
        try {
            hasACL = Boolean
                    .parseBoolean(response.getFirstHeader(HttpGatewayConstants.HEADER_ACL_BOOLEAN_ANS).getValue());
        } catch (NullPointerException e) {
            // do nothing
        }
        String owner = null;
        String domain = null;
        String uid = null;
        String gid = null;
        try {
            owner = response.getFirstHeader(HttpGatewayConstants.HEADER_OWNER).getValue();
        } catch (NullPointerException e) {
            // do nothing
        }
        try {
            domain = response.getFirstHeader(HttpGatewayConstants.HEADER_DOMAIN).getValue();
        } catch (NullPointerException e) {
            // do nothing
        }
        try {
            uid = response.getFirstHeader(HttpGatewayConstants.HEADER_UID).getValue();
        } catch (NullPointerException e) {
            // do nothing
        }
        try {
            gid = response.getFirstHeader(HttpGatewayConstants.HEADER_GID).getValue();
        } catch (NullPointerException e) {
            // do nothing
        }
        return handleAdditionalMetadata(metadata, hasACL, owner, domain, uid, gid, filePath);
    }

    private boolean handleAdditionalMetadata(FileMetadata metadata, final boolean hasACL, final String owner,
            final String domain, final String uid, final String gid, final String filePath) {
        if (hasACL) {
            ACLMetadata acl = new ACLMetadata(ACLMetadata.Form.PROFILED, filePath);
            metadata.setACL(acl);
        }

        Owner ownerObj;
        if (domain != null && domain.length() > 0) {
            if (owner == null) {
                throw new IllegalArgumentException(
                        "handleAddtionalMetadata: Excepted owner value to be supplied with domain");
            }
            ownerObj = new Owner(owner, domain);
        } else if (owner != null && owner.length() > 0) {
            ownerObj = new Owner(owner);
        } else if (uid != null && uid.length() > 0 && gid != null && gid.length() > 0) {
            ownerObj = null;
            metadata.setUid(uid);
            metadata.setGid(gid);
        } else {
            ownerObj = new Owner(Owner.OwnerType.PUBLIC);
        }

        metadata.setOwner(ownerObj);

        return true; // we always have an owner or a uid/gid
    }

    public InputStream getACLStream(final String path) throws StorageAdapterException {
        Header contentTypeHeader = new BasicHeader(HttpGatewayConstants.CONTENT_TYPE,
                HttpGatewayConstants.XML_CONTENT_TYPE);
        return getInputStream(path, ACL_TYPE_STR + HCPMoverConstants.URL_PARAM_PRETTY_PRINT, contentTypeHeader);
    }

    public void setACLFromStream(final String path, InputStream aclStream) throws StorageAdapterException {
        LOG.info("Attempting to write acl.");
        String queryString = ACL_TYPE_STR;
        Header contentTypeHeader = new BasicHeader(HttpGatewayConstants.ACCEPT,
                HttpGatewayConstants.XML_CONTENT_TYPE);
        reallyDoWriteStream(getHost(), path, queryString, aclStream, "writing acl", contentTypeHeader);
    }

    @Override
    public boolean isVersioningEnabled() throws StorageAdapterException {
        // Try to get the versions of a random (made up) file.
        // There are 3 possible scenarios:
        // a) versioning is enabled, and the file exsists, so the call will succeed without error
        // b) versioning is enabled and the file does NOT exist. We'll get a 404 NOT FOUND returned
        // c) versioning is not enabled, regardless whether the file exists or not, we'll get a 400
        // BAD REQUEST
        try {
            ArcMoverDirectory dir = getVersions("/HCP_dummy_filename_used_by_Data_Migrator_may_or_may_not_exist");
            dir.getFileListIterator(true, true);
            return true;
        } catch (StorageAdapterException e) {
            Integer statusCode = e.getStatusCode();
            if (statusCode != null && statusCode == HttpStatus.SC_BAD_REQUEST) { // 400
                return false;
            } else if (statusCode != null && statusCode == HttpStatus.SC_NOT_FOUND) { // 404
                return true;
            } else {
                throw e;
            }
        }
    }

    @Override
    protected String setAuthHeader(HcapAdapterCookie cookie) {
        String username = HCPMoverConstants.HCP_ANON_USERNAME;
        String password = StringUtils.EMPTYSTR;

        if (!profile.isAnonymousAccess()) {
            username = profile.getUsername();
            password = profile.getPassword();
        }

        String authString = "hcp-ns-auth= " + Base64Utils.encode(username.getBytes()) + ":" + password;
        cookie.getRequest().setHeader("Cookie", authString);
        return authString;
    }
}