Merge pull request #19455 from 2beaucoup/remove-java6-compat

Remove java6 compat
This commit is contained in:
Konrad Malawski 2016-01-14 18:50:53 +01:00
commit ba1240e28f
19 changed files with 48 additions and 258 deletions

View file

@ -40,6 +40,7 @@ public final class FormData {
/**
* Creates the FormData from the given parameters.
*/
@SafeVarargs
public static FormData create(Pair<String, String>... params) {
return new FormData(Query.create(params));
}

View file

@ -94,6 +94,7 @@ public abstract class Query {
/**
* Returns a Query from the given parameters.
*/
@SafeVarargs
public static Query create(Pair<String, String>... params) {
return new JavaQuery(UriJavaAccessor.queryApply(params));
}

View file

@ -1,60 +0,0 @@
/*
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.impl.util
import java.lang.reflect.{ InvocationTargetException, Method }
import java.net.InetSocketAddress
import scala.util.control.NonFatal
/**
* Provides getHostString support for Java 6.
*
* TODO: can be removed once support for Java 6 is dropped.
*
* Internal API
*/
private[http] class EnhancedInetSocketAddress(val address: InetSocketAddress) extends AnyVal {
/**
* Retrieve the original host string that was given (IP or DNS name) if the current JDK has
* a `getHostString` method with the right signature that can be made accessible.
*
* This avoids a reverse DNS query from calling getHostName() if the original host string is an IP address.
* If the reflective call doesn't work it falls back to getHostName.
*/
def getHostStringJava6Compatible: String = EnhancedInetSocketAddress.getHostStringFunction(address)
}
/**
* Internal API
*/
private[http] object EnhancedInetSocketAddress {
private[http] val getHostStringFunction: InetSocketAddress String = {
def fallbackToGetHostName = (_: InetSocketAddress).getHostName
def callReflectively(m: Method) =
(address: InetSocketAddress)
try m.invoke(address).asInstanceOf[String]
catch {
case ite: InvocationTargetException throw ite.getTargetException
}
try {
val m = classOf[InetSocketAddress].getDeclaredMethod("getHostString")
val candidate =
if (m.getReturnType == classOf[String] && m.getParameterTypes.isEmpty) {
if (!m.isAccessible) m.setAccessible(true)
callReflectively(m)
} else fallbackToGetHostName
// probe so that we can be sure a reflective problem only turns up once
// here during construction
candidate(new InetSocketAddress("127.0.0.1", 80))
candidate
} catch {
case NonFatal(_) fallbackToGetHostName
}
}
}

View file

@ -1,60 +0,0 @@
/*
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.impl.util
import java.lang.reflect.{ InvocationTargetException, Method }
import javax.net.ssl.SSLParameters
import scala.util.control.NonFatal
/**
* INTERNAL API
*
* Enables accessing SslParameters even if compiled against Java 6.
*/
private[http] object Java6Compat {
def isJava6: Boolean =
System.getProperty("java.version").take(4) match {
case "1.6." true
case _ false
}
/**
* Returns true if setting the algorithm was successful.
*/
def trySetEndpointIdentificationAlgorithm(parameters: SSLParameters, algorithm: String): Boolean =
setEndpointIdentificationAlgorithmFunction(parameters, algorithm)
private[this] val setEndpointIdentificationAlgorithmFunction: (SSLParameters, String) Boolean = {
def unsupported: (SSLParameters, String) Boolean = (_, _) false
def callReflectively(m: Method) =
(params: SSLParameters, algorithm: String)
try {
m.invoke(params, algorithm)
true
} catch {
case ite: InvocationTargetException throw ite.getTargetException
}
try {
val m = classOf[SSLParameters].getMethod("setEndpointIdentificationAlgorithm", classOf[java.lang.String])
val candidate =
if (m.getReturnType == Void.TYPE && m.getParameterTypes.toSeq == Seq(classOf[java.lang.String])) {
if (!m.isAccessible) m.setAccessible(true)
callReflectively(m)
} else unsupported
// probe so that we can be sure a reflective problem only turns up once
// here during construction
candidate(new SSLParameters(), "https")
candidate
} catch {
case NonFatal(_) unsupported
}
}
}

View file

@ -49,7 +49,7 @@ private[http] abstract class SettingsCompanion[T](protected val prefix: String)
private[http] object SettingsCompanion {
lazy val configAdditions: Config = {
val localHostName =
try new InetSocketAddress(InetAddress.getLocalHost, 80).getHostStringJava6Compatible
try new InetSocketAddress(InetAddress.getLocalHost, 80).getHostString
catch { case NonFatal(_) "" }
ConfigFactory.parseMap(Map("akka.http.hostname" -> localHostName).asJava)
}

View file

@ -4,12 +4,9 @@
package akka.http.impl
import java.net.InetSocketAddress
import language.implicitConversions
import language.higherKinds
import java.nio.charset.Charset
import java.util.concurrent.atomic.AtomicInteger
import com.typesafe.config.Config
import akka.stream.scaladsl.{ Flow, Source }
import akka.stream.stage._
@ -18,7 +15,6 @@ import scala.concurrent.{ Await, Future }
import scala.reflect.ClassTag
import scala.util.{ Failure, Success }
import scala.util.matching.Regex
import akka.event.LoggingAdapter
import akka.util.ByteString
import akka.actor._
@ -40,8 +36,6 @@ package object util {
private[http] implicit def enhanceConfig(config: Config): EnhancedConfig = new EnhancedConfig(config)
private[http] implicit def enhanceString_(s: String): EnhancedString = new EnhancedString(s)
private[http] implicit def enhanceRegex(regex: Regex): EnhancedRegex = new EnhancedRegex(regex)
private[http] implicit def enhanceInetSocketAddress(address: InetSocketAddress): EnhancedInetSocketAddress =
new EnhancedInetSocketAddress(address)
private[http] implicit def enhanceByteStrings(byteStrings: TraversableOnce[ByteString]): EnhancedByteStringTraversableOnce =
new EnhancedByteStringTraversableOnce(byteStrings)
private[http] implicit def enhanceByteStringsMat[Mat](byteStrings: Source[ByteString, Mat]): EnhancedByteStringSource[Mat] =
@ -75,7 +69,7 @@ package object util {
private[http] def installEventStreamLoggerFor(channel: Class[_])(implicit system: ActorSystem): Unit = {
synchronized {
if (eventStreamLogger == null)
eventStreamLogger = system.actorOf(Props[util.EventStreamLogger].withDeploy(Deploy.local), name = "event-stream-logger")
eventStreamLogger = system.actorOf(Props[util.EventStreamLogger]().withDeploy(Deploy.local), name = "event-stream-logger")
}
system.eventStream.subscribe(eventStreamLogger, channel)
}
@ -178,6 +172,4 @@ package util {
}
}
}
private[http] class ReadTheDocumentationException(message: String) extends RuntimeException(message)
}

View file

@ -16,7 +16,7 @@ import akka.http.impl.engine.HttpConnectionTimeoutException
import akka.http.impl.engine.client._
import akka.http.impl.engine.server._
import akka.http.impl.engine.ws.WebsocketClientBlueprint
import akka.http.impl.util.{ Java6Compat, ReadTheDocumentationException, StreamUtils }
import akka.http.impl.util.StreamUtils
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.Host
import akka.http.scaladsl.model.ws.{ WebsocketUpgradeResponse, WebsocketRequest, Message }
@ -770,12 +770,7 @@ trait DefaultSSLContextCreation {
defaultParams.setCipherSuites(cipherSuites)
// hostname!
if (!Java6Compat.trySetEndpointIdentificationAlgorithm(defaultParams, "https")) {
log.info("Unable to use JDK built-in hostname verification, please consider upgrading your Java runtime to " +
"a more up to date version (JDK7+). Using Typesafe ssl-config hostname verification.")
// enabling the JDK7+ solution did not work, however this is fine since we do handle hostname
// verification directly in SslTlsCipherActor manually by applying an ssl-config provider verifier
}
defaultParams.setEndpointIdentificationAlgorithm("https")
HttpsContext(sslContext, sslParameters = Some(defaultParams))
}

View file

@ -10,7 +10,6 @@ import java.security.MessageDigest
import java.util
import javax.net.ssl.SSLSession
import akka.event.Logging
import akka.stream.io.ScalaSessionAPI
import scala.reflect.ClassTag
@ -151,7 +150,7 @@ sealed abstract case class Expect private () extends ModeledHeader {
// http://tools.ietf.org/html/rfc7230#section-5.4
object Host extends ModeledCompanion[Host] {
def apply(authority: Uri.Authority): Host = apply(authority.host, authority.port)
def apply(address: InetSocketAddress): Host = apply(address.getHostStringJava6Compatible, address.getPort)
def apply(address: InetSocketAddress): Host = apply(address.getHostString, address.getPort)
def apply(host: String): Host = apply(host, 0)
def apply(host: String, port: Int): Host = apply(Uri.Host(host), port)
val empty = Host("")

View file

@ -69,19 +69,14 @@ class TlsEndpointVerificationSpec extends AkkaSpec("""
val ex = intercept[Exception] {
Http().singleRequest(req).futureValue
}
if (Java6Compat.isJava6) {
// our manual verification
ex.getMessage should include("Hostname verification failed")
} else {
// JDK built-in verification
val expectedMsg = "No subject alternative DNS name matching www.howsmyssl.com found"
// JDK built-in verification
val expectedMsg = "No subject alternative DNS name matching www.howsmyssl.com found"
var e: Throwable = ex
while (e.getCause != null) e = e.getCause
var e: Throwable = ex
while (e.getCause != null) e = e.getCause
info("TLS failure cause: " + e.getMessage)
e.getMessage should include(expectedMsg)
}
info("TLS failure cause: " + e.getMessage)
e.getMessage should include(expectedMsg)
}
"pass hostname verification on https://www.playframework.com/" in {

View file

@ -1,34 +0,0 @@
/*
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.impl.util
import java.net.{ InetAddress, InetSocketAddress }
import org.scalatest.{ Matchers, WordSpec }
class EnhancedInetSocketAddressSpec extends WordSpec with Matchers {
"getHostStringJava6Compatible" should {
"return IPv4 address if InetSocketAddress was created with the address" in {
val addr = likelyReverseResolvableAddress
val socketAddress = new InetSocketAddress(addr, 80)
socketAddress.getHostStringJava6Compatible shouldEqual addr.getHostAddress
}
"return host name if InetSocketAddress was created with host name" in {
val address = new InetSocketAddress("github.com", 80)
address.getHostStringJava6Compatible shouldEqual "github.com"
}
}
/**
* Returns an InetAddress that can likely be reverse looked up, so that
* getHostName returns a DNS address and not the IP. Unfortunately, we
* cannot be sure that a host name was already cached somewhere in which
* case getHostString may still return a host name even without doing
* a reverse lookup at this time. If this start to fail non-deterministically,
* it may be decided that this test needs to be disabled.
*/
def likelyReverseResolvableAddress: InetAddress =
InetAddress.getByAddress(InetAddress.getByName("google.com").getAddress)
}

View file

@ -43,7 +43,7 @@ object ExampleHttpContexts {
context.init(null, certManagerFactory.getTrustManagers, new SecureRandom)
val params = new SSLParameters()
Java6Compat.trySetEndpointIdentificationAlgorithm(params, "https")
params.setEndpointIdentificationAlgorithm("https")
HttpsContext(context, sslParameters = Some(params))
}