Permalink
Browse files

Migration of Content into content package

Added Content.asString support and better platform separation
  • Loading branch information...
darkfrog26 committed Dec 28, 2018
1 parent 7e4df4f commit e41aa980f88b0223ea4cb0c11578da2f8709d9c9
Showing with 287 additions and 176 deletions.
  1. +3 −2 app/jvm/src/main/scala/io/youi/app/MatcherPage.scala
  2. +1 −0 app/jvm/src/main/scala/io/youi/app/MultiPage.scala
  3. +2 −1 app/jvm/src/main/scala/io/youi/app/Page.scala
  4. +1 −0 app/jvm/src/main/scala/io/youi/app/ServerApplication.scala
  5. +3 −2 app/jvm/src/main/scala/io/youi/app/TemplatePage.scala
  6. +3 −2 client/js/src/main/scala/io/youi/client/JSHttpClient.scala
  7. +1 −0 client/jvm/src/main/scala/io/youi/client/JVMHttpClient.scala
  8. +3 −2 client/jvm/src/test/scala/spec/HttpClientSpec.scala
  9. +2 −1 client/shared/src/main/scala/io/youi/client/HttpClient.scala
  10. +20 −0 core/js/src/main/scala/io/youi/http/content/ContentHelpers.scala
  11. +2 −1 core/jvm/src/main/scala/io/youi/http/StreamContent.scala
  12. +3 −0 core/jvm/src/main/scala/io/youi/http/StreamZipContent.scala
  13. +24 −0 core/jvm/src/main/scala/io/youi/http/content/ContentHelpers.scala
  14. +21 −0 core/jvm/src/main/scala/io/youi/http/content/FileContent.scala
  15. +41 −0 core/jvm/src/main/scala/io/youi/http/content/URLContent.scala
  16. +0 −143 core/shared/src/main/scala/io/youi/http/Content.scala
  17. +1 −0 core/shared/src/main/scala/io/youi/http/HttpRequest.scala
  18. +1 −0 core/shared/src/main/scala/io/youi/http/HttpResponse.scala
  19. +14 −0 core/shared/src/main/scala/io/youi/http/content/BytesContent.scala
  20. +41 −0 core/shared/src/main/scala/io/youi/http/content/Content.scala
  21. +3 −0 core/shared/src/main/scala/io/youi/http/content/FormData.scala
  22. +39 −0 core/shared/src/main/scala/io/youi/http/content/FormDataContent.scala
  23. +13 −0 core/shared/src/main/scala/io/youi/http/content/FormDataEntry.scala
  24. +14 −0 core/shared/src/main/scala/io/youi/http/content/StringContent.scala
  25. +0 −5 core/shared/src/main/scala/io/youi/http/package.scala
  26. +3 −2 example/jvm/src/main/scala/io/youi/example/CookiesExample.scala
  27. +1 −0 example/jvm/src/main/scala/io/youi/example/GeneralPages.scala
  28. +2 −1 example/jvm/src/main/scala/io/youi/example/SessionExample.scala
  29. +3 −2 server/src/main/scala/io/youi/server/DefaultErrorHandler.scala
  30. +2 −1 server/src/main/scala/io/youi/server/dsl/package.scala
  31. +2 −1 server/src/main/scala/io/youi/server/handler/ContentHandler.scala
  32. +2 −1 server/src/main/scala/io/youi/server/handler/HttpHandler.scala
  33. +1 −0 server/src/main/scala/io/youi/server/handler/HttpHandlerBuilder.scala
  34. +3 −2 server/src/main/scala/io/youi/server/handler/ProxyCache.scala
  35. +2 −1 server/src/main/scala/io/youi/server/handler/SenderHandler.scala
  36. +2 −1 server/src/main/scala/io/youi/server/handler/ValidatorHttpHandler.scala
  37. +2 −1 server/src/main/scala/io/youi/server/rest/RestfulHandler.scala
  38. +1 −0 server/src/test/scala/spec/ServerSpec.scala
  39. +1 −1 serverUndertow/src/main/scala/io/youi/server/ResourceServer.scala
  40. +1 −0 serverUndertow/src/main/scala/io/youi/server/UndertowServerImplementation.scala
  41. +3 −3 stream/src/main/scala/io/youi/stream/HTMLParser.scala
@@ -1,6 +1,7 @@
package io.youi.app

import io.youi.http.{Content, HttpConnection}
import io.youi.http.HttpConnection
import io.youi.http.content.Content
import io.youi.net.URLMatcher

trait MatcherPage extends Page {
@@ -13,4 +14,4 @@ trait MatcherPage extends Page {
} else {
None
}
}
}
@@ -3,6 +3,7 @@ import java.io.File

import io.youi.http
import io.youi.http._
import io.youi.http.content.Content
import io.youi.net.URLMatcher

trait MultiPage extends MatcherPage {
@@ -1,6 +1,7 @@
package io.youi.app

import io.youi.http.{Content, HttpConnection}
import io.youi.http.HttpConnection
import io.youi.http.content.Content
import io.youi.server.handler.{CachingManager, HttpProcessor}
import io.youi.server.validation.Validator
import io.youi.stream._
@@ -4,6 +4,7 @@ import java.io.File

import akka.actor.{ActorSystem, Cancellable}
import io.youi.http._
import io.youi.http.content.{Content, FileContent, FormDataContent, StringContent, URLContent}
import io.youi.net.{ContentType, URL}
import io.youi.server.Server
import io.youi.server.handler.{CachingManager, HttpHandler, HttpHandlerBuilder, SenderHandler}
@@ -1,6 +1,7 @@
package io.youi.app

import io.youi.http.{Content, HttpConnection}
import io.youi.http.HttpConnection
import io.youi.http.content.Content

trait TemplatePage extends MatcherPage {
def templateFor(path: String): Option[Content]
@@ -15,4 +16,4 @@ trait TemplatePage extends MatcherPage {
templateFor(path)
}
}
}
}
@@ -1,7 +1,8 @@
package io.youi.client

import io.youi.ajax.{AjaxAction, AjaxRequest}
import io.youi.http.{Content, Headers, HttpRequest, HttpResponse, HttpStatus, StringContent}
import io.youi.http.content._
import io.youi.http.{Headers, HttpRequest, HttpResponse, HttpStatus}
import io.youi.net.ContentType

import scala.concurrent.Future
@@ -46,4 +47,4 @@ case class JSHttpClient(config: HttpClientConfig = HttpClient.config) extends Ht
case c: StringContent => c.value
case _ => throw new RuntimeException(s"$content not supported")
}
}
}
@@ -7,6 +7,7 @@ import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicLong

import io.youi.http._
import io.youi.http.content._
import io.youi.net.ContentType
import okhttp3.Dns
import org.powerscala.io._
@@ -2,7 +2,8 @@ package spec

import io.youi.client.HttpClient
import io.youi.client.intercept.Interceptor
import io.youi.http.{HttpRequest, HttpStatus, StringContent}
import io.youi.http.content.StringContent
import io.youi.http.{HttpRequest, HttpStatus}
import io.youi.net._
import org.scalatest.{Assertion, AsyncWordSpec, Matchers}

@@ -48,4 +49,4 @@ class HttpClientSpec extends AsyncWordSpec with Matchers {
}
}
}
}
}
@@ -4,8 +4,9 @@ import io.circe.{Decoder, Encoder, Json, Printer}
import io.circe.parser.decode
import io.circe.syntax._
import io.youi.client.intercept.RateLimiter
import io.youi.http.content.{Content, StringContent}
import io.youi.http.cookie.RequestCookie
import io.youi.http.{Content, Headers, HttpRequest, HttpResponse, Method, StringContent}
import io.youi.http.{Headers, HttpRequest, HttpResponse, Method}
import io.youi.net.{ContentType, URL}
import reactify.Var

@@ -0,0 +1,20 @@
package io.youi.http.content

import java.io.File
import java.net.URL

import io.youi.net.ContentType

import scala.xml.Elem

trait ContentHelpers extends SharedContentHelpers {
private def unsupported: Content = throw new UnsupportedOperationException("Not supported in Scala.js")

override def xml(value: Elem, contentType: ContentType): Content = unsupported
override def file(file: File): Content = unsupported
override def file(file: File, contentType: ContentType): Content = unsupported
override def url(url: URL): Content = unsupported
override def url(url: URL, contentType: ContentType): Content = unsupported
override def classPath(url: URL): Content = unsupported
override def classPath(path: String, contentType: ContentType): Content = unsupported
}
@@ -2,10 +2,11 @@ package io.youi.http

import java.io.OutputStream

import io.youi.http.content.Content
import io.youi.net.ContentType

abstract class StreamContent(val contentType: ContentType,
val lastModified: Long = System.currentTimeMillis(),
val length: Long = -1L) extends Content {
def stream(out: OutputStream): Unit
}
}
@@ -3,6 +3,7 @@ package io.youi.http
import java.io.OutputStream
import java.util.zip.{ZipEntry, ZipOutputStream}

import io.youi.http.content.Content
import io.youi.net.ContentType
import org.powerscala.io._

@@ -29,4 +30,6 @@ class StreamZipContent(entries: List[ZipFileEntry],
zos.flush()
zos.close()
}

override def asString: String = entries.mkString(", ")
}
@@ -0,0 +1,24 @@
package io.youi.http.content

import java.io.File
import java.net.URL

import io.youi.net.ContentType

import scala.language.implicitConversions
import scala.xml.{Elem, PrettyPrinter}

trait ContentHelpers extends SharedContentHelpers {
private lazy val xmlPrinter = new PrettyPrinter(80, 4)

implicit def file2Content(file: File): Content = this.file(file)
implicit def url2Content(url: java.net.URL): Content = this.url(url)

override def xml(value: Elem, contentType: ContentType): Content = StringContent(xmlPrinter.format(value), contentType)
override def file(file: File): Content = FileContent(file, ContentType.byFileName(file.getName))
override def file(file: File, contentType: ContentType): Content = FileContent(file, contentType)
override def url(url: URL): Content = URLContent(url, ContentType.byFileName(url.toString))
override def url(url: URL, contentType: ContentType): Content = URLContent(url, contentType)
override def classPath(url: URL): Content = URLContent(url, ContentType.byFileName(url.toString))
override def classPath(path: String, contentType: ContentType): Content = URLContent(Thread.currentThread().getContextClassLoader.getResource(path), contentType)
}
@@ -0,0 +1,21 @@
package io.youi.http.content

import java.io.File

import io.youi.net.ContentType
import org.powerscala.io.IO

case class FileContent(file: File, contentType: ContentType, lastModifiedOverride: Option[Long] = None) extends Content {
assert(file.isFile, s"Cannot send back ${file.getAbsolutePath} as it is a directory or does not exist!")

override def length: Long = file.length()

override def withContentType(contentType: ContentType): Content = copy(contentType = contentType)
override def withLastModified(lastModified: Long): Content = copy(lastModifiedOverride = Some(lastModified))

override def lastModified: Long = lastModifiedOverride.getOrElse(file.lastModified())

override def toString: String = s"FileContent(file: ${file.getAbsolutePath}, contentType: $contentType)"

override def asString: String = IO.stream(file, new StringBuilder).toString
}
@@ -0,0 +1,41 @@
package io.youi.http.content

import java.net.{HttpURLConnection, JarURLConnection, URL}

import io.youi.net.ContentType
import org.powerscala.io.IO
import sun.net.www.protocol.file.FileURLConnection

case class URLContent(url: URL, contentType: ContentType, lastModifiedOverride: Option[Long] = None) extends Content {
assert(url != null, "URL must not be null.")

override def withContentType(contentType: ContentType): Content = copy(contentType = contentType)
override def withLastModified(lastModified: Long): Content = copy(lastModifiedOverride = Some(lastModified))

private lazy val (contentLength, contentModified) = {
val connection = url.openConnection()
connection match {
case c: HttpURLConnection => try {
c.setRequestMethod("HEAD")
c.getInputStream
c.getContentLengthLong -> c.getLastModified
} finally {
c.disconnect()
}
case c: FileURLConnection => {
c.getContentLengthLong -> c.getLastModified
}
case c: JarURLConnection => {
c.getContentLengthLong -> c.getLastModified
}
}
}

override def length: Long = contentLength

override def lastModified: Long = lastModifiedOverride.getOrElse(contentModified)

override def toString: String = s"URLContent(url: $url, contentType: $contentType)"

override def asString: String = IO.stream(url, new StringBuilder).toString
}

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit e41aa98

Please sign in to comment.