Coverage Report - com.jcabi.http.wire.TrustedWire
 
Classes in this File Line Coverage Branch Coverage Complexity
TrustedWire
63%
12/19
0%
0/12
2
TrustedWire$1
25%
1/4
N/A
2
 
 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.Request;
 34  
 import com.jcabi.http.Response;
 35  
 import com.jcabi.http.Wire;
 36  
 import java.io.IOException;
 37  
 import java.io.InputStream;
 38  
 import java.security.KeyManagementException;
 39  
 import java.security.NoSuchAlgorithmException;
 40  
 import java.security.SecureRandom;
 41  
 import java.security.cert.X509Certificate;
 42  
 import java.util.Collection;
 43  
 import java.util.Map;
 44  
 import javax.net.ssl.HttpsURLConnection;
 45  
 import javax.net.ssl.SSLContext;
 46  
 import javax.net.ssl.SSLSocketFactory;
 47  
 import javax.net.ssl.TrustManager;
 48  
 import javax.net.ssl.X509TrustManager;
 49  
 import lombok.EqualsAndHashCode;
 50  
 import lombok.ToString;
 51  
 
 52  
 /**
 53  
  * Wire that ignores SSL PKIX verifications.
 54  
  *
 55  
  * <p>This wire ignores :
 56  
  *
 57  
  * <pre> String html = new JdkRequest("http://goggle.com")
 58  
  *   .through(TrustedWire.class)
 59  
  *   .fetch()
 60  
  *   .body();</pre>
 61  
  *
 62  
  * <p>The class is immutable and thread-safe.
 63  
  *
 64  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 65  
  * @version $Id: 9b169ecd75868ee51b9d158ab25ec822783f42a4 $
 66  
  * @since 1.10
 67  
  */
 68  
 @Immutable
 69  0
 @ToString(of = "origin")
 70  0
 @EqualsAndHashCode(of = "origin")
 71  
 public final class TrustedWire implements Wire {
 72  
 
 73  
     /**
 74  
      * Trust manager.
 75  
      */
 76  1
     private static final TrustManager MANAGER = new X509TrustManager() {
 77  
         @Override
 78  
         public X509Certificate[] getAcceptedIssuers() {
 79  0
             return new X509Certificate[0];
 80  
         }
 81  
         @Override
 82  
         public void checkClientTrusted(final X509Certificate[] certs,
 83  
             final String type) {
 84  
             // nothing to check here
 85  0
         }
 86  
         @Override
 87  
         public void checkServerTrusted(final X509Certificate[] certs,
 88  
             final String types) {
 89  
             // nothing to check here
 90  0
         }
 91  
     };
 92  
 
 93  
     /**
 94  
      * Original wire.
 95  
      */
 96  
     private final transient Wire origin;
 97  
 
 98  
     /**
 99  
      * Public ctor.
 100  
      * @param wire Original wire
 101  
      */
 102  1
     public TrustedWire(final Wire wire) {
 103  1
         this.origin = wire;
 104  1
     }
 105  
 
 106  
     // @checkstyle ParameterNumber (13 lines)
 107  
     @Override
 108  
     public Response send(final Request req, final String home,
 109  
         final String method,
 110  
         final Collection<Map.Entry<String, String>> headers,
 111  
         final InputStream content,
 112  
         final int connect, final int read) throws IOException {
 113  1
         synchronized (TrustedWire.class) {
 114  1
             final SSLSocketFactory def =
 115  
                 HttpsURLConnection.getDefaultSSLSocketFactory();
 116  
             try {
 117  1
                 HttpsURLConnection.setDefaultSSLSocketFactory(
 118  
                     TrustedWire.context().getSocketFactory()
 119  
                 );
 120  1
                 return this.origin.send(
 121  
                     req, home, method, headers, content,
 122  
                     connect, read
 123  
                 );
 124  
             } finally {
 125  1
                 HttpsURLConnection.setDefaultSSLSocketFactory(def);
 126  
             }
 127  0
         }
 128  
     }
 129  
 
 130  
     /**
 131  
      * Create context.
 132  
      * @return Context
 133  
      */
 134  
     private static SSLContext context() {
 135  
         try {
 136  1
             final SSLContext ctx = SSLContext.getInstance("SSL");
 137  1
             ctx.init(
 138  
                 null,
 139  
                 new TrustManager[] {TrustedWire.MANAGER},
 140  
                 new SecureRandom()
 141  
             );
 142  1
             return ctx;
 143  0
         } catch (final KeyManagementException ex) {
 144  0
             throw new IllegalStateException(ex);
 145  0
         } catch (final NoSuchAlgorithmException ex) {
 146  0
             throw new IllegalStateException(ex);
 147  
         }
 148  
     }
 149  
 
 150  
 }