java.net.HttpURLConnection Scala Examples
The following examples show how to use java.net.HttpURLConnection.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ComponentsFixture.scala From daml with Apache License 2.0 | 6 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator.test import java.util.concurrent.atomic.AtomicReference import com.daml.navigator.test.config.Arguments import com.daml.navigator.test.runner.{HeadNavigator, PackagedDamlc, PackagedSandbox} import com.typesafe.scalalogging.LazyLogging import scala.io.Source import scala.util.{Failure, Success, Try} class ComponentsFixture( val args: Arguments, val navigatorPort: Int, val sandboxPort: Int, val scenario: String ) extends LazyLogging { // A list of commands on how to destroy started processes private val killProcs: AtomicReference[List[Unit => Unit]] = new AtomicReference(List.empty) private val onlineUrl = s"http://localhost:$navigatorPort/api/about" private def get( url: String, connectTimeout: Int = 1000, readTimeout: Int = 1000, requestMethod: String = "GET" ): String = { import java.net.{URL, HttpURLConnection} val connection = (new URL(url)).openConnection.asInstanceOf[HttpURLConnection] connection.setConnectTimeout(connectTimeout) connection.setReadTimeout(readTimeout) connection.setRequestMethod(requestMethod) val inputStream = connection.getInputStream val content = Source.fromInputStream(inputStream).mkString if (inputStream != null) inputStream.close() content } def startup(): Try[Unit] = { if (args.startComponents) { logger.info("Starting the sandbox and the Navigator") for { (darFile, tempFiles) <- Try(PackagedDamlc.run(args.damlPath)) sandbox <- Try(PackagedSandbox.runAsync(sandboxPort, darFile, scenario)) _ = killProcs.updateAndGet(s => sandbox :: s) navigator <- Try( HeadNavigator.runAsync(args.navConfPAth, args.navigatorDir, navigatorPort, sandboxPort)) _ = killProcs.updateAndGet(s => navigator :: s) } yield { () } } else { Success(()) } } private def retry[R](action: => R, maxRetries: Int, delayMillis: Int): Try[R] = { def retry0(count: Int): Try[R] = { Try(action) match { case Success(r) => Success(r) case Failure(e) => if (count > maxRetries) { logger.error( s"Navigator is not available after $maxRetries retries with $delayMillis millis interval.") Failure(e) } else { logger.info(s"Navigator is not available yet, waiting $delayMillis millis ") Thread.sleep(delayMillis.toLong) retry0(count + 1) } } } retry0(0) } def waitForNavigator(): Try[Unit] = { logger.info(s"Waiting for the Navigator to start up (waiting for $onlineUrl)") retry({ get(onlineUrl); () }, 120, 1000) } def shutdown(): Unit = { killProcs.getAndUpdate(procs => { procs.foreach(killAction => Try { killAction(()) }) List.empty }) () } }
Example 2
Source File: MasterWebUISuite.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy.master.ui import java.io.DataOutputStream import java.net.{HttpURLConnection, URL} import java.nio.charset.StandardCharsets import java.util.Date import scala.collection.mutable.HashMap import org.mockito.Mockito.{mock, times, verify, when} import org.scalatest.BeforeAndAfterAll import org.apache.spark.{SecurityManager, SparkConf, SparkFunSuite} import org.apache.spark.deploy.DeployMessages.{KillDriverResponse, RequestKillDriver} import org.apache.spark.deploy.DeployTestUtils._ import org.apache.spark.deploy.master._ import org.apache.spark.rpc.{RpcEndpointRef, RpcEnv} class MasterWebUISuite extends SparkFunSuite with BeforeAndAfterAll { val conf = new SparkConf val securityMgr = new SecurityManager(conf) val rpcEnv = mock(classOf[RpcEnv]) val master = mock(classOf[Master]) val masterEndpointRef = mock(classOf[RpcEndpointRef]) when(master.securityMgr).thenReturn(securityMgr) when(master.conf).thenReturn(conf) when(master.rpcEnv).thenReturn(rpcEnv) when(master.self).thenReturn(masterEndpointRef) val masterWebUI = new MasterWebUI(master, 0) override def beforeAll() { super.beforeAll() masterWebUI.bind() } override def afterAll() { masterWebUI.stop() super.afterAll() } test("kill application") { val appDesc = createAppDesc() // use new start date so it isn't filtered by UI val activeApp = new ApplicationInfo( new Date().getTime, "app-0", appDesc, new Date(), null, Int.MaxValue) when(master.idToApp).thenReturn(HashMap[String, ApplicationInfo]((activeApp.id, activeApp))) val url = s"http://localhost:${masterWebUI.boundPort}/app/kill/" val body = convPostDataToString(Map(("id", activeApp.id), ("terminate", "true"))) val conn = sendHttpRequest(url, "POST", body) conn.getResponseCode // Verify the master was called to remove the active app verify(master, times(1)).removeApplication(activeApp, ApplicationState.KILLED) } test("kill driver") { val activeDriverId = "driver-0" val url = s"http://localhost:${masterWebUI.boundPort}/driver/kill/" val body = convPostDataToString(Map(("id", activeDriverId), ("terminate", "true"))) val conn = sendHttpRequest(url, "POST", body) conn.getResponseCode // Verify that master was asked to kill driver with the correct id verify(masterEndpointRef, times(1)).ask[KillDriverResponse](RequestKillDriver(activeDriverId)) } private def convPostDataToString(data: Map[String, String]): String = { (for ((name, value) <- data) yield s"$name=$value").mkString("&") } private def sendHttpRequest( url: String, method: String, body: String = ""): HttpURLConnection = { val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod(method) if (body.nonEmpty) { conn.setDoOutput(true) conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") conn.setRequestProperty("Content-Length", Integer.toString(body.length)) val out = new DataOutputStream(conn.getOutputStream) out.write(body.getBytes(StandardCharsets.UTF_8)) out.close() } conn } }
Example 3
Source File: package.scala From sbt-idea-plugin with Apache License 2.0 | 5 votes |
package org.jetbrains.sbtidea import java.net.{HttpURLConnection, URL} import java.nio.file.{Files, Path} import com.eclipsesource.json.Json import org.jetbrains.sbtidea.Keys._ import org.jetbrains.sbtidea.pathToPathExt import sbt._ package object download { case class BuildInfo(buildNumber: String, edition: IntelliJPlatform, jbrVersion: Option[String]) { override def toString: String = s"BuildInfo($edition-$buildNumber)" } def withConnection[V](url: URL)(f: => HttpURLConnection => V): V = { var connection: HttpURLConnection = null try { connection = url.openConnection().asInstanceOf[HttpURLConnection] f(connection) } finally { try { if (connection != null) connection.disconnect() } catch { case e: Exception => println(s"Failed to close connection $url: ${e.getMessage}") } } } implicit class BuildInfoOps(val buildInfo: BuildInfo) { def getActualIdeaBuild(ideaRoot: Path): String = { val productInfo = ideaRoot / "product-info.json" if (buildInfo.buildNumber.count(_ == '.') < 2 && productInfo.exists) { // most likely some LATEST-EAP-SNAPSHOT kind of version try { val content = new String(Files.readAllBytes(productInfo)) val parsed = Json.parse(content) parsed.asObject().getString("buildNumber", buildInfo.buildNumber) } catch { case _: Throwable => buildInfo.buildNumber } } else buildInfo.buildNumber } } }
Example 4
Source File: AttachmentService.scala From BacklogMigration-Redmine with MIT License | 5 votes |
package com.nulabinc.backlog.r2b.exporter.service import java.io.{File, FileOutputStream} import java.net.{HttpURLConnection, URL} import java.nio.channels.Channels import com.nulabinc.backlog.migration.common.utils.ControlUtil.using import com.nulabinc.backlog.migration.common.utils.Logging object AttachmentService extends Logging { private val MAX_REDIRECT_COUNT = 10 def download(url: URL, file: File): Unit = { val redirected = followRedirect(url) doDownload(redirected, file) } private def doDownload(url: URL, file: File): Unit = try { val rbc = Channels.newChannel(url.openStream()) val fos = new FileOutputStream(file) fos.getChannel.transferFrom(rbc, 0, java.lang.Long.MAX_VALUE) rbc.close() fos.close() } catch { case e: Throwable => logger.warn("Download attachment failed: " + e.getMessage) } private def followRedirect(url: URL, count: Int = 0): URL = url.openConnection match { case http: HttpURLConnection => http.setRequestMethod("GET") http.connect() using(http) { connection => connection.getResponseCode match { case 301 | 302 | 303 => val newUrl = new URL(connection.getHeaderField("Location")) if (count < MAX_REDIRECT_COUNT) followRedirect(newUrl, count + 1) else newUrl case _ => url } } case _ => url } }
Example 5
Source File: DefaultBintrayRepoConnector.scala From releaser with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.releaser.bintray import java.net.{HttpURLConnection, URL} import java.nio.file.Path import play.api.libs.json.{JsValue, Json} import uk.gov.hmrc.{FileDownloader, Logger, ServiceCredentials} import scala.util.{Failure, Success, Try} object BintrayRepoConnector extends Logger { def apply(bintrayCreds: ServiceCredentials, workDir : Path): BintrayRepoConnector = new DefaultBintrayRepoConnector(workDir, new BintrayHttp(bintrayCreds), new FileDownloader()) def dryRun(bintrayCreds: ServiceCredentials, workDir : Path) = { log.info("Bintray : running in dry-run mode") val dryRunHttp = new BintrayHttp(bintrayCreds){ override def emptyPost(url:String): Try[Unit] = { println("BintrayHttp emptyPost DRY_RUN");Success(Unit)} override def putFile(version: VersionDescriptor, file: Path, url: String): Try[Unit] = { println("BintrayHttp putFile DRY_RUN");Success(Unit) } } new DefaultBintrayRepoConnector(workDir, dryRunHttp, new FileDownloader()) } } trait BintrayRepoConnector { def findJar(jarFileName: String, jarUrl: String, version: VersionDescriptor): Option[Path] def publish(version: VersionDescriptor): Try[Unit] def downloadFile(url: String, fileName: String): Try[Path] def uploadFile(version: VersionDescriptor, filePath: Path, url: String): Try[Unit] def verifyTargetDoesNotExist(version: VersionDescriptor): Try[Unit] def findFiles(version: VersionDescriptor): Try[List[String]] def getRepoMetaData(repoName:String, artefactName: String): Try[Unit] } class DefaultBintrayRepoConnector(workDir: Path, bintrayHttp: BintrayHttp, fileDownloader: FileDownloader) extends BintrayRepoConnector with Logger { def publish(version: VersionDescriptor):Try[Unit] = { val url = BintrayPaths.publishUrlFor(version) bintrayHttp.emptyPost(url) } def verifyTargetDoesNotExist(version: VersionDescriptor): Try[Unit] = { val url = BintrayPaths.fileListUrlFor(version) log.info(s"Bintray : checking to see if $url exists") val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod("HEAD") conn.connect() conn.getResponseCode match { case 200 => Failure(new IllegalArgumentException(s"${version.artefactName} ${version.version} already exists")) case _ => Success() } } def findJar(jarFileName: String, jarUrl: String, version: VersionDescriptor): Option[Path] = { downloadFile(jarUrl, jarFileName) match { case Success(x) => Some(x) case Failure(y) => None } } def uploadFile(version: VersionDescriptor, filePath: Path, url: String): Try[Unit] = { bintrayHttp.putFile(version, filePath, url) } def downloadFile(url: String, fileName: String): Try[Path] = { val targetFile = workDir.resolve(fileName) fileDownloader.url2File(url, targetFile) map { unit => targetFile } } def findFiles(version: VersionDescriptor): Try[List[String]] = { val url = BintrayPaths.fileListUrlFor(version) bintrayHttp.get(url).map { st => val fileNames: Seq[JsValue] = Json.parse(st) \\ "path" fileNames.map(_.as[String]).toList } } def getRepoMetaData(repoName:String, artefactName: String): Try[Unit] = { val url = BintrayPaths.metadata(repoName, artefactName) bintrayHttp.get(url).map { _ => Unit} } }
Example 6
Source File: TextDisplay.scala From almond with BSD 3-Clause "New" or "Revised" License | 5 votes |
package almond.display import java.io.{ByteArrayOutputStream, InputStream} import java.net.{HttpURLConnection, URL, URLConnection} import java.nio.charset.{Charset, StandardCharsets} import scala.util.Try abstract class TextDisplay extends UpdatableDisplay { def contentOrUrl: Either[URL, String] def content: Option[String] = contentOrUrl.right.toOption def url: Option[URL] = contentOrUrl.left.toOption def finalContent: String = contentOrUrl match { case Left(url) => TextDisplay.urlContent(url) case Right(c) => c } def withContent(code: String): UpdatableDisplay def withUrl(url: String): UpdatableDisplay } object TextDisplay { type Builder[T] = Display.Builder[String, T] private[almond] def readFully(is: InputStream): Array[Byte] = { val buffer = new ByteArrayOutputStream val data = Array.ofDim[Byte](16384) var nRead = 0 while ( { nRead = is.read(data, 0, data.length) nRead != -1 }) buffer.write(data, 0, nRead) buffer.flush() buffer.toByteArray } def urlContent(url: URL): String = { var conn: URLConnection = null val (rawContent, charsetOpt) = try { conn = url.openConnection() conn.setConnectTimeout(5000) // allow users to tweak that? val b = readFully(conn.getInputStream) val charsetOpt0 = conn match { case conn0: HttpURLConnection => conn0 .getContentType .split(';') .map(_.trim) .find(_.startsWith("charset=")) .map(_.stripPrefix("charset=")) .filter(Charset.isSupported) .map(Charset.forName) case _ => None } (b, charsetOpt0) } finally { if (conn != null) { Try(conn.getInputStream.close()) conn match { case conn0: HttpURLConnection => Try(conn0.getErrorStream.close()) Try(conn0.disconnect()) case _ => } } } new String(rawContent, charsetOpt.getOrElse(StandardCharsets.UTF_8)) } }
Example 7
Source File: SparkSvc.scala From Mastering-Spark-for-Data-Science with MIT License | 5 votes |
package svc import java.io.StringWriter import java.net.{HttpURLConnection, URL} import com.typesafe.config.ConfigFactory import io.gzet.recommender.Node import org.apache.commons.io.IOUtils import play.api.Logger import play.api.libs.json._ class SparkSvc() { val config = ConfigFactory.load() val host = config.getString("spark.job.server.host") val port = config.getInt("spark.job.server.port") val timeout = config.getInt("spark.job.server.timeout") val appName = config.getString("spark.job.server.app") val context = config.getString("spark.job.server.context") val indexJob = config.getString("spark.job.index") val playlistJob = config.getString("spark.job.playlist") val playlistRecommendJob = config.getString("spark.job.personalized.playlist") private def getConnection(endpoint: String, params: Option[String]) = { try { val url = new URL(endpoint) val connection = url.openConnection().asInstanceOf[HttpURLConnection] connection.setDoOutput(true) connection.setRequestMethod("POST") connection.setRequestProperty("Accept", "application/json") if(params.isDefined){ val os = connection.getOutputStream os.write(params.get.getBytes()) os.flush() os.close() } val inputStream = connection.getInputStream val writer = new StringWriter() IOUtils.copy(inputStream, writer, "UTF-8") val ret = writer.toString Json.parse(ret) } catch { case e: Exception => throw new Exception("Job Failed: " + e.getMessage) } } private def parseResponse(json: JsValue) : String = { val jobId = (json \ "result" \ "jobId").asOpt[String] if(jobId.isDefined){ s"Job submitted [${jobId.get}]" } else { val message = (json \ "result" \ "message").asOpt[String] if(message.isDefined){ throw new Exception(s"Job failed: ${message.get}") } throw new Exception("Could not find Spark job id") } } def index(path: String): String = { Logger.info("Submitting INDEX job") val url = s"http://$host:$port/jobs?appName=$appName&classPath=$indexJob&context=$context" val params = "input.dir=\"" + path + "\"" val json = getConnection(url, Some(params)) parseResponse(json) } def playlist() = { Logger.info("Submitting PLAYLIST job") val url = s"http://$host:$port/jobs?appName=$appName&classPath=$playlistJob&context=$context" val json = getConnection(url, None) parseResponse(json) } def playlist(id: Long) = { Logger.info("Submitting RECOMMEND job") val url = s"http://$host:$port/jobs?appName=$appName&classPath=$playlistRecommendJob&context=$context&sync=true&timeout=$timeout" val params = s"song.id=$id" val json: JsValue = getConnection(url, Some(params)) val array = (json \ "result").as[Array[String]] array.map({line => val Array(id, pr, song) = line.split(",").take(3) Node(id.toLong, song, pr.toDouble) }).toList } }
Example 8
Source File: ServerHostTest.scala From polynote with Apache License 2.0 | 5 votes |
package polynote.server import java.net.{HttpURLConnection, InetAddress, InetSocketAddress, URL} import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import polynote.app.{App, Args, Environment, MainArgs} import polynote.config._ import polynote.kernel.{BaseEnv, Kernel} import polynote.kernel.environment.Config import polynote.kernel.environment.Env.LayerOps import polynote.kernel.interpreter.Interpreter import polynote.kernel.logging.Logging import polynote.server.auth.IdentityProvider import polynote.server.repository.NotebookRepository import polynote.server.repository.fs.FileSystems import polynote.testing.{ConfiguredZIOSpec, ZIOSpec} import zio.{RIO, Task, ZIO, ZLayer} import zio.blocking.effectBlocking class ServerHostTest extends FreeSpec with Matchers with ConfiguredZIOSpec with MockFactory { override val config: PolynoteConfig = PolynoteConfig( listen = Listen(host = "0.0.0.0", port = 0) ) val configLayer: ZLayer[BaseEnv, Nothing, Config] = ZLayer.succeed(config) private def request(uri: String) = effectBlocking { val conn = new URL(uri).openConnection().asInstanceOf[HttpURLConnection] conn.setConnectTimeout(500) conn.connect() val responseCode = conn.getResponseCode responseCode shouldEqual 200 } "Server" - { "listens on all interfaces when given listen=0.0.0.0" ignore { val kernel = mock[Kernel] val kernelFactory = Kernel.Factory.const(kernel) val server = new Server val serverEnv: ZLayer[BaseEnv, Throwable, server.MainEnv with MainArgs] = (configLayer andThen IdentityProvider.layer) ++ Interpreter.Factories.load ++ ZLayer.succeed(kernelFactory) ++ ZLayer.succeed(Args(watchUI = true)) ++ (configLayer ++ FileSystems.live >>> NotebookRepository.live) val run = server.server("TESTKEY").provideSomeLayer[BaseEnv](serverEnv).use { server => for { localAddress <- effectBlocking(InetAddress.getLocalHost.getCanonicalHostName) _ <- server.awaitUp port <- server.localAddress.map(_.asInstanceOf[InetSocketAddress].getPort) _ <- request(s"http://$localAddress:$port/") _ <- request(s"http://127.0.0.1:$port/") _ <- server.shutdown() } yield () } run.runIO() } } }
Example 9
Source File: DownloadableFile.scala From polynote with Apache License 2.0 | 5 votes |
package polynote.kernel.util import java.io.{File, FileInputStream, InputStream} import java.net.{HttpURLConnection, URI} import java.util.ServiceLoader import scala.collection.JavaConverters._ import cats.effect.IO import zio.{RIO, ZIO} import zio.blocking.{Blocking, effectBlocking} trait DownloadableFile { def openStream: IO[InputStream] def size: IO[Long] } trait DownloadableFileProvider { def getFile(uri: URI): Option[DownloadableFile] = provide.lift(uri) def provide: PartialFunction[URI, DownloadableFile] def protocols: Seq[String] object Supported { def unapply(arg: URI): Option[URI] = { Option(arg.getScheme).flatMap(scheme => protocols.find(_ == scheme)).map(_ => arg) } } } object DownloadableFileProvider { private lazy val unsafeLoad = ServiceLoader.load(classOf[DownloadableFileProvider]).iterator.asScala.toList def isSupported(uri: URI): RIO[Blocking, Boolean] = effectBlocking(unsafeLoad).map { providers => Option(uri.getScheme).exists(providers.flatMap(_.protocols).contains) } def getFile(uri: URI): ZIO[Blocking, Throwable, DownloadableFile] = { effectBlocking(unsafeLoad).map { providers => for { scheme <- Option(uri.getScheme) provider <- providers.find(_.protocols.contains(scheme)) file <- provider.getFile(uri) } yield file }.someOrFail(new Exception(s"Unable to find provider for uri $uri")) } } class HttpFileProvider extends DownloadableFileProvider { override def protocols: Seq[String] = Seq("http", "https") override def provide: PartialFunction[URI, DownloadableFile] = { case Supported(uri) => HTTPFile(uri) } } case class HTTPFile(uri: URI) extends DownloadableFile { override def openStream: IO[InputStream] = IO(uri.toURL.openStream()) override def size: IO[Long] = IO(uri.toURL.openConnection().asInstanceOf[HttpURLConnection]).bracket { conn => IO { conn.setRequestMethod("HEAD") conn.getContentLengthLong } } { conn => IO(conn.disconnect())} } class LocalFileProvider extends DownloadableFileProvider { override def protocols: Seq[String] = Seq("file") override def provide: PartialFunction[URI, DownloadableFile] = { case Supported(uri) => LocalFile(uri) } } case class LocalFile(uri: URI) extends DownloadableFile { lazy val file = new File(uri) override def openStream: IO[InputStream] = IO(new FileInputStream(file)) override def size: IO[Long] = IO.pure(file.length()) }
Example 10
Source File: InfinispanClient.scala From infinispan-spark with Apache License 2.0 | 5 votes |
package org.infinispan.spark.test import java.net.HttpURLConnection import java.security.SecureRandom import java.security.cert.X509Certificate import javax.net.ssl._ import sttp.client.{HttpURLConnectionBackend, basicRequest, _} case class InfinispanClient(port: Int = 11222, tls: Boolean) { val SkipSSLValidation: HttpURLConnection => Unit = { case connection: HttpsURLConnection => val sc = SSLContext.getInstance("TLS") sc.init(null, Array(new TrustAllX509TrustManager), new SecureRandom) connection.setSSLSocketFactory(sc.getSocketFactory) connection.setHostnameVerifier(new HostnameVerifier { override def verify(s: String, sslSession: SSLSession) = true }) case _ => } implicit val backend = HttpURLConnectionBackend(customizeConnection = SkipSSLValidation) val protocol = if (tls) "https" else "http" val baseURL = s"$protocol://localhost:$port/rest/v2" def shutdownServer(): Unit = { shutDownResource("server") } def shutdownCluster(): Unit = { shutDownResource("cluster") } private def shutDownResource(resource: String): Unit = { val request = basicRequest.get(uri"$baseURL/$resource?action=stop") val response = request.send() val code = response.code.code if (code < 200 || code > 204) throw new Exception(s"Failed to stop $resource, code $code") } def cacheExists(name: String): Boolean = { val request = basicRequest.get(uri"$baseURL/caches/") val response = request.send() response.body match { case Right(x) => ujson.read(x).arr.map(_.str).contains(name) case _ => false } } def isHealthy(cacheManager: String, members: Int): Boolean = { val request = basicRequest.get(uri"$baseURL/cache-managers/$cacheManager/health/") val response = request.send() response.body match { case Right(x) => val json = ujson.read(x) val clusterHealth = json("cluster_health") val health = clusterHealth("health_status") val nodes = clusterHealth("number_of_nodes") health.str == "HEALTHY" && nodes.num == members case Left(x) => throw new RuntimeException(x) } } def createCache(cacheName: String, cfg: String) = { if (!cacheExists(cacheName)) { val response = basicRequest.post(uri"$baseURL/caches/$cacheName") .contentType("application/json") .body(cfg).send() val status = response.code.code if (status < 200 || status > 204) throw new RuntimeException(s"Failed to create cache, code $status") } } class TrustAllX509TrustManager extends X509TrustManager { def getAcceptedIssuers = new Array[X509Certificate](0) def checkClientTrusted(certs: Array[X509Certificate], authType: String): Unit = {} def checkServerTrusted(certs: Array[X509Certificate], authType: String): Unit = {} } }
Example 11
Source File: SnapshotGetter.scala From c4proto with Apache License 2.0 | 5 votes |
package ee.cone.c4gate.tests import java.io.BufferedInputStream import java.net.{HttpURLConnection, URL} import java.util.Locale import ee.cone.c4actor._ import ee.cone.c4gate.HttpResponse import scala.collection.JavaConverters.{iterableAsScalaIterableConverter, mapAsScalaMapConverter} //C4STATE_TOPIC_PREFIX=ee.cone.c4actor.tests.SnapshotGetterApp sbt ~'c4actor-extra-examples/runMain ee.cone.c4actor.ServerMain' class SnapshotGetterTest(execution: Execution) extends Executable { private def withConnection[T](url: String): (HttpURLConnection => T) => T = FinallyClose[HttpURLConnection, T](_.disconnect())( new URL(url).openConnection().asInstanceOf[HttpURLConnection] ) private def setHeaders(connection: HttpURLConnection, headers: List[(String, String)]): Unit = { headers.flatMap(normalizeHeader).foreach { case (k, v) => connection.setRequestProperty(k, v) } } private def normalizeHeader[T](kv: (String, T)): List[(String, T)] = { val (k, v) = kv Option(k).map(k => (k.toLowerCase(Locale.ENGLISH), v)).toList } def get(url: String): (String, Long, Long, Map[String, List[String]]) = { val time = System.currentTimeMillis() val res: HttpResponse = withConnection(url) { conn => setHeaders(conn, Nil) FinallyClose(new BufferedInputStream(conn.getInputStream)) { is => FinallyClose(new okio.Buffer) { buffer => buffer.readFrom(is) val headers = conn.getHeaderFields.asScala.map { case (k, l) => k -> l.asScala.toList }.flatMap(normalizeHeader).toMap HttpResponse(conn.getResponseCode, headers, buffer.readByteString()) } } } val took = System.currentTimeMillis() - time (SnapshotUtilImpl.hashFromData(res.body.toByteArray), res.body.size(), took, res.headers) } def run(): Unit = { val url = "http://10.20.2.216:10580/snapshots/000000000081c0e5-92b87c05-294d-3c1d-b443-fb83bdc71d20-c-lz4" for {i <- 1 to 5} yield { println(get(url)) } execution.complete() } } class SnapshotGetterApp extends ToStartApp with VMExecutionApp with ExecutableApp with RichDataApp with EnvConfigApp { override def toStart: List[Executable] = new SnapshotGetterTest(execution) :: super.toStart def assembleProfiler: AssembleProfiler = NoAssembleProfiler }
Example 12
Source File: HTTPUtils.scala From mimir with Apache License 2.0 | 5 votes |
package mimir.util import play.api.libs.json._ object HTTPUtils { @throws(classOf[java.io.IOException]) @throws(classOf[java.net.SocketTimeoutException]) def get(url: String, connectTimeout: Int = 8000, readTimeout: Int = 8000, requestMethod: String = "GET") = { import java.net.{URL, HttpURLConnection} val connection = (new URL(url)).openConnection.asInstanceOf[HttpURLConnection] connection.setConnectTimeout(connectTimeout) connection.setReadTimeout(readTimeout) connection.setRequestMethod(requestMethod) val inputStream = connection.getInputStream val content = scala.io.Source.fromInputStream(inputStream).mkString if (inputStream != null) inputStream.close content } def getJson(url:String, path: Option[String] = None, connectTimeout: Int = 8000, readTimeout: Int = 8000, requestMethod: String = "GET"): JsValue = { path match { case None => play.api.libs.json.Json.parse(get(url, connectTimeout, readTimeout, requestMethod)) case Some(path) => JsonUtils.seekPath(play.api.libs.json.Json.parse(get(url, connectTimeout, readTimeout, requestMethod)), path) } } }
Example 13
Source File: MasterWebUISuite.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy.master.ui import java.io.DataOutputStream import java.net.{HttpURLConnection, URL} import java.nio.charset.StandardCharsets import java.util.Date import scala.collection.mutable.HashMap import org.mockito.Mockito.{mock, times, verify, when} import org.scalatest.BeforeAndAfterAll import org.apache.spark.{SecurityManager, SparkConf, SparkFunSuite} import org.apache.spark.deploy.DeployMessages.{KillDriverResponse, RequestKillDriver} import org.apache.spark.deploy.DeployTestUtils._ import org.apache.spark.deploy.master._ import org.apache.spark.rpc.{RpcEndpointRef, RpcEnv} class MasterWebUISuite extends SparkFunSuite with BeforeAndAfterAll { val conf = new SparkConf val securityMgr = new SecurityManager(conf) val rpcEnv = mock(classOf[RpcEnv]) val master = mock(classOf[Master]) val masterEndpointRef = mock(classOf[RpcEndpointRef]) when(master.securityMgr).thenReturn(securityMgr) when(master.conf).thenReturn(conf) when(master.rpcEnv).thenReturn(rpcEnv) when(master.self).thenReturn(masterEndpointRef) val masterWebUI = new MasterWebUI(master, 0) override def beforeAll() { super.beforeAll() masterWebUI.bind() } override def afterAll() { masterWebUI.stop() super.afterAll() } test("kill application") { val appDesc = createAppDesc() // use new start date so it isn't filtered by UI val activeApp = new ApplicationInfo( new Date().getTime, "app-0", appDesc, new Date(), null, Int.MaxValue) when(master.idToApp).thenReturn(HashMap[String, ApplicationInfo]((activeApp.id, activeApp))) val url = s"http://localhost:${masterWebUI.boundPort}/app/kill/" val body = convPostDataToString(Map(("id", activeApp.id), ("terminate", "true"))) val conn = sendHttpRequest(url, "POST", body) conn.getResponseCode // Verify the master was called to remove the active app verify(master, times(1)).removeApplication(activeApp, ApplicationState.KILLED) } test("kill driver") { val activeDriverId = "driver-0" val url = s"http://localhost:${masterWebUI.boundPort}/driver/kill/" val body = convPostDataToString(Map(("id", activeDriverId), ("terminate", "true"))) val conn = sendHttpRequest(url, "POST", body) conn.getResponseCode // Verify that master was asked to kill driver with the correct id verify(masterEndpointRef, times(1)).ask[KillDriverResponse](RequestKillDriver(activeDriverId)) } private def convPostDataToString(data: Map[String, String]): String = { (for ((name, value) <- data) yield s"$name=$value").mkString("&") } private def sendHttpRequest( url: String, method: String, body: String = ""): HttpURLConnection = { val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod(method) if (body.nonEmpty) { conn.setDoOutput(true) conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") conn.setRequestProperty("Content-Length", Integer.toString(body.length)) val out = new DataOutputStream(conn.getOutputStream) out.write(body.getBytes(StandardCharsets.UTF_8)) out.close() } conn } }
Example 14
import java.net.HttpURLConnection import sbt.Keys._ import sbt._ import scala.annotation.switch object Ui { lazy val uiVersion: SettingKey[String] = settingKey[String]("Ui version") lazy val uiUrl: SettingKey[String => String] = settingKey[String => String]("Construct url for ui downloading") lazy val uiCheckoutDir: SettingKey[String] = settingKey[String]("Directory for downloading ui") lazy val ui: TaskKey[File] = taskKey[File]("Download ui or return cached") lazy val settings = Seq( uiUrl := { (s: String) => s"https://github.com/Hydrospheredata/mist-ui/releases/download/v$s/mist-ui-$s.tar.gz" }, uiVersion := "2.2.1", uiCheckoutDir := "ui_local", ui := { val local = baseDirectory.value / uiCheckoutDir.value if (!local.exists()) IO.createDirectory(local) val v = uiVersion.value val target = local / s"ui-$v" if (!target.exists()) { val link = uiUrl.value(v) val targetF = local/ s"ui-$v.tar.gz" Downloader.download(link, targetF) Tar.extractTarGz(targetF, target) } target / "dist" } ) }
Example 15
Source File: MasterWebUISuite.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy.master.ui import java.io.DataOutputStream import java.net.{HttpURLConnection, URL} import java.nio.charset.StandardCharsets import java.util.Date import scala.collection.mutable.HashMap import org.mockito.Mockito.{mock, times, verify, when} import org.scalatest.BeforeAndAfterAll import org.apache.spark.{SecurityManager, SparkConf, SparkFunSuite} import org.apache.spark.deploy.DeployMessages.{KillDriverResponse, RequestKillDriver} import org.apache.spark.deploy.DeployTestUtils._ import org.apache.spark.deploy.master._ import org.apache.spark.rpc.{RpcEndpointRef, RpcEnv} class MasterWebUISuite extends SparkFunSuite with BeforeAndAfterAll { val conf = new SparkConf val securityMgr = new SecurityManager(conf) val rpcEnv = mock(classOf[RpcEnv]) val master = mock(classOf[Master]) val masterEndpointRef = mock(classOf[RpcEndpointRef]) when(master.securityMgr).thenReturn(securityMgr) when(master.conf).thenReturn(conf) when(master.rpcEnv).thenReturn(rpcEnv) when(master.self).thenReturn(masterEndpointRef) val masterWebUI = new MasterWebUI(master, 0) override def beforeAll() { super.beforeAll() masterWebUI.bind() } override def afterAll() { masterWebUI.stop() super.afterAll() } test("kill application") { val appDesc = createAppDesc() // use new start date so it isn't filtered by UI val activeApp = new ApplicationInfo( new Date().getTime, "app-0", appDesc, new Date(), null, Int.MaxValue) when(master.idToApp).thenReturn(HashMap[String, ApplicationInfo]((activeApp.id, activeApp))) val url = s"http://localhost:${masterWebUI.boundPort}/app/kill/" val body = convPostDataToString(Map(("id", activeApp.id), ("terminate", "true"))) val conn = sendHttpRequest(url, "POST", body) conn.getResponseCode // Verify the master was called to remove the active app verify(master, times(1)).removeApplication(activeApp, ApplicationState.KILLED) } test("kill driver") { val activeDriverId = "driver-0" val url = s"http://localhost:${masterWebUI.boundPort}/driver/kill/" val body = convPostDataToString(Map(("id", activeDriverId), ("terminate", "true"))) val conn = sendHttpRequest(url, "POST", body) conn.getResponseCode // Verify that master was asked to kill driver with the correct id verify(masterEndpointRef, times(1)).ask[KillDriverResponse](RequestKillDriver(activeDriverId)) } private def convPostDataToString(data: Map[String, String]): String = { (for ((name, value) <- data) yield s"$name=$value").mkString("&") } private def sendHttpRequest( url: String, method: String, body: String = ""): HttpURLConnection = { val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod(method) if (body.nonEmpty) { conn.setDoOutput(true) conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") conn.setRequestProperty("Content-Length", Integer.toString(body.length)) val out = new DataOutputStream(conn.getOutputStream) out.write(body.getBytes(StandardCharsets.UTF_8)) out.close() } conn } }
Example 16
Source File: HTTPClient.scala From hail with MIT License | 5 votes |
package is.hail.utils import java.net.URL import java.io.OutputStream import java.io.InputStream import java.net.HttpURLConnection import is.hail.utils._ import java.nio.charset.StandardCharsets import org.apache.commons.io.output.ByteArrayOutputStream object HTTPClient { def post[T]( url: String, contentLength: Int, writeBody: OutputStream => Unit, readResponse: InputStream => T = (_: InputStream) => (), chunkSize: Int = 0 ): T = { val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod("POST") if (chunkSize > 0) conn.setChunkedStreamingMode(chunkSize) conn.setDoOutput(true); conn.setRequestProperty("Content-Length", Integer.toString(contentLength)) using(conn.getOutputStream())(writeBody) assert(200 <= conn.getResponseCode() && conn.getResponseCode() < 300, s"POST ${url} ${conn.getResponseCode()} ${using(conn.getErrorStream())(fullyReadInputStreamAsString)}") val result = using(conn.getInputStream())(readResponse) conn.disconnect() result } def get[T]( url: String, readResponse: InputStream => T ): T = { val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod("GET") assert(200 <= conn.getResponseCode() && conn.getResponseCode() < 300, s"GET ${url} ${conn.getResponseCode()} ${using(conn.getErrorStream())(fullyReadInputStreamAsString)}") val result = using(conn.getInputStream())(readResponse) conn.disconnect() result } def delete( url: String, readResponse: InputStream => Unit = (_: InputStream) => () ): Unit = { val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod("DELETE") assert(200 <= conn.getResponseCode() && conn.getResponseCode() < 300, s"DELETE ${url} ${conn.getResponseCode()} ${using(conn.getErrorStream())(fullyReadInputStreamAsString)}") val result = using(conn.getInputStream())(readResponse) conn.disconnect() result } private[this] def fullyReadInputStreamAsString(is: InputStream): String = using(new ByteArrayOutputStream()) { baos => drainInputStreamToOutputStream(is, baos) new String(baos.toByteArray(), StandardCharsets.UTF_8) } }
Example 17
Source File: Build.scala From lagom with Apache License 2.0 | 5 votes |
import java.net.HttpURLConnection import java.io.BufferedReader import java.io.InputStreamReader import sbt.IO import sbt.File object DevModeBuild { def waitForReloads(file: File, count: Int): Unit = { waitFor[Int]( IO.readLines(file).count(_.nonEmpty), _ == count, actual => s"Expected $count reloads, but only got $actual" ) } def waitFor[T](check: => T, assertion: T => Boolean, error: T => String): Unit = { var checks = 0 var actual = check while (!assertion(actual) && checks < 10) { Thread.sleep(1000) actual = check checks += 1 } if (!assertion(actual)) { throw new RuntimeException(error(actual)) } } }
Example 18
Source File: Build.scala From lagom with Apache License 2.0 | 5 votes |
import java.net.HttpURLConnection import java.io.BufferedReader import java.io.InputStreamReader import sbt.IO import sbt.File object DevModeBuild { def waitForReloads(file: File, count: Int): Unit = { waitFor[Int]( IO.readLines(file).count(_.nonEmpty), _ == count, actual => s"Expected $count reloads, but only got $actual" ) } def waitFor[T](check: => T, assertion: T => Boolean, error: T => String): Unit = { var checks = 0 var actual = check while (!assertion(actual) && checks < 10) { Thread.sleep(1000) actual = check checks += 1 } if (!assertion(actual)) { throw new RuntimeException(error(actual)) } } }
Example 19
Source File: AppMasterResolver.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.experiments.yarn.client import java.io.IOException import java.net.{HttpURLConnection, URL} import java.nio.charset.StandardCharsets import akka.actor.{ActorRef, ActorSystem} import org.apache.commons.io.IOUtils import org.apache.gearpump.experiments.yarn.glue.Records.{ApplicationId, ApplicationReport} import org.apache.gearpump.experiments.yarn.glue.YarnClient import org.apache.gearpump.util.{AkkaHelper, LogUtil} import org.apache.hadoop.hdfs.web.URLConnectionFactory import org.apache.hadoop.yarn.conf.YarnConfiguration import scala.util.Try class AppMasterResolver(yarnClient: YarnClient, system: ActorSystem) { val LOG = LogUtil.getLogger(getClass) val RETRY_INTERVAL_MS = 3000 // ms def resolve(appId: ApplicationId, timeoutSeconds: Int = 30): ActorRef = { val appMaster = retry(connect(appId), 1 + timeoutSeconds * 1000 / RETRY_INTERVAL_MS) appMaster } private def connect(appId: ApplicationId): ActorRef = { val report = yarnClient.getApplicationReport(appId) AppMasterResolver.resolveAppMasterAddress(report, system) } private def retry(fun: => ActorRef, times: Int): ActorRef = { var index = 0 var result: ActorRef = null while (index < times && result == null) { Thread.sleep(RETRY_INTERVAL_MS) index += 1 val tryConnect = Try(fun) if (tryConnect.isFailure) { LOG.error(s"Failed to connect YarnAppMaster(tried $index)... " + tryConnect.failed.get.getMessage) } else { result = tryConnect.get } } result } } object AppMasterResolver { val LOG = LogUtil.getLogger(getClass) def resolveAppMasterAddress(report: ApplicationReport, system: ActorSystem): ActorRef = { val appMasterPath = s"${report.getTrackingURL}/supervisor-actor-path" LOG.info(s"appMasterPath=$appMasterPath") val connectionFactory: URLConnectionFactory = URLConnectionFactory .newDefaultURLConnectionFactory(new YarnConfiguration()) val url: URL = new URL(appMasterPath) val connection: HttpURLConnection = connectionFactory.openConnection(url) .asInstanceOf[HttpURLConnection] connection.setInstanceFollowRedirects(true) try { connection.connect() } catch { case e: IOException => LOG.error(s"Failed to connect to AppMaster" + e.getMessage) } val status = connection.getResponseCode if (status == 200) { val stream: java.io.InputStream = connection.getInputStream val response = IOUtils.toString(stream, StandardCharsets.UTF_8) LOG.info("Successfully resolved AppMaster address: " + response) connection.disconnect() AkkaHelper.actorFor(system, response) } else { connection.disconnect() throw new IOException("Fail to resolve AppMaster address, please make sure " + s"${report.getTrackingURL} is accessible...") } } }
Example 20
Source File: SendSlackMessage.scala From hyperion with Apache License 2.0 | 5 votes |
package com.krux.hyperion.contrib.activity.notification import java.net.{ HttpURLConnection, URL } import org.json4s.JsonAST.{ JString, JObject } import org.json4s.jackson.JsonMethods._ import scopt.OptionParser object SendSlackMessage { case class Options( failOnError: Boolean = false, webhookUrl: String = "", user: Option[String] = None, message: Seq[String] = Seq.empty, iconEmoji: Option[String] = None, channel: Option[String] = None ) def apply(options: Options): Boolean = try { // Setup the connection val connection = new URL(options.webhookUrl).openConnection().asInstanceOf[HttpURLConnection] connection.setDoOutput(true) connection.setRequestProperty("Content-Type", "application/json") connection.setRequestProperty("Accept", "application/json") // Write the message val output = connection.getOutputStream try { val message = Seq( "icon_emoji" -> options.iconEmoji, "channel" -> options.channel, "username" -> options.user, "text" -> Option(options.message.mkString("\n")) ).flatMap { case (k, None) => None case (k, Some(v)) => Option(k -> JString(v)) } output.write(compact(render(JObject(message: _*))).getBytes) } finally { output.close() } // Check the response code connection.getResponseCode == 200 || !options.failOnError } catch { case e: Throwable => System.err.println(e.toString) !options.failOnError } def main(args: Array[String]): Unit = { val parser = new OptionParser[Options](s"hyperion-notification-slack-activity") { override def showUsageOnError = true note("Sends a notification message to a Slack incoming webhook.") help("help").text("prints this usage text") opt[Unit]("fail-on-error").optional().action((_, c) => c.copy(failOnError = true)) .text("Causes the activity to fail if any error received from the webhook") opt[String]("webhook-url").valueName("WEBHOOK").required().action((x, c) => c.copy(webhookUrl = x)) .text("Sends the message to the given WEBHOOK url") opt[String]("user").valueName("NAME").optional().action((x, c) => c.copy(user = Option(x))) .text("Sends the message as the user with NAME") opt[String]("emoji").valueName("EMOJI").optional().action((x, c) => c.copy(iconEmoji = Option(x))) .text("Use EMOJI for the icon") opt[String]("to").valueName("CHANNEL or USERNAME").optional().action((x, c) => c.copy(channel = Option(x))) .text("Sends the message to #CHANNEL or @USERNAME") arg[String]("MESSAGE").required().unbounded().action((x, c) => c.copy(message = c.message :+ x)) .text("Sends the given MESSAGE") } if (!parser.parse(args, Options()).exists(apply)) { System.exit(3) } } }
Example 21
Source File: SendFlowdockMessage.scala From hyperion with Apache License 2.0 | 5 votes |
package com.krux.hyperion.contrib.activity.notification import java.net.{ HttpURLConnection, URL } import org.json4s.JsonDSL._ import org.json4s.jackson.JsonMethods._ import scopt.OptionParser object SendFlowdockMessage { case class Options( failOnError: Boolean = false, apiKey: String = "", message: String = "", user: String = "hyperion", tags: Seq[String] = Seq.empty ) def apply(options: Options): Boolean = try { // Setup the connection val connection = new URL(s"https://api.flowdock.com/v1/messages/chat/${options.apiKey}") .openConnection().asInstanceOf[HttpURLConnection] connection.setDoOutput(true) connection.setRequestProperty("Content-Type", "application/json") connection.setRequestProperty("Accept", "application/json") // Write the message val output = connection.getOutputStream try { output.write(compact(render(("event" -> "message") ~ ("external_user_name" -> options.user) ~ ("content" -> options.message) ~ ("tags" -> options.tags))).getBytes) } finally { output.close() } // Check the response code connection.getResponseCode == 200 || !options.failOnError } catch { case e: Throwable => System.err.println(e.toString) !options.failOnError } def main(args: Array[String]): Unit = { val parser = new OptionParser[Options](s"hyperion-notification-flowdock-activity") { override def showUsageOnError = true note("Sends a notification message to a Flowdock flow.") help("help").text("prints this usage text") opt[Unit]("fail-on-error").optional().action((_, c) => c.copy(failOnError = true)) opt[String]("api-key").valueName("KEY").required().action((x, c) => c.copy(apiKey = x)) .text("Sends the given TEXT as the subject") opt[String]("user").valueName("NAME").optional().action((x, c) => c.copy(user = x)) .text("Sends the message as the user with NAME") opt[Seq[String]]("tags").valueName("TAG1,TAG2").optional().action((x, c) => c.copy(tags = x)) .text("Adds the tags to the message") arg[String]("MESSAGE").required().unbounded().action((x, c) => c.copy(message = s"${c.message} $x")) .text("Sends the given MESSAGE") } if (!parser.parse(args, Options()).exists(apply)) { System.exit(3) } } }
Example 22
Source File: HttpClientTestSupport.scala From wix-http-testkit with MIT License | 5 votes |
package com.wix.e2e.http.drivers import java.io.DataOutputStream import java.net.{HttpURLConnection, URL} import akka.http.scaladsl.model.HttpMethods.GET import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers.`Transfer-Encoding` import akka.stream.scaladsl.Source import com.wix.e2e.http.client.extractors._ import com.wix.e2e.http.info.HttpTestkitVersion import com.wix.e2e.http.matchers.{RequestMatcher, ResponseMatcher} import com.wix.e2e.http.{BaseUri, HttpRequest, RequestHandler} import com.wix.test.random._ import scala.collection.immutable import scala.collection.mutable.ListBuffer trait HttpClientTestSupport { val parameter = randomStrPair val header = randomStrPair val formData = randomStrPair val userAgent = randomStr val cookie = randomStrPair val path = s"$randomStr/$randomStr" val anotherPath = s"$randomStr/$randomStr" val someObject = SomeCaseClass(randomStr, randomInt) val somePort = randomPort val content = randomStr val anotherContent = randomStr val requestData = ListBuffer.empty[String] val bigResponse = 1024 * 1024 def issueChunkedPostRequestWith(content: String, toPath: String)(implicit baseUri: BaseUri) = { val serverUrl = new URL(s"http://localhost:${baseUri.port}/$toPath") val conn = serverUrl.openConnection.asInstanceOf[HttpURLConnection] conn.setRequestMethod("POST") conn.setRequestProperty("Content-Type", "text/plain") conn.setChunkedStreamingMode(0) conn.setDoOutput(true) conn.setDoInput(true) conn.setUseCaches(false) conn.connect() val out = new DataOutputStream(conn.getOutputStream) out.writeBytes(content) out.flush() out.close() conn.disconnect() } } object HttpClientTestResponseHandlers { def handlerFor(path: String, returnsBody: String): RequestHandler = { case r: HttpRequest if r.uri.path.toString.endsWith(path) => HttpResponse(entity = returnsBody) } def unmarshallingAndStoringHandlerFor(path: String, storeTo: ListBuffer[String]): RequestHandler = { case r: HttpRequest if r.uri.path.toString.endsWith(path) => storeTo.append( r.extractAsString ) HttpResponse() } def bigResponseWith(size: Int): RequestHandler = { case HttpRequest(GET, uri, _, _, _) if uri.path.toString().contains("big-response") => HttpResponse(entity = HttpEntity(randomStrWith(size))) } def chunkedResponseFor(path: String): RequestHandler = { case r: HttpRequest if r.uri.path.toString.endsWith(path) => HttpResponse(entity = HttpEntity.Chunked(ContentTypes.`text/plain(UTF-8)`, Source.single(randomStr))) } def alwaysRespondWith(transferEncoding: TransferEncoding, toPath: String): RequestHandler = { case r: HttpRequest if r.uri.path.toString.endsWith(toPath) => HttpResponse().withHeaders(immutable.Seq(`Transfer-Encoding`(transferEncoding))) } val slowRespondingServer: RequestHandler = { case _ => Thread.sleep(500); HttpResponse() } } case class SomeCaseClass(s: String, i: Int) object HttpClientMatchers { import com.wix.e2e.http.matchers.RequestMatchers._ def haveClientHttpTestkitUserAgentWithLibraryVersion: RequestMatcher = haveAnyHeadersOf("User-Agent" -> s"client-http-testkit/$HttpTestkitVersion") } object HttpServerMatchers { import com.wix.e2e.http.matchers.ResponseMatchers._ def haveServerHttpTestkitHeaderWithLibraryVersion: ResponseMatcher = haveAnyHeadersOf("Server" -> s"server-http-testkit/$HttpTestkitVersion") }
Example 23
package flaky import java.io._ import java.net.{HttpURLConnection, URL} import java.util.Scanner import sbt.{File, Logger} import scala.language.postfixOps import scala.util.{Failure, Success, Try} object Io { def writeToFile(file: File, content: String): Unit = { new PrintWriter(file) { write(content) close() } } def writeToFile(file: File, content: Array[Byte]): Unit = { new FileOutputStream(file) { write(content) close() } } def writeToFile(file: File, is: InputStream): Unit = { val array: Array[Byte] = Stream.continually(is.read).takeWhile(_ != -1).map(_.toByte).toArray writeToFile(file, array) } def sendToSlack(webHook: String, jsonMsg: String, log: Logger, backupFile: File): Unit = { log.info("Sending report to slack") log.debug("Dumping slack msg to file") new PrintWriter(backupFile) { write(jsonMsg) close() } val send: Try[Unit] = Try { val url = new URL(webHook) val urlConnection = url.openConnection().asInstanceOf[HttpURLConnection] // Indicate that we want to write to the HTTP request body urlConnection.setDoOutput(true) urlConnection.setRequestMethod("POST") // Writing the post data to the HTTP request body log.debug(jsonMsg) val httpRequestBodyWriter = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream)) httpRequestBodyWriter.write(jsonMsg) httpRequestBodyWriter.close() val scanner = new Scanner(urlConnection.getInputStream) log.debug("Response from SLACK:") while (scanner.hasNextLine) { log.debug(s"Response from SLACK: ${scanner.nextLine()}") } scanner.close() } send match { case Success(_) => log.info("Notification successfully send to Slack") case Failure(e) => log.error(s"Can't send message to slack: ${e.getMessage}") } } }
Example 24
Source File: ServerStartupCheck.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.standalone import java.net.{HttpURLConnection, URL} import akka.http.scaladsl.model.Uri import com.google.common.base.Stopwatch import org.apache.openwhisk.utils.retry import scala.concurrent.duration._ class ServerStartupCheck(uri: Uri, serverName: String) { def waitForServerToStart(): Unit = { val w = Stopwatch.createStarted() retry({ println(s"Waiting for $serverName server at $uri to start since $w") require(getResponseCode() == 200) }, 30, Some(1.second)) } private def getResponseCode(): Int = { val u = new URL(uri.toString()) val hc = u.openConnection().asInstanceOf[HttpURLConnection] hc.setRequestMethod("GET") hc.connect() hc.getResponseCode } }
Example 25
Source File: Fabric8ClientTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.containerpool.kubernetes.test import java.net.HttpURLConnection import common.{StreamLogging, WskActorSystem} import io.fabric8.kubernetes.api.model.{EventBuilder, PodBuilder} import io.fabric8.kubernetes.client.utils.HttpClientUtils.createHttpClientForMockServer import io.fabric8.kubernetes.client.{ConfigBuilder, DefaultKubernetesClient} import okhttp3.TlsVersion.TLS_1_0 import org.apache.openwhisk.common.{ConfigMapValue, TransactionId} import org.apache.openwhisk.core.containerpool.kubernetes._ import org.apache.openwhisk.core.entity.size._ import org.junit.runner.RunWith import org.scalatest.concurrent.ScalaFutures import org.scalatest.junit.JUnitRunner import org.scalatest.{FlatSpec, Matchers} import scala.concurrent.duration._ @RunWith(classOf[JUnitRunner]) class Fabric8ClientTests extends FlatSpec with Matchers with WskActorSystem with ScalaFutures with KubeClientSupport with StreamLogging { implicit val tid: TransactionId = TransactionId.testing behavior of "Fabric8Client" val runTimeout = 2.seconds def config(configMap: Option[ConfigMapValue] = None, affinity: Option[KubernetesInvokerNodeAffinity] = None) = KubernetesClientConfig( KubernetesClientTimeoutConfig(runTimeout, 2.seconds), affinity.getOrElse(KubernetesInvokerNodeAffinity(false, "k", "v")), false, None, configMap, Some(KubernetesCpuScalingConfig(300, 3.MB, 1000)), false, Some(Map("POD_UID" -> "metadata.uid")), None) it should "fail activation on cold start when apiserver fails" in { //use an invalid client to simulate broken api server def defaultClient = { val config = new ConfigBuilder() .withMasterUrl("http://localhost:11111") //test assumes that port 11111 will fail in some way .withTrustCerts(true) .withTlsVersions(TLS_1_0) .withNamespace("test") .build new DefaultKubernetesClient(createHttpClientForMockServer(config), config) } val restClient = new KubernetesClient(config(), testClient = Some(defaultClient))(executionContext) restClient.run("fail", "fail", 256.MB).failed.futureValue shouldBe a[KubernetesPodApiException] } it should "fail activation on cold start when pod ready times out" in { val podName = "failWait" server .expect() .post() .withPath("/api/v1/namespaces/test/pods") .andReturn( HttpURLConnection.HTTP_CREATED, new PodBuilder().withNewMetadata().withName(podName).endMetadata().build()) .once(); server .expect() .get() .withPath("/api/v1/namespaces/test/pods/failWait") .andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withName(podName).endMetadata().build()) .times(3) server .expect() .get() .withPath("/api/v1/namespaces/test/events?fieldSelector=involvedObject.name%3DfailWait") .andReturn( HttpURLConnection.HTTP_OK, new EventBuilder().withNewMetadata().withName(podName).endMetadata().build()) .once implicit val patienceConfig = PatienceConfig(timeout = runTimeout + 1.seconds, interval = 0.5.seconds) val restClient = new KubernetesClient(config(), testClient = Some(kubeClient))(executionContext) restClient.run(podName, "anyimage", 256.MB).failed.futureValue shouldBe a[KubernetesPodReadyTimeoutException] } }
Example 26
Source File: InternalHttpSpec.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.http import java.net.{HttpURLConnection, URL} import java.util.concurrent.TimeUnit import akka.actor.{Props, ActorSystem} import akka.testkit.TestKit import akka.util.Timeout import com.webtrends.harness.TestKitSpecificationWithJUnit import com.webtrends.harness.service.messages.CheckHealth import scala.concurrent.Await import akka.pattern.ask import scala.concurrent.duration.FiniteDuration class InternalHttpSpec extends TestKitSpecificationWithJUnit(ActorSystem("test")) with InternalHttpClient { val port = 8123 val path = "http://127.0.0.1:" + port + "/" val httpActor = system.actorOf(Props(classOf[SimpleHttpServer], port)) // We need to make sure the httpActor has started up before trying to connect. implicit val timeout = Timeout(FiniteDuration(5, TimeUnit.SECONDS)) Await.result(httpActor ? CheckHealth, timeout.duration) "Test handlers" should { "handle the get path /ping" in { val url = new URL(path + "ping") val conn = url.openConnection().asInstanceOf[HttpURLConnection] val resp = getResponseContent(conn) resp.status mustEqual "200" resp.content.length must be > 0 resp.content.substring(0, 5) mustEqual "pong:" } } step { TestKit.shutdownActorSystem(system) } }
Example 27
Source File: InternalHttpClient.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.http import java.io.{BufferedReader, InputStreamReader} import java.net.{HttpURLConnection, URLConnection} import java.util.zip.{GZIPInputStream, InflaterInputStream} case class HttpResponseData( statusLine: String, content: String, headers: collection.mutable.Map[String, String]) { private val startIndex = statusLine.indexOf(" ") + 1 val status = statusLine.substring(startIndex, startIndex + 3) override def toString: String = { val sb = new StringBuilder sb.append(statusLine + "\n") headers.foreach(m => sb.append(m._1 + "=" + m._2 + "\n")) sb.append("\n" + content + "\n") sb.toString() } } }
Example 28
Source File: GetUrlTest.scala From piflow with BSD 2-Clause "Simplified" License | 5 votes |
package cn.piflow.bundle.http import java.io.{BufferedReader, InputStreamReader, PrintWriter} import java.net.{HttpURLConnection, InetAddress, URL, URLConnection} import cn.piflow.Runner import cn.piflow.conf.bean.FlowBean import cn.piflow.conf.util.{FileUtil, OptionUtil} import cn.piflow.util.{PropertyUtil, ServerIpUtil} import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet} import org.apache.http.impl.client.HttpClients import org.apache.http.util.EntityUtils import org.apache.spark.sql.SparkSession import org.h2.tools.Server import org.junit.Test import scala.util.parsing.json.JSON class GetUrlTest { @Test def testFlow(): Unit ={ //parse flow json val file = "src/main/resources/flow/http/getUrl.json" val flowJsonStr = FileUtil.fileReader(file) val map = OptionUtil.getAny(JSON.parseFull(flowJsonStr)).asInstanceOf[Map[String, Any]] println(map) //create flow val flowBean = FlowBean(map) val flow = flowBean.constructFlow() val ip = InetAddress.getLocalHost.getHostAddress cn.piflow.util.FileUtil.writeFile("server.ip=" + ip, ServerIpUtil.getServerIpFile()) val h2Server = Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort","50001").start() //execute flow val spark = SparkSession.builder() .master("local[12]") .appName("hive") .config("spark.driver.memory", "4g") .config("spark.executor.memory", "8g") .config("spark.cores.max", "8") .config("hive.metastore.uris",PropertyUtil.getPropertyValue("hive.metastore.uris")) .enableHiveSupport() .getOrCreate() val process = Runner.create() .bind(classOf[SparkSession].getName, spark) .bind("checkpoint.path", "") .bind("debug.path","") .start(flow); process.awaitTermination(); val pid = process.pid(); println(pid + "!!!!!!!!!!!!!!!!!!!!!") spark.close(); } }
Example 29
Source File: HttpClient.scala From ArchiveSpark with MIT License | 5 votes |
package org.archive.archivespark.sparkling.http import java.io.{BufferedInputStream, InputStream} import java.net.{HttpURLConnection, URL, URLConnection} import org.archive.archivespark.sparkling.logging.LogContext import org.archive.archivespark.sparkling.util.Common import scala.collection.JavaConverters._ import scala.util.Try object HttpClient { val DefaultRetries: Int = 30 val DefaultSleepMillis: Int = 1000 val DefaultTimeoutMillis: Int = -1 implicit val logContext: LogContext = LogContext(this) def request[R](url: String, headers: Map[String, String] = Map.empty, retries: Int = DefaultRetries, sleepMillis: Int = DefaultSleepMillis, timeoutMillis: Int = DefaultTimeoutMillis)(action: InputStream => R): R = rangeRequest(url, headers, retries = retries, sleepMillis = sleepMillis, timeoutMillis = timeoutMillis)(action) def rangeRequest[R](url: String, headers: Map[String, String] = Map.empty, offset: Long = 0, length: Long = -1, retries: Int = DefaultRetries, sleepMillis: Int = DefaultSleepMillis, timeoutMillis: Int = DefaultTimeoutMillis)(action: InputStream => R): R = { rangeRequestConnection(url, headers, offset, length, retries, sleepMillis, timeoutMillis) { case connection: HttpURLConnection => val in = new BufferedInputStream(connection.getInputStream) val r = action(in) Try(in.close()) r } } def requestMessage[R](url: String, headers: Map[String, String] = Map.empty, retries: Int = DefaultRetries, sleepMillis: Int = DefaultSleepMillis, timeoutMillis: Int = DefaultTimeoutMillis)(action: HttpMessage => R): R = rangeRequestMessage(url, headers, retries = retries, sleepMillis = sleepMillis, timeoutMillis = timeoutMillis)(action) def rangeRequestMessage[R](url: String, headers: Map[String, String] = Map.empty, offset: Long = 0, length: Long = -1, retries: Int = DefaultRetries, sleepMillis: Int = DefaultSleepMillis, timeoutMillis: Int = DefaultTimeoutMillis)(action: HttpMessage => R): R = { rangeRequestConnection(url, headers, offset, length, retries, sleepMillis, timeoutMillis) { case connection: HttpURLConnection => val in = new BufferedInputStream(connection.getInputStream) val responseHeaders = connection.getHeaderFields.asScala.toMap.flatMap{case (k, v) => v.asScala.headOption.map((if (k == null) "" else k) -> _)} val message = new HttpMessage(connection.getResponseMessage, responseHeaders, in) val r = action(message) Try(in.close()) r } } def requestConnection[R](url: String, headers: Map[String, String] = Map.empty, retries: Int = DefaultRetries, sleepMillis: Int = DefaultSleepMillis, timeoutMillis: Int = DefaultTimeoutMillis)(action: URLConnection => R): R = rangeRequestConnection(url, headers, retries = retries, sleepMillis = sleepMillis, timeoutMillis = timeoutMillis)(action) def rangeRequestConnection[R](url: String, headers: Map[String, String] = Map.empty, offset: Long = 0, length: Long = -1, retries: Int = DefaultRetries, sleepMillis: Int = DefaultSleepMillis, timeoutMillis: Int = DefaultTimeoutMillis)(action: URLConnection => R): R = { Common.timeoutWithReporter(timeoutMillis) { reporter => val connection = Common.retry(retries, sleepMillis, (retry, e) => { "Request failed (" + retry + "/" + retries + "): " + url + " (" + offset + "-" + (if (length >= 0) length else "") + ") - " + e.getMessage }) { _ => reporter.alive() val connection = new URL(url).openConnection() for ((key, value) <- headers) connection.addRequestProperty(key, value) if (offset > 0 || length >= 0) connection.addRequestProperty("Range", "bytes=" + offset + "-" + (if (length >= 0) offset + length - 1 else "")) connection.asInstanceOf[HttpURLConnection] } val r = action(connection) Try(connection.disconnect()) r } } }
Example 30
Source File: MasterWebUISuite.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy.master.ui import java.io.DataOutputStream import java.net.{HttpURLConnection, URL} import java.nio.charset.StandardCharsets import java.util.Date import scala.collection.mutable.HashMap import org.mockito.Mockito.{mock, times, verify, when} import org.scalatest.BeforeAndAfterAll import org.apache.spark.{SecurityManager, SparkConf, SparkFunSuite} import org.apache.spark.deploy.DeployMessages.{KillDriverResponse, RequestKillDriver} import org.apache.spark.deploy.DeployTestUtils._ import org.apache.spark.deploy.master._ import org.apache.spark.rpc.{RpcEndpointRef, RpcEnv} class MasterWebUISuite extends SparkFunSuite with BeforeAndAfterAll { val conf = new SparkConf val securityMgr = new SecurityManager(conf) val rpcEnv = mock(classOf[RpcEnv]) val master = mock(classOf[Master]) val masterEndpointRef = mock(classOf[RpcEndpointRef]) when(master.securityMgr).thenReturn(securityMgr) when(master.conf).thenReturn(conf) when(master.rpcEnv).thenReturn(rpcEnv) when(master.self).thenReturn(masterEndpointRef) val masterWebUI = new MasterWebUI(master, 0) override def beforeAll() { super.beforeAll() masterWebUI.bind() } override def afterAll() { masterWebUI.stop() super.afterAll() } test("kill application") { val appDesc = createAppDesc() // use new start date so it isn't filtered by UI val activeApp = new ApplicationInfo( new Date().getTime, "app-0", appDesc, new Date(), null, Int.MaxValue) when(master.idToApp).thenReturn(HashMap[String, ApplicationInfo]((activeApp.id, activeApp))) val url = s"http://localhost:${masterWebUI.boundPort}/app/kill/" val body = convPostDataToString(Map(("id", activeApp.id), ("terminate", "true"))) val conn = sendHttpRequest(url, "POST", body) conn.getResponseCode // Verify the master was called to remove the active app verify(master, times(1)).removeApplication(activeApp, ApplicationState.KILLED) } test("kill driver") { val activeDriverId = "driver-0" val url = s"http://localhost:${masterWebUI.boundPort}/driver/kill/" val body = convPostDataToString(Map(("id", activeDriverId), ("terminate", "true"))) val conn = sendHttpRequest(url, "POST", body) conn.getResponseCode // Verify that master was asked to kill driver with the correct id verify(masterEndpointRef, times(1)).ask[KillDriverResponse](RequestKillDriver(activeDriverId)) } private def convPostDataToString(data: Map[String, String]): String = { (for ((name, value) <- data) yield s"$name=$value").mkString("&") } private def sendHttpRequest( url: String, method: String, body: String = ""): HttpURLConnection = { val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection] conn.setRequestMethod(method) if (body.nonEmpty) { conn.setDoOutput(true) conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") conn.setRequestProperty("Content-Length", Integer.toString(body.length)) val out = new DataOutputStream(conn.getOutputStream) out.write(body.getBytes(StandardCharsets.UTF_8)) out.close() } conn } }