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

com.centurylink.mdw.model.asset.RequestKey Maven / Gradle / Ivy

There is a newer version: 6.1.39
Show newest version
/*
 * Copyright (C) 2018 CenturyLink, Inc.
 *
 * 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.centurylink.mdw.model.asset;

import com.centurylink.mdw.model.asset.AssetRequest.HttpMethod;

/**
 * Uniquely identifies a request by its method/path.
 */
public class RequestKey implements Comparable {

    /**
     * This is the full service path (whereas AssetRequest.path is to asset package).
     */
    private String path;
    public String getPath() { return path; }

    private HttpMethod method;
    public HttpMethod getMethod() { return method; }

    public RequestKey(HttpMethod method, String path) {
        this.method = method;
        this.path = path;
    }

    public RequestKey(String method, String path) {
        this.method = HttpMethod.valueOf(method.toUpperCase());
        this.path = path;
    }

    public String toString() {
        return path + " " + method;
    }

    public boolean match(RequestKey other) {
        if (!method.equals(other.method))
            return false;
        // handle dynamic path elements
        String[] segs1 = path.split("/");
        String[] segs2 = other.path.split("/");
        for (int i = 0; i < segs1.length; i++) {
            String seg = segs1[i];
            if (seg.startsWith("{") && seg.endsWith("}")) {
                segs1[i] = "{}";
                if (segs2.length > i)
                    segs2[i] = "{}";
            }
        }
        for (int i = 0; i < segs2.length; i++) {
            String seg = segs2[i];
            if (seg.startsWith("{") && seg.endsWith("}") && !seg.equals("{}")) {
                segs2[i] = "{}";
                if (segs1.length > i)
                    segs1[i] = "{}";
            }
        }
        return String.join("/", segs1).equals(String.join("/", segs2));
    }

    /**
     * Not to be used for equality tests (see match).
     */
    @Override
    public int compareTo(RequestKey other) {
        return toString().compareTo(other.toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy