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

org.jboss.resteasy.reactive.common.util.PathSegmentImpl Maven / Gradle / Ivy

There is a newer version: 3.17.5
Show newest version
package org.jboss.resteasy.reactive.common.util;

import java.util.ArrayList;
import java.util.List;

import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.PathSegment;

/**
 * @author Bill Burke
 */
public class PathSegmentImpl implements PathSegment {
    private String path;
    private String original;
    private MultivaluedMap matrixParameters;
    private boolean hasMatrixParams;

    /**
     * @param segment encoded path segment
     * @param decode whether or not to decode values
     */
    public PathSegmentImpl(final String segment, final boolean decode) {
        this.original = segment;
        this.path = segment;
        int semicolon = segment.indexOf(';');
        if (semicolon >= 0) {
            matrixParameters = new QuarkusMultivaluedHashMap();
            hasMatrixParams = true;
            if (semicolon > 0)
                this.path = segment.substring(0, semicolon);
            else
                this.path = "";
            String matrixParams = segment.substring(semicolon + 1);
            String[] params = matrixParams.split(";");
            for (String param : params) {
                String[] namevalue = param.split("=");
                if (namevalue != null && namevalue.length > 0) {
                    String name = namevalue[0];
                    if (decode)
                        name = Encode.decodePath(name);
                    String value = null;
                    if (namevalue.length > 1) {
                        value = namevalue[1];
                    }
                    if (decode && value != null)
                        value = Encode.decodePath(value);
                    matrixParameters.add(name, value);
                }
            }
        }
        if (decode)
            this.path = Encode.decodePath(this.path);
    }

    /**
     * NOTE: Used for optimization in ResteasyUriInfo
     *
     * @return true if path segment contains matrix parameters
     */
    public boolean hasMatrixParams() {
        return hasMatrixParams;
    }

    public String getOriginal() {
        return original;
    }

    public String getPath() {
        return path;
    }

    public MultivaluedMap getMatrixParameters() {
        if (matrixParameters == null) {
            matrixParameters = new QuarkusMultivaluedHashMap();
        }
        return matrixParameters;
    }

    public String toString() {
        StringBuilder buf = new StringBuilder();
        if (path != null)
            buf.append(path);
        if (matrixParameters != null) {
            for (String name : matrixParameters.keySet()) {
                for (String value : matrixParameters.get(name)) {
                    buf.append(";").append(name).append("=").append(value);
                }
            }
        }
        return buf.toString();
    }

    public static List parseSegments(String path, boolean decode) {
        return parseSegmentsOptimization(path, decode).segments;
    }

    /**
     * Used when creating the matching path in ResteasyUriInfo
     *
     */
    public static class SegmentParse {
        public List segments;
        public boolean hasMatrixParams;

    }

    /**
     *
     * @param path encoded full path
     * @param decode whether or not to decode each segment
     * @return {@link SegmentParse}
     */
    public static SegmentParse parseSegmentsOptimization(String path, boolean decode) {
        SegmentParse parse = new SegmentParse();
        List pathSegments = new ArrayList();
        parse.segments = pathSegments;
        int start = 0;
        if (path.startsWith("/"))
            start++;
        int length = path.length();
        do {
            String p;
            int slash = path.indexOf('/', start);
            if (slash < 0) {
                p = path.substring(start);
                start = length;
            } else {
                p = path.substring(start, slash);
                start = slash + 1;
            }
            PathSegmentImpl pathSegment = new PathSegmentImpl(p, decode);
            parse.hasMatrixParams |= pathSegment.hasMatrixParams();
            pathSegments.add(pathSegment);
        } while (start < length);
        return parse;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy