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

com.jcabi.http.response.WebLinkingResponse Maven / Gradle / Ivy

/**
 * Copyright (c) 2011-2014, jcabi.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met: 1) Redistributions of source code must retain the above
 * copyright notice, this list of conditions and the following
 * disclaimer. 2) Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution. 3) Neither the name of the jcabi.com nor
 * the names of its contributors may be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.jcabi.http.response;

import com.jcabi.aspects.Immutable;
import com.jcabi.http.Request;
import com.jcabi.http.Response;
import com.jcabi.immutable.ArrayMap;
import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;

/**
 * Web Linking response.
 *
 * 

This response decorator is able to understand and parse {@code Link} * HTTP header according to * RFC 5988 "Web Linking", * for example: * *

 String name = new JdkRequest("http://my.example.com")
 *   .fetch()
 *   .as(WebLinkingResponse.class)
 *   .follow("next")
 *   .fetch();
* *

The class is immutable and thread-safe. * * @author Yegor Bugayenko ([email protected]) * @version $Id$ * @since 0.9 * @see RFC 5988 "Web Linking" */ @Immutable @EqualsAndHashCode(callSuper = true) @SuppressWarnings("PMD.TooManyMethods") public final class WebLinkingResponse extends AbstractResponse { /** * ImmutableHeader name. */ private static final String HEADER = "Link"; /** * Param name. */ private static final String REL = "rel"; /** * Public ctor. * @param resp Response */ public WebLinkingResponse( @NotNull(message = "response can't be NULL") final Response resp) { super(resp); } /** * Follow link by REL. * @param rel Relation name * @return The same object * @throws IOException If fails */ @NotNull(message = "response is never NULL") public Request follow(@NotNull(message = "rel can't be NULL") final String rel) throws IOException { final WebLinkingResponse.Link link = this.links().get(rel); if (link == null) { throw new IOException( String.format( "Link with rel=\"%s\" doesn't exist, use #hasLink()", rel ) ); } return new RestResponse(this).jump(link.uri()); } /** * Get all links provided. * @return List of all links found * @throws IOException If fails */ @NotNull(message = "list of links is never NULL") @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public Map links() throws IOException { final ConcurrentMap links = new ConcurrentHashMap(); final Collection headers = this.headers().get(WebLinkingResponse.HEADER); if (headers != null) { for (final String header : headers) { for (final String part : header.split(",")) { final WebLinkingResponse.Link link = new WebLinkingResponse.SimpleLink(part.trim()); final String rel = link.get(WebLinkingResponse.REL); if (rel != null) { links.put(rel, link); } } } } return links; } /** * Single link. */ @Immutable public interface Link extends Map { /** * Its URI. * @return URI */ URI uri(); } /** * Implementation of a link. */ @Immutable @EqualsAndHashCode private static final class SimpleLink implements WebLinkingResponse.Link { /** * Pattern to match link value. */ @SuppressWarnings("PMD.UnusedPrivateField") private static final Pattern PTN = Pattern.compile( "<([^>]+)>\\s*;(.*)" ); /** * URI encapsulated. */ private final transient String addr; /** * Map of link params. */ private final transient ArrayMap params; /** * Public ctor (parser). * @param text Text to parse * @throws IOException If fails */ SimpleLink(final String text) throws IOException { final Matcher matcher = WebLinkingResponse.SimpleLink.PTN .matcher(text); if (!matcher.matches()) { throw new IOException( String.format( "Link header value doesn't comply to RFC-5988: \"%s\"", text ) ); } this.addr = matcher.group(1); final ConcurrentMap args = new ConcurrentHashMap(); for (final String pair : matcher.group(2).trim().split("\\s*;\\s*")) { final String[] parts = pair.split("="); args.put( parts[0].trim().toLowerCase(Locale.ENGLISH), parts[1].trim().replaceAll("(^\"|\"$)", "") ); } this.params = new ArrayMap(args); } @Override public URI uri() { return URI.create(this.addr); } @Override public int size() { return this.params.size(); } @Override public boolean isEmpty() { return this.params.isEmpty(); } @Override public boolean containsKey(final Object key) { return this.params.containsKey(key); } @Override public boolean containsValue(final Object value) { return this.params.containsValue(value); } @Override public String get(final Object key) { return this.params.get(key); } @Override public String put(final String key, final String value) { throw new UnsupportedOperationException("#put()"); } @Override public String remove(final Object key) { throw new UnsupportedOperationException("#remove()"); } @Override public void putAll(final Map map) { throw new UnsupportedOperationException("#putAll()"); } @Override public void clear() { throw new UnsupportedOperationException("#clear()"); } @Override public Set keySet() { return this.params.keySet(); } @Override public Collection values() { return this.params.values(); } @Override public Set> entrySet() { return this.params.entrySet(); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy