All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.branch.cos.internal.AbstractCosResponseHandler Maven / Gradle / Ivy

There is a newer version: 5.6.20
Show newest version
/*
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. 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.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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.
 
 * According to cos feature, we modify some class,comment, field name, etc.
 */


package com.branch.cos.internal;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.branch.cos.Headers;
import com.branch.cos.http.CosHttpResponse;
import com.branch.cos.http.HttpResponseHandler;
import com.branch.cos.model.ObjectMetadata;
import com.branch.cos.utils.DateUtils;
import com.branch.cos.utils.StringUtils;

import java.util.Map.Entry;

public abstract class AbstractCosResponseHandler
        implements HttpResponseHandler> {

    private static final Logger log = LoggerFactory.getLogger(AbstractCosResponseHandler.class);

    /** The set of response headers that aren't part of the object's metadata */
    private static final Set ignoredHeaders;

    static {
        ignoredHeaders = new HashSet();
//        ignoredHeaders.add(Headers.DATE);
        ignoredHeaders.add(Headers.SERVER);
//        ignoredHeaders.add(Headers.REQUEST_ID);
        ignoredHeaders.add(Headers.TRACE_ID);
    }

    /**
     * The majority of COS response handlers read the complete response while handling it, and don't
     * need to manually manage the underlying HTTP connection.
     *
     */
    public boolean needsConnectionLeftOpen() {
        return false;
    }

    /**
     * Parses the COS response metadata (ex: COS request ID) from the specified response, and
     * returns a CosServiceResponse object ready for the result to be plugged in.
     *
     * @param response The response containing the response metadata to pull out.
     *
     * @return A new, populated CosServiceResponse object, ready for the result to be plugged in.
     */
    protected CosServiceResponse parseResponseMetadata(CosHttpResponse response) {
        CosServiceResponse cosResponse = new CosServiceResponse();
        String cosRequestId = response.getHeaders().get(Headers.REQUEST_ID);
        String cosTraceId = response.getHeaders().get(Headers.TRACE_ID);

        Map metadataMap = new HashMap();
        metadataMap.put(Headers.REQUEST_ID, cosRequestId);
        metadataMap.put(Headers.TRACE_ID, cosTraceId);
        cosResponse.setResponseMetadata(new ResponseMetadata(metadataMap));

        return cosResponse;
    }

    /**
     * Populates the specified COSObjectMetadata object with all object metadata pulled from the
     * headers in the specified response.
     *
     * @param response The HTTP response containing the object metadata within the headers.
     * @param metadata The metadata object to populate from the response's headers.
     */
    protected void populateObjectMetadata(CosHttpResponse response, ObjectMetadata metadata) {
        for (Entry header : response.getHeaders().entrySet()) {
            String key = header.getKey();
            if (key.startsWith(Headers.COS_USER_METADATA_PREFIX)) {
                key = key.substring(Headers.COS_USER_METADATA_PREFIX.length());
                metadata.addUserMetadata(key, header.getValue());
            } else if (ignoredHeaders.contains(key)) {
                // ignore...
            } else if (key.equals(Headers.LAST_MODIFIED)) {
                try {
                    metadata.setHeader(key, DateUtils.parseRFC822Date(header.getValue()));
                } catch (Exception pe) {
                    log.warn("Unable to parse last modified date: " + header.getValue(), pe);
                }
            } else if (key.equals(Headers.CONTENT_LENGTH)) {
                try {
                    metadata.setHeader(key, Long.parseLong(header.getValue()));
                } catch (NumberFormatException nfe) {
                    log.warn("Unable to parse content length: " + header.getValue(), nfe);
                }
            } else if (key.equals(Headers.OBJECT_SIZE_WHEN_HEAD)) {
                try {
                    metadata.setHeader(key, Long.parseLong(header.getValue()));
                } catch (NumberFormatException nfe) {
                    log.warn("Unable to parse Size: " + header.getValue(), nfe);
                }
            } else if (key.equals(Headers.DELETE_MARKER)) {
                metadata.setDeleteMarker(Boolean.parseBoolean(header.getValue()));
            } else if (key.equals(Headers.ETAG)) {
                metadata.setHeader(key, StringUtils.removeQuotes(header.getValue()));
            } else if (key.equals(Headers.EXPIRES)) {
                try {
                    metadata.setHttpExpiresDate(DateUtils.parseRFC822Date(header.getValue()));
                } catch (Exception pe) {
                    log.warn("Unable to parse http expiration date: " + header.getValue(), pe);
                }
            } else if (key.equals(Headers.EXPIRATION)) {
                new ObjectExpirationHeaderHandler().handle(metadata, response);
            } else if (key.equalsIgnoreCase(Headers.RESTORE)) {
                new ObjectRestoreHeaderHandler().handle(metadata, response);
            } else {
                metadata.setHeader(key, header.getValue());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy