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

Java tutorial

Introduction

Here is the source code for com.archivas.clienttools.arcutils.impl.adapter.Hcp6AuthNamespaceAdapter.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.config.HCPMoverConstants;
import com.archivas.clienttools.arcutils.model.CustomMetadata;
import com.archivas.clienttools.arcutils.model.FileMetadata;
import com.archivas.clienttools.arcutils.profile.Hcp6AuthNamespaceProfile;
import com.archivas.clienttools.arcutils.utils.Base64Utils;
import com.archivas.clienttools.arcutils.utils.StringUtils;
import com.archivas.clienttools.arcutils.utils.net.SSLCertificateCallback;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.AbstractHttpClient;

import javax.xml.stream.XMLStreamReader;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Hcp6AuthNamespaceAdapter extends Hcp5AuthNamespaceAdapter {
    public static final Logger LOG = Logger.getLogger(Hcp6AuthNamespaceAdapter.class.getName());

    public Hcp6AuthNamespaceAdapter(Hcp6AuthNamespaceProfile profile, SSLCertificateCallback sslExceptionCallback,
            AbstractHttpClient httpClient) throws StorageAdapterException {
        super(profile, sslExceptionCallback, httpClient);
    }

    @Override
    /**
     * Determine if versioning is enabled by doing a GET of /proc, but request only results for a
     * single namespace
     */
    public boolean isVersioningEnabled() throws StorageAdapterException {
        return isVersioningEnabled(PROC_PATH + "?single=true"); // fetch only one namespace
    }

    @Override
    protected boolean getAdditionalMetadata(final XMLStreamReader xmlr, FileMetadata metadata, String filePath) {
        String annotations = "";
        try {
            annotations = xmlr.getAttributeValue(null, HttpGatewayConstants.MD_CM_ANNOTATIONS);
        } catch (NullPointerException e) {
            LOG.log(Level.WARNING, "Exception parsing metadata for: " + filePath, e);
        }
        return handleAdditionalMetadata(metadata, annotations, filePath)
                && super.getAdditionalMetadata(xmlr, metadata, filePath);
    }

    @Override
    protected boolean getAdditionalMetadata(final HttpResponse response, FileMetadata metadata, String filePath) {
        String annotations = "";
        try {
            annotations = response.getFirstHeader(HttpGatewayConstants.HEADER_ANNOTATIONS_ANS).getValue();
        } catch (NullPointerException e) {
            /* nom nom nom */
        }
        return handleAdditionalMetadata(metadata, annotations, filePath)
                && super.getAdditionalMetadata(response, metadata, filePath);
    }

    private boolean handleAdditionalMetadata(FileMetadata metadata, final String annotations,
            final String filePath) {
        if (!annotations.isEmpty()) {
            Hashtable<String, CustomMetadata> aList = new Hashtable<String, CustomMetadata>();
            for (String a : annotations.split(", ")) {
                String[] tmp = a.split(";");
                aList.put(tmp[0], new CustomMetadata(CustomMetadata.Form.PROFILED, filePath, null, tmp[0]));
            }
            metadata.setAnnotationList(aList);
        }
        return true;
    }

    @Override
    protected String buildCMQueryString(final Long version, final String annotation) {
        StringBuilder sb = new StringBuilder(super.buildCMQueryString(version, annotation));
        if (annotation != null) {
            sb.append("&annotation=" + annotation);
        }
        return sb.toString();
    }

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

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

        String authString = "HCP " + username + ":" + password;
        cookie.getRequest().setHeader("Authorization", authString);
        return authString;
    }
}