Commit a72df66f authored by Mathieu's avatar Mathieu
Browse files

Not orking /* request with listFiles

parent e957e667
......@@ -49,7 +49,9 @@ lazy val server = project.in(file("server")) settings (defaultSettings) settings
"org.json4s" %% "json4s-jackson" % json4sVersion,
"com.pauldijou" %% "jwt-core" % scalaJWTVersion,
"fr.hmil" %% "roshttp" % rosHttpVersion,
"io.skuber" %% "skuber" % skuberVersion
"org.scalaj" %% "scalaj-http" % "2.4.2",
"io.skuber" %% "skuber" % skuberVersion,
"io.suzaku" %% "boopickle" % "1.2.6"
)
) dependsOn (shared) enablePlugins (ScalatraPlugin)
......
......@@ -15,12 +15,14 @@ import scalatags.Text.{all => tags}
import scala.concurrent.duration._
import shared.Data._
import boopickle.Default._
import fr.hmil.roshttp.HttpRequest
import monix.execution.Scheduler.Implicits.global
import fr.hmil.roshttp.response.SimpleHttpResponse
import org.openmoleconnect.server.DB._
class ConnectServlet(arguments: ConnectServer.ServletArguments) extends ScalatraServlet {
......@@ -32,16 +34,22 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With")
)
def waitForGet(httpRequest: HttpRequest): SimpleHttpResponse = {
Await.result(
httpRequest.get()
, 1 minute)
def waitForGet(httpRequest: HttpRequest) = {
Await.result(
httpRequest.get()
, 1 minute)
}
def waitForPost(httpRequest: HttpRequest) = {
Await.result(
httpRequest.withMethod(Method.POST).send(),
1 minute)
}
def waitForPost(httpRequest: HttpRequest): SimpleHttpResponse = {
def waitForPost2(httpRequest: HttpRequest) = {
Await.result(
httpRequest.withMethod(Method.POST).send()
, 1 minute)
httpRequest.withMethod(Method.POST).send(),
1 minute)
}
......@@ -52,6 +60,7 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
def proxyRequest(hostIP: Option[String]) = {
hostIP.map { hip =>
println("HIP " + hip)
withForwardRequest(hip) { forwardRequest =>
val req = forwardRequest.withHeaders((headers(request)): _*).withHeaders(allowHeaders: _*)
val fR = waitForGet(req)
......@@ -60,8 +69,9 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
}
}
//def withForwardRequest(hostIP: String)(action: HttpRequest => ActionResult): ActionResult = {
def withForwardRequest(hostIP: String)(action: HttpRequest => ActionResult): ActionResult = {
action(baseForwardRequest.withHost(hostIP).withPort(80).withPath(""))
action(baseForwardRequest.withHost(hostIP).withPort(80).withPath(""))
}
def withAccesToken(action: TokenData => ActionResult): Serializable = {
......@@ -102,8 +112,17 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
NotFound()
}
var incr = 0
def toUnsignedByte(bytes:Array[Byte]):Short = {
val aByte:Int = 0xff & bytes(0).asInstanceOf[Int]
aByte.asInstanceOf[Short]
}
// OM instance requests
post("/*") {
println("POST /*")
withAccesToken { tokenData =>
tokenData.host.hostIP.map { hip =>
withForwardRequest(hip) { forwardRequest =>
......@@ -113,20 +132,98 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
val bytes: Array[Byte] = Iterator.continually(is.read()).takeWhile(_ != -1).map(_.asInstanceOf[Byte]).toArray[Byte]
val bb = ByteBuffer.wrap(bytes)
println("----------- REQUEST " + path)
for {
b <- bytes
} yield {
println("B " + b)
}
// val req = waitForPost(
// forwardRequest.withPath(s"/$path").withHeader("Content-Type", "application/octet-stream") //withBody(ByteBufferBody(bb))
// )
//
//
// val reqByte = req.body.asInstanceOf[ByteBuffer]
//
//
// println("Body " + req)
// val data = Array.ofDim[Byte](reqByte.remaining())
// reqByte.get(data)
// // Ok(data)
//
// println("Data " + data)
// if (req.statusCode < 400) Ok(data)
// else NotFound()
val req = waitForPost(
forwardRequest.withPath(s"/$path").withHeader("Content-Type", "application/octet-stream").withBody(ByteBufferBody(bb))
)
if (req.statusCode < 400) Ok(req.body)
response addHeader("Content-Type", "application/octet-stream")
println("----------- RESPONSE " + path)
val is2 = req.body.getBytes//.map{_ & 0xff }
for {
b <- is2
} yield {
println("B " + b)
}
println("----------------")
val bbuffer = ByteBuffer.wrap(is2)
val data = Array.ofDim[Byte](bbuffer.remaining)
val getdata = bbuffer.get(data)
// val is2 = req.body.map{TypedArrayBuffer.wrap}
//val is2 = req.body
// println("GET DATA " + getdata)
//
// println("DATA " + data)
// for {
// b <- data
// } yield {
// println("B " + b)
// }
//
// if (incr == 2) {
// println("INCR " + incr)
// println("unpickle: " + Unpickle[ListFilesData].tryFromBytes(data.asInstanceOf[ByteBuffer]))
//
// // val bytes2: Array[Byte] = Iterator.continually(is.read()).takeWhile(_ != -1).map(_.asInstanceOf[Byte]).toArray[Byte]
// // val bb2 = ByteBuffer.wrap(is2)
// } else {
// incr = incr + 1
// }
if (req.statusCode < 400) Ok(data) //Ok(is2)
else NotFound()
case None => NotFound()
}
}
}
}.getOrElse(NotFound())
}
}
case class ListFilesData(list: Seq[TreeNodeData], nbFilesOnServer: Int)
case class DirData(isEmpty: Boolean)
case class TreeNodeData(
name: String,
dirData: Option[DirData],
size: Long,
time: Long
)
post(connectionRoute) {
Authentication.isValid(request, TokenType.accessToken) match {
case false =>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment