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

com.qwazr.extractor.UriInfoImpl Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 * Copyright 2015-2020 Emmanuel Keller
 * 

* 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.qwazr.extractor; import com.qwazr.utils.StringUtils; import org.apache.commons.lang3.NotImplementedException; import org.glassfish.jersey.internal.util.collection.ImmutableMultivaluedMap; import org.glassfish.jersey.uri.internal.JerseyUriBuilder; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.PathSegment; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Collections; import java.util.List; class UriInfoImpl implements UriInfo { private final URI baseUri; private final URI absoluteUri; private final URI relativeUri; private volatile URI absolutePath; private volatile List rawPathSegments; private volatile List decodedPathSegments; private volatile MultivaluedMap rawParameters; private volatile MultivaluedMap decodedParameters; public UriInfoImpl(final URI base, final URI child) { baseUri = base; absoluteUri = child.isAbsolute() ? child : base.resolve(child); relativeUri = base.relativize(child); } public UriInfoImpl(final String base, final String child) throws URISyntaxException { this(new URI(base), new URI(child)); } @Override public String getPath() { return relativeUri.getPath(); } @Override public String getPath(boolean decode) { return decode ? getPath() : relativeUri.getRawPath(); } @Override public synchronized List getPathSegments() { if (decodedPathSegments == null) decodedPathSegments = pathSegments(relativeUri.getPath()); return decodedPathSegments; } @Override public synchronized List getPathSegments(boolean decode) { if (decode) return getPathSegments(); if (rawPathSegments == null) rawPathSegments = pathSegments(relativeUri.getRawPath()); return rawPathSegments; } @Override public URI getRequestUri() { return absoluteUri; } @Override public UriBuilder getRequestUriBuilder() { return new JerseyUriBuilder().uri(absoluteUri); } @Override public synchronized URI getAbsolutePath() { if (absolutePath == null) absolutePath = baseUri.resolve(getPath(false)); return absolutePath; } @Override public UriBuilder getAbsolutePathBuilder() { return new JerseyUriBuilder().uri(getAbsolutePath()); } @Override public URI getBaseUri() { return baseUri; } @Override public UriBuilder getBaseUriBuilder() { return new JerseyUriBuilder().uri(baseUri); } @Override public MultivaluedMap getPathParameters() { return getPathParameters(true); } @Override public MultivaluedMap getPathParameters(boolean decode) { throw new IllegalStateException("Not implemented"); } private final static MultivaluedMap EMPTY = new ImmutableMultivaluedMap<>(new MultivaluedHashMap<>()); private MultivaluedMap queryParameters(final String query) { if (query == null) return EMPTY; final String[] queryParts = StringUtils.split(query, '&'); final MultivaluedMap multivaluedMap = new MultivaluedHashMap<>(); for (String queryPart : queryParts) { final int sepPos = queryPart.indexOf('='); if (sepPos == -1) continue; final String key = queryPart.substring(0, sepPos); final String value = queryPart.substring(sepPos + 1); multivaluedMap.add(key, value); } return multivaluedMap; } @Override public synchronized MultivaluedMap getQueryParameters() { if (decodedParameters == null) decodedParameters = queryParameters(relativeUri.getQuery()); return decodedParameters; } @Override public synchronized MultivaluedMap getQueryParameters(boolean decode) { if (decode) return getQueryParameters(); if (rawParameters == null) rawParameters = queryParameters(relativeUri.getRawQuery()); return rawParameters; } @Override public List getMatchedURIs() { throw new NotImplementedException("Not implemented"); } @Override public List getMatchedURIs(boolean decode) { throw new NotImplementedException("Not implemented"); } @Override public List getMatchedResources() { throw new NotImplementedException("Not implemented"); } @Override public URI resolve(URI uri) { return baseUri.resolve(uri).normalize(); } @Override public URI relativize(final URI uri) { return absolutePath.relativize(uri); } private List pathSegments(final String path) { if (path == null || path.isEmpty()) return Collections.emptyList(); final String[] pathParts = StringUtils.split(path, '/'); final List pathSegments = new ArrayList<>(pathParts.length); for (String pathPart : pathParts) pathSegments.add(new PathSegmentImpl(pathPart)); return pathSegments; } private static final class PathSegmentImpl implements PathSegment { private final String path; private PathSegmentImpl(final String path) { this.path = path; } @Override public String getPath() { return path; } @Override public MultivaluedMap getMatrixParameters() { throw new NotImplementedException("Not implemented"); } } }