Coverage Report - com.jcabi.http.wire.BasicAuthWire
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicAuthWire
66%
12/18
15%
3/20
3
 
 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.http.ImmutableHeader;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.Response;
 36  
 import com.jcabi.http.Wire;
 37  
 import com.jcabi.log.Logger;
 38  
 import java.io.IOException;
 39  
 import java.io.InputStream;
 40  
 import java.net.URI;
 41  
 import java.nio.charset.Charset;
 42  
 import java.util.Collection;
 43  
 import java.util.LinkedList;
 44  
 import java.util.Map;
 45  
 import javax.ws.rs.core.HttpHeaders;
 46  
 import javax.xml.bind.DatatypeConverter;
 47  
 import lombok.EqualsAndHashCode;
 48  
 import lombok.ToString;
 49  
 
 50  
 /**
 51  
  * Wire with HTTP basic authentication based on user info of URI.
 52  
  *
 53  
  * <p>This wire converts user info from URI into
 54  
  * {@code "Authorization"} HTTP header, for example:
 55  
  *
 56  
  * <pre> String html = new JdkRequest("http://jeff:12345@example.com")
 57  
  *   .through(BasicAuthWire.class)
 58  
  *   .fetch()
 59  
  *   .body();</pre>
 60  
  *
 61  
  * <p>In this example, an additional HTTP header {@code Authorization}
 62  
  * will be added with a value {@code Basic amVmZjoxMjM0NQ==}.
 63  
  *
 64  
  * <p>The class is immutable and thread-safe.
 65  
  *
 66  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 67  
  * @version $Id: 9655873db4b9f4d3f8066c1445005df793072a6d $
 68  
  * @since 0.10
 69  
  * @see <a href="http://tools.ietf.org/html/rfc2617">RFC 2617 "HTTP Authentication: Basic and Digest Access Authentication"</a>
 70  
  */
 71  
 @Immutable
 72  0
 @ToString(of = "origin")
 73  0
 @EqualsAndHashCode(of = "origin")
 74  
 public final class BasicAuthWire implements Wire {
 75  
 
 76  
     /**
 77  
      * The encoding to use.
 78  
      */
 79  
     private static final String ENCODING = "UTF-8";
 80  
 
 81  
     /**
 82  
      * The Charset to use.
 83  
      */
 84  1
     private static final Charset CHARSET =
 85  
         Charset.forName(BasicAuthWire.ENCODING);
 86  
 
 87  
     /**
 88  
      * Original wire.
 89  
      */
 90  
     private final transient Wire origin;
 91  
 
 92  
     /**
 93  
      * Public ctor.
 94  
      * @param wire Original wire
 95  
      */
 96  5
     public BasicAuthWire(final Wire wire) {
 97  5
         this.origin = wire;
 98  5
     }
 99  
 
 100  
     // @checkstyle ParameterNumber (7 lines)
 101  
     @Override
 102  
     public Response send(final Request req, final String home,
 103  
         final String method,
 104  
         final Collection<Map.Entry<String, String>> headers,
 105  
         final InputStream content,
 106  
         final int connect,
 107  
         final int read) throws IOException {
 108  5
         final Collection<Map.Entry<String, String>> hdrs =
 109  
             new LinkedList<Map.Entry<String, String>>();
 110  5
         boolean absent = true;
 111  5
         for (final Map.Entry<String, String> header : headers) {
 112  0
             if (header.getKey().equals(HttpHeaders.AUTHORIZATION)) {
 113  0
                 absent = false;
 114  
             }
 115  0
             hdrs.add(header);
 116  0
         }
 117  5
         final String info = URI.create(home).getUserInfo();
 118  5
         if (absent && info != null) {
 119  5
             final String[] parts = info.split(":", 2);
 120  5
             hdrs.add(
 121  
                 new ImmutableHeader(
 122  
                     HttpHeaders.AUTHORIZATION,
 123  
                     Logger.format(
 124  
                         "Basic %s",
 125  
                         DatatypeConverter.printBase64Binary(
 126  
                             Logger.format(
 127  
                                 "%s:%s",
 128  
                                 parts[0],
 129  
                                 parts[1]
 130  
                             ).getBytes(BasicAuthWire.CHARSET)
 131  
                         )
 132  
                     )
 133  
                 )
 134  
             );
 135  
         }
 136  5
         return this.origin.send(
 137  
             req, home, method, hdrs, content, connect, read
 138  
         );
 139  
     }
 140  
 }