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

io.fluxcapacitor.javaclient.web.WebPattern Maven / Gradle / Ivy

There is a newer version: 0.1072.0
Show newest version
/*
 * Copyright (c) Flux Capacitor IP B.V. 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.
 * 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 io.fluxcapacitor.javaclient.web;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Value;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Value
public class WebPattern {
    static final Pattern uriPattern = Pattern.compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");

    static Matcher uriMatcher(String uriString) {
        Matcher result = uriPattern.matcher(Optional.ofNullable(uriString).orElse(""));
        if (result.matches()) {
            return result;
        }
        throw new IllegalStateException("Malformed URI: '" + uriString + "'");
    }

    String uri;
    HttpRequestMethod method;

    @Getter(lazy = true, value = AccessLevel.PRIVATE)
    Matcher matcher = uriMatcher(uri);
    @Getter(lazy = true)
    String path = Optional.ofNullable(getMatcher().group(5)).map(p -> p.startsWith("/") ? p : p.isBlank() ? "" : "/" + p).orElse("");
    @Getter(lazy = true)
    String origin = Optional.ofNullable(getMatcher().group(1))
            .map(scheme -> scheme + getMatcher().group(3)).orElse(null);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy