| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FcWire |
|
| 1.6;1.6 |
| 1 | /** | |
| 2 | * Copyright (c) 2011-2017, jcabi.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.jcabi.http.wire; | |
| 31 | ||
| 32 | import com.jcabi.aspects.Immutable; | |
| 33 | import com.jcabi.aspects.Tv; | |
| 34 | import com.jcabi.http.Request; | |
| 35 | import com.jcabi.http.Response; | |
| 36 | import com.jcabi.http.Wire; | |
| 37 | import java.io.IOException; | |
| 38 | import java.io.InputStream; | |
| 39 | import java.net.URI; | |
| 40 | import java.util.Collection; | |
| 41 | import java.util.Map; | |
| 42 | import lombok.EqualsAndHashCode; | |
| 43 | import lombok.ToString; | |
| 44 | ||
| 45 | /** | |
| 46 | * Wire that caches GET requests. | |
| 47 | * | |
| 48 | * <p>This decorator can be used when you want to avoid duplicate | |
| 49 | * GET requests to load-sensitive resources, for example: | |
| 50 | * | |
| 51 | * <pre> String html = new JdkRequest("http://goggle.com") | |
| 52 | * .through(FileCachingWire.class) | |
| 53 | * .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN) | |
| 54 | * .fetch() | |
| 55 | * .body();</pre> | |
| 56 | * | |
| 57 | * <p>You can also configure it to flush the entire cache | |
| 58 | * on certain request URI's, for example: | |
| 59 | * | |
| 60 | * <pre>new JdkRequest(uri) | |
| 61 | * .through(CachingWire.class, "GET /save/.*") | |
| 62 | * .uri().path("/save/123").back() | |
| 63 | * .fetch();</pre> | |
| 64 | * | |
| 65 | * <p>The regular expression provided will be used against a string | |
| 66 | * constructed as an HTTP method, space, path of the URI together with | |
| 67 | * query part. | |
| 68 | * | |
| 69 | * <p>The class is immutable and thread-safe. | |
| 70 | * | |
| 71 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
| 72 | * @version $Id: d5017110cd6dec09fea6944fe318d42b26cbb49c $ | |
| 73 | * @since 1.16 | |
| 74 | */ | |
| 75 | @Immutable | |
| 76 | 0 | @ToString |
| 77 | 0 | @EqualsAndHashCode(of = { "origin", "regex" }) |
| 78 | public final class FcWire implements Wire { | |
| 79 | ||
| 80 | /** | |
| 81 | * Cache in files. | |
| 82 | */ | |
| 83 | private final transient FcCache cache; | |
| 84 | ||
| 85 | /** | |
| 86 | * Original wire. | |
| 87 | */ | |
| 88 | private final transient Wire origin; | |
| 89 | ||
| 90 | /** | |
| 91 | * Flushing regular expression. | |
| 92 | */ | |
| 93 | private final transient String regex; | |
| 94 | ||
| 95 | /** | |
| 96 | * Public ctor. | |
| 97 | * @param wire Original wire | |
| 98 | */ | |
| 99 | public FcWire(final Wire wire) { | |
| 100 | 2 | this(wire, "$never"); |
| 101 | 2 | } |
| 102 | ||
| 103 | /** | |
| 104 | * Public ctor. | |
| 105 | * @param wire Original wire | |
| 106 | * @param flsh Flushing regular expression | |
| 107 | */ | |
| 108 | public FcWire(final Wire wire, final String flsh) { | |
| 109 | 3 | this(wire, flsh, new FcCache()); |
| 110 | 3 | } |
| 111 | ||
| 112 | /** | |
| 113 | * Public ctor. | |
| 114 | * @param wire Original wire | |
| 115 | * @param flsh Flushing regular expression | |
| 116 | * @param path Path for the files | |
| 117 | */ | |
| 118 | public FcWire(final Wire wire, final String flsh, final String path) { | |
| 119 | 0 | this(wire, flsh, new FcCache(path)); |
| 120 | 0 | } |
| 121 | ||
| 122 | /** | |
| 123 | * Public ctor. | |
| 124 | * @param wire Original wire | |
| 125 | * @param flsh Flushing regular expression | |
| 126 | * @param fcc Cache | |
| 127 | */ | |
| 128 | 3 | public FcWire(final Wire wire, final String flsh, final FcCache fcc) { |
| 129 | 3 | this.origin = wire; |
| 130 | 3 | this.regex = flsh; |
| 131 | 3 | this.cache = fcc; |
| 132 | 3 | } |
| 133 | ||
| 134 | // @checkstyle ParameterNumber (5 lines) | |
| 135 | @Override | |
| 136 | public Response send(final Request req, final String home, | |
| 137 | final String method, | |
| 138 | final Collection<Map.Entry<String, String>> headers, | |
| 139 | final InputStream content, | |
| 140 | final int connect, | |
| 141 | final int read) throws IOException { | |
| 142 | 16 | final URI uri = req.uri().get(); |
| 143 | 16 | final StringBuilder label = new StringBuilder(Tv.HUNDRED) |
| 144 | .append(method).append(' ').append(uri.getPath()); | |
| 145 | 16 | if (uri.getQuery() != null) { |
| 146 | 1 | label.append('?').append(uri.getQuery()); |
| 147 | } | |
| 148 | 16 | if (label.toString().matches(this.regex)) { |
| 149 | 1 | this.cache.invalidate(); |
| 150 | } | |
| 151 | final Response rsp; | |
| 152 | 16 | if (method.equals(Request.GET)) { |
| 153 | 13 | rsp = this.cache.get( |
| 154 | label.toString(), this.origin, req, | |
| 155 | home, method, headers, content, connect, read | |
| 156 | ); | |
| 157 | } else { | |
| 158 | 3 | rsp = this.origin.send( |
| 159 | req, home, method, headers, content, | |
| 160 | connect, read | |
| 161 | ); | |
| 162 | } | |
| 163 | 16 | return rsp; |
| 164 | } | |
| 165 | ||
| 166 | } |