本文整理汇总了Scala中akka.http.scaladsl.model.Uri类的典型用法代码示例。如果您正苦于以下问题:Scala Uri类的具体用法?Scala Uri怎么用?Scala Uri使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Uri类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: disableUi
//设置package包名称以及导入依赖的类
package net.ruippeixotog.scalafbp.http
import java.net.URLDecoder
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model.Uri
import akka.http.scaladsl.model.Uri.Query
import akka.http.scaladsl.server.Directives._
import spray.json.DefaultJsonProtocol._
import spray.json._
trait UiHttpService {
def disableUi: Boolean
private[this] val dummyUser = "user"
private[this] val dummyUserName = "ScalaFBP Development Environment"
private[this] val dummyToken = "oauthToken"
// format: OFF
lazy val uiRoutes =
if(disableUi) reject
else {
pathPrefix("oauth") {
path("login" / "oauth" / "authorize") {
parameter("redirect_uri") { encodedUri =>
val uri = Uri(URLDecoder.decode(encodedUri, "UTF-8"))
redirect(uri.withQuery(Query("code" -> dummyUser)), Found)
}
} ~
path("authenticate" / dummyUser) {
complete(Map("token" -> dummyToken))
} ~
path("user") {
complete(JsObject(
"name" -> JsString(dummyUserName),
"github" -> JsObject.empty))
}
} ~
pathEndOrSingleSlash { getFromResource("ui/index.html") } ~
getFromResourceDirectory("ui")
}
// format: ON
}
示例2: MonitoringServer
//设置package包名称以及导入依赖的类
package com.scalaio.http.monitoring
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.ContentTypes._
import akka.http.scaladsl.model.{HttpEntity, HttpResponse, StatusCodes, Uri}
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._
import akka.stream.Materializer
import com.typesafe.config.Config
import com.yammer.metrics.HealthChecks
import com.yammer.metrics.core.HealthCheckRegistry
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsArray, Json}
import scala.util.{Failure, Success}
import scala.collection.convert.wrapAsScala._
object MonitoringServer {
lazy val logger = LoggerFactory.getLogger(getClass)
def handleHealthchecks(registry: HealthCheckRegistry): Route = {
path("health") {
get {
complete {
val checks = registry.runHealthChecks
val payload = JsArray(checks.map {
case (name, result) =>
Json.obj(
"name" -> name,
"healthy" -> result.isHealthy,
"message" -> result.getMessage
)
}.toSeq)
val status = if (checks.values().forall(_.isHealthy)) OK else InternalServerError
HttpResponse(entity = HttpEntity(`application/json`, Json.stringify(payload)), status = status)
}
}
}
}
def start(serverConfig: Config, registry: HealthCheckRegistry = HealthChecks.defaultRegistry())
(implicit system: ActorSystem, materializer: Materializer): Unit = {
val host = serverConfig.getString("host")
val port = serverConfig.getInt("port")
logger.info(s"Starting monitoring server at: $host:$port")
val routes = handleHealthchecks(registry) ~ redirect(Uri("/health"), StatusCodes.SeeOther)
import system.dispatcher
Http()
.bindAndHandle(routes, host, port).onComplete {
case Success(Http.ServerBinding(address)) =>
logger.info(s"Monitoring server started at :$address")
case Failure(t) =>
logger.error("Error while trying to start monitoring server", t)
}
}
}
示例3: create
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.ironmq
import akka.actor.ActorSystem
import akka.http.scaladsl.model.Uri
import akka.stream.alpakka.ironmq.ConfigIronMqSettings.ConfigConsumerSettings
import akka.stream.alpakka.ironmq.IronMqSettings.ConsumerSettings
import com.typesafe.config.Config
import scala.concurrent.duration.FiniteDuration
def create(as: ActorSystem): IronMqSettings = apply()(as)
}
object ConfigIronMqSettings {
class ConfigConsumerSettings(config: Config) extends ConsumerSettings {
override val bufferMinSize: Int = config.getInt("buffer-min-size")
override val bufferMaxSize: Int = config.getInt("buffer-max-size")
override val fetchInterval: FiniteDuration = config.getDuration("fetch-interval").asScala
override val pollTimeout: FiniteDuration = config.getDuration("poll-timeout").asScala
override val reservationTimeout: FiniteDuration = config.getDuration("reservation-timeout").asScala
}
}
class ConfigIronMqSettings(config: Config) extends IronMqSettings {
override val endpoint: Uri = config.getString("endpoint")
override val projectId: String = config.getString("credentials.project-id")
override val token: String = config.getString("credentials.token")
override def consumerSettings: ConsumerSettings = new ConfigConsumerSettings(config.getConfig("consumer"))
}
示例4: ScrapingKitReactor
//设置package包名称以及导入依赖的类
package ru.fediq.scrapingkit
import akka.actor.{ActorSystem, Props}
import akka.http.scaladsl.model.Uri
import akka.routing.RoundRobinPool
import com.typesafe.scalalogging.StrictLogging
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
import ru.fediq.scrapingkit.backend.{FeedExporter, LinksHistory, LinksQueue, PageCache}
import ru.fediq.scrapingkit.model.PageRef
import ru.fediq.scrapingkit.platform._
import ru.fediq.scrapingkit.scraper.Scraper
import scala.util.Try
class ScrapingKitReactor(
linksQueue: LinksQueue,
linksHistory: LinksHistory,
pageCache: PageCache,
exporter: FeedExporter,
scrapers: Map[String, Scraper],
redirectFilter: Option[(PageRef, Uri) => Boolean] = None
)(implicit val system: ActorSystem)
extends AnyRef with AutoCloseable with StrictLogging {
val config = system.settings.config.as[ScrapingKitConfig]("scrapingkit")
val queueingActor = system
.actorOf(
Props(new QueueingActor(linksQueue, linksHistory, config))
.withDispatcher("pinnedDispatcher"),
"queueing"
)
val downloadingActor = system
.actorOf(
Props(new DownloadingActor(pageCache, config, redirectFilter)),
"downloading"
)
val scrapingActor = system
.actorOf(
RoundRobinPool(config.scrapingThreads, routerDispatcher = "pinnedDispatcher")
.props(Props(new ScrapingActor(scrapers, exporter, config))),
"scraping"
)
system.registerOnTermination(close())
override def close() = {
logger.info("Stopping ScarpingKit Reactor")
Try(linksQueue.close())
Try(linksHistory.close())
Try(pageCache.close())
Try(exporter.close())
logger.info("Stopped")
}
}
示例5: LinksHistoryFiller
//设置package包名称以及导入依赖的类
package ru.fediq.scrapingkit.tools
import akka.http.scaladsl.model.Uri
import ru.fediq.scrapingkit.backend.LinksHistory
import scala.concurrent.{ExecutionContext, Future}
class LinksHistoryFiller(
linksHistory: LinksHistory
) {
def add(uri: Uri)(implicit ec: ExecutionContext): Future[Any] = {
linksHistory.isKnown(uri).flatMap {
case true => Future.successful()
case false => linksHistory.addKnown(uri)
}
}
def fill(uris: Traversable[Uri])(implicit ec: ExecutionContext): Future[Any] = {
val futures = uris.map(add)
Future.sequence(futures)
}
}
示例6: ScrapingKitReactorTest
//设置package包名称以及导入依赖的类
package ru.fediq.scrapingkit
import java.util.concurrent.TimeUnit
import akka.actor.ActorSystem
import akka.http.scaladsl.model.{HttpMethods, Uri}
import com.codahale.metrics.Slf4jReporter
import com.codahale.metrics.Slf4jReporter.LoggingLevel
import com.typesafe.config.ConfigFactory
import org.scalatest.FlatSpec
import org.slf4j.LoggerFactory
import ru.fediq.scrapingkit.backend.{InMemoryFifoLinksQueue, InMemoryLinksHistory, NoOpFeedExporter, NoOpPageCache}
import ru.fediq.scrapingkit.model.PageRef
import ru.fediq.scrapingkit.scraper.HtmlCrawlingScraper
import ru.fediq.scrapingkit.util.Metrics
class ScrapingKitReactorTest extends FlatSpec {
"Reactor" should "crawl something" in {
val scraperName = "crawl"
val scrapers = Map(scraperName -> new HtmlCrawlingScraper(scraperName))
val config = ConfigFactory.load()
implicit val system = ActorSystem("reactor-test", config)
val linksQueue = new InMemoryFifoLinksQueue()
val linksHistory = new InMemoryLinksHistory()
val pageCache = new NoOpPageCache()
val exporter = new NoOpFeedExporter()
val reactor = new ScrapingKitReactor(linksQueue, linksHistory, pageCache, exporter, scrapers)
linksQueue.enqueue(PageRef(Uri("http://quotes.toscrape.com/"), HttpMethods.GET, scraperName))
Slf4jReporter
.forRegistry(Metrics.metricRegistry)
.withLoggingLevel(LoggingLevel.INFO)
.outputTo(LoggerFactory.getLogger("METRICS"))
.convertDurationsTo(TimeUnit.MILLISECONDS)
.convertRatesTo(TimeUnit.SECONDS)
.build()
.start(10, TimeUnit.SECONDS)
Thread.sleep(10000)
reactor.close()
}
}
示例7: PageRef
//设置package包名称以及导入依赖的类
package ru.fediq.scrapingkit.model
import akka.http.scaladsl.model.{HttpMethod, HttpMethods, Uri}
case class PageRef(
uri: Uri,
method: HttpMethod,
scraperName: String,
depth: Double = 1.0,
context: Map[String, String] = Map.empty,
redirectsChain: List[Uri] = Nil
) {
def lastUri = redirectsChain.headOption.getOrElse(uri)
def redirectSteps = if (redirectsChain.isEmpty) Nil else uri :: redirectsChain.tail
def chain(nextLocation: Uri) = copy(
method = HttpMethods.GET,
redirectsChain = nextLocation :: redirectsChain
)
}
示例8: DownloadRequest
//设置package包名称以及导入依赖的类
package ru.fediq.scrapingkit.scraper
import akka.http.scaladsl.model.{HttpMethod, HttpMethods, Uri}
import ru.fediq.scrapingkit.util.Implicits._
import spray.json._
sealed trait Scraped
case class DownloadRequest(
uri: Uri,
scraperName: String,
context: Map[String, String] = Map.empty,
method: HttpMethod = HttpMethods.GET,
depthInc: Double = 1.0
) extends Scraped
trait ScrapedEntity extends Scraped {
def dump: String
}
case class MapScrapedEntity(map: Map[String, Any]) extends ScrapedEntity with DefaultJsonProtocol {
override def dump = map.mapToJson.compactPrint
}
abstract class JsonScrapedEntity[T](format: JsonFormat[T]) extends ScrapedEntity {
self: T =>
override def dump = {
self.asInstanceOf[T].toJson(format).compactPrint
}
}
trait PrintableScrapedEntity extends ScrapedEntity {
override def dump = toString
}
示例9: RmqConstants
//设置package包名称以及导入依赖的类
package ru.fediq.scrapingkit.backend
import akka.http.scaladsl.model.{HttpMethods, Uri}
import ru.fediq.scrapingkit.model.PageRef
object RmqConstants {
val connectorActorName = "links-queue"
val channelActorName = "rmqchannel"
val channelActorPath = "/user/links-queue/rmqchannel"
case class SerializedPageRef(
uri: String,
method: String,
scraper: String,
depth: Double,
context: Map[String, String]
) {
def deserialize(): PageRef = PageRef(
Uri(uri),
HttpMethods.getForKey(method).getOrElse(HttpMethods.GET),
scraper,
depth,
context
)
}
}
示例10: AkkaBoom
//设置package包名称以及导入依赖的类
package com.github.basking2.sdsai.akka
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, Uri}
import akka.stream.{ActorMaterializer, OverflowStrategy}
import akka.stream.scaladsl.Source
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.util.{Failure, Success}
object AkkaBoom {
def main(args: Array[String]):Unit = {
implicit val actorSystem = ActorSystem("akkaboom")
implicit val materializer = ActorMaterializer()
implicit val dispatcher = actorSystem.dispatcher
val uri = Uri("http://www.nhptv.org/wild/images/moosenpsJacobWFrank.jpg")
val src = Source(0 to 100)
val httpFlow = Http().cachedHostConnectionPool[Int](uri.authority.host.toString(), uri.authority.port)
val f = src.map { i =>
val req = HttpRequest(HttpMethods.GET, uri.path.toString())
(req, i)
}
.via(httpFlow)
.runForeach {
case (Success(resp), i) =>
Await.ready(resp.entity.discardBytes().future(), 10 seconds)
case (Failure(t), i) =>
t.printStackTrace()
}
Await.ready(f, 1 minute)
Await.ready(actorSystem.terminate(), 1 minute)
println("OK")
}
}
示例11: WolframServiceImpl
//设置package包名称以及导入依赖的类
package me.alexray.wolfram.impl
import java.net.URLEncoder
import akka.NotUsed
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpRequest, Uri}
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.Materializer
import akka.util.ByteString
import com.lightbend.lagom.scaladsl.api.ServiceCall
import me.alexray.wolfram.api.WolframService
import play.api.Configuration
import scala.concurrent.{ExecutionContext, Future}
class WolframServiceImpl(config: Configuration)
(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext)
extends WolframService
{
val appID = config.underlying.getString("wolfram.appid")
val apiUrl = s"http://api.wolframalpha.com/v2/"
override def query(q: String): ServiceCall[NotUsed, String] = ServiceCall { _ =>
val url = apiUrl + s"query?appid=$appID&input=" + URLEncoder.encode(q, "UTF-8")
for {
response <- Http().singleRequest(HttpRequest(uri = Uri(url)))
if response.status.isSuccess()
data <- Unmarshal(response).to[String]
} yield data
}
override def simple(q: String): ServiceCall[NotUsed, Array[Byte]] = ServiceCall { _ =>
println(s"quetions = '$q'")
val url = apiUrl + s"simple?appid=$appID&input=" + URLEncoder.encode(q, "UTF-8").replace("+", "%20")
println(s"url = '$url'")
for {
response <- Http().singleRequest(HttpRequest(uri = Uri(url)))
if response.status.isSuccess()
bytes <- Unmarshal(response).to[ByteString]
} yield {
println(s"received image ${bytes.size} bytes long")
bytes.toArray
}
}
}
示例12: Cert
//设置package包名称以及导入依赖的类
package run.cosy.crypto
import java.math.BigInteger
import java.security.KeyFactory
import java.security.interfaces.RSAPublicKey
import java.security.spec.RSAPublicKeySpec
import akka.http.scaladsl.model.Uri
import org.w3.banana.{PointedGraph, RDF, RDFOps, binder}
import org.w3.banana.binder.{PGBinder, RecordBinder, ToPG}
object Cert {
def binderWithName[Rdf<:RDF](u: Uri)(implicit ops: RDFOps[Rdf]): PGBinder[Rdf, RSAPublicKey] =
new Cert[Rdf].binderRootName(u.toString())
def binder[Rdf<:RDF](implicit ops: RDFOps[Rdf]): PGBinder[Rdf, RSAPublicKey] = new Cert[Rdf].binder
}
class Cert[Rdf<:RDF](implicit ops: RDFOps[Rdf]) {
import org.w3.banana.{CertPrefix, RDF, RDFOps, binder}
implicit val recordBinder = org.w3.banana.binder.RecordBinder[Rdf](ops)
val cert = CertPrefix[Rdf]
import org.w3.banana.binder._
import recordBinder._
implicit val rsaClassUri = recordBinder.classUrisFor[RSAPublicKey](cert.RSAPublicKey)
val factory = KeyFactory.getInstance("RSA")
val exponent = property[BigInteger](cert.exponent)
val modulus = property[Array[Byte]](cert.modulus)
val binder: PGBinder[Rdf, RSAPublicKey] =
pgb[RSAPublicKey](modulus, exponent)(
(m: Array[Byte], e: BigInteger) => factory.generatePublic(new RSAPublicKeySpec(new BigInteger(m), e)).asInstanceOf[RSAPublicKey],
(key: RSAPublicKey) => Some((key.getModulus.toByteArray, key.getPublicExponent))
).withClasses(rsaClassUri)
def binderRootName(uri: String) =
pgbWithConstId[RSAPublicKey](uri)(modulus, exponent)(
(m: Array[Byte], e: BigInteger) => factory.generatePublic(new RSAPublicKeySpec(new BigInteger(m), e)).asInstanceOf[RSAPublicKey],
(key: RSAPublicKey) => Some((key.getModulus.toByteArray, key.getPublicExponent))
).withClasses(rsaClassUri)
}
示例13: signRequest
//设置package包名称以及导入依赖的类
package im.actor.server.file.local
import java.net.URLEncoder
import akka.actor.ActorSystem
import akka.http.scaladsl.model.{ HttpMethod, Uri }
import im.actor.acl.ACLBase
import org.apache.commons.codec.digest.DigestUtils.sha256Hex
import org.apache.commons.codec.digest.HmacUtils
trait RequestSigning extends ACLBase {
private val urlEncode: String ? String = URLEncoder.encode(_, "UTF-8")
def signRequest(httpVerb: HttpMethod, uri: Uri, secret: String): Uri =
uri.withQuery(("signature" ? calculateSignature(httpVerb, uri, secret)) +: uri.query())
def calculateSignature(httpVerb: HttpMethod, uri: Uri)(implicit system: ActorSystem): String =
calculateSignature(httpVerb, uri, secretKey())
def calculateSignature(httpVerb: HttpMethod, uri: Uri, secret: String): String = {
val resourcePath = uri.path
val canonicalQueryString = uri.query() sortBy (_._1) map {
case (name, value) ?
s"${urlEncode(name)}=${urlEncode(value)}"
} mkString "&"
val canonicalRequest =
s"""$httpVerb
|$resourcePath
|$canonicalQueryString""".stripMargin
HmacUtils.hmacSha256Hex(secret, sha256Hex(canonicalRequest))
}
}
示例14: ConnectionActor
//设置package包名称以及导入依赖的类
package com.github.pvoznenko.newPackage
import akka.actor.{Actor, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods.GET
import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri}
import scala.concurrent.Future
import scala.io.StdIn
class ConnectionActor extends Actor {
case object StartConnection
case class ConnectWebsite(s: String)
override def receive = {
case StartConnection => {
val requestHandler: HttpRequest => Future[HttpResponse] = {
case HttpRequest(GET, Uri.Path("/pintpin"), _, _, _) => {
val c = context.system.actorOf(Props[RequestHandler], "child1")
val a = c ? OneSlash
a map { p =>
p
}
}
case HttpRequest(GET, Uri.Path("/ping"), _, _, _) =>
Future {
HttpResponse(entity = "PONG!")
}
case HttpRequest(GET, Uri.Path("/crash"), _, _, _) =>
sys.error("BOOM!")
}
val bindingFuture = Http().bindAndHandleAsync(requestHandler, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.shutdown()) // and shutdown when done
}
}
}
示例15: QueueRouter
//设置package包名称以及导入依赖的类
package reactive.queue.router
import akka.actor.{Actor, Props}
import akka.http.scaladsl.model.Uri
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Sink, Source}
import io.scalac.amqp.Connection
object QueueRouter {
def props(queueConnection: Connection, queueSubscription: QueueSubscription, queuePrefetchCount: Int): Props = {
Props(classOf[QueueRouter], queueConnection, queueSubscription, queuePrefetchCount)
}
}
class QueueRouter(queueConnection: Connection, queueSubscription: QueueSubscription, queuePrefetchCount: Int) extends Actor {
implicit val materlizer = ActorMaterializer()
val queuePublisher = QueuePublisher(queueConnection, queueSubscription.queueName, queuePrefetchCount).publisher
val queueSubscriber = QueueSubscriber.props(queueConnection, queueSubscription.queueName, Uri(queueSubscription.queueSubscriberUrl))
Source.fromPublisher(queuePublisher).map(_.message).runWith(Sink.actorSubscriber(queueSubscriber))
def receive = Actor.emptyBehavior
}