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