javax.servlet.http.HttpServlet Scala Examples

The following examples show how to use javax.servlet.http.HttpServlet. 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: FileDownloadServlet.scala    From udash-core   with Apache License 2.0 5 votes vote down vote up
package io.udash.rpc.utils

import java.io.File
import java.nio.file.Files
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}


  protected def resolveFileMimeType(file: File): String =
    Option(getServletContext.getMimeType(file.getAbsolutePath)).getOrElse("application/octet-stream")

  override def doGet(request: HttpServletRequest, response: HttpServletResponse): Unit = {
    val file = resolveFile(request)

    if (!file.exists()) response.sendError(404, "File not found!")
    else {
      // MIME type
      response.setContentType(resolveFileMimeType(file))
      // content length
      response.setContentLengthLong(file.length)
      // file name
      response.setHeader("Content-Disposition", s"""attachment; filename="${presentedFileName(file.getName)}"""")

      val outStream = response.getOutputStream
      Files.copy(file.toPath, outStream)
      outStream.close()
    }
  }
} 
Example 2
Source File: FileUploadServlet.scala    From udash-core   with Apache License 2.0 5 votes vote down vote up
package io.udash.rpc.utils

import java.io.InputStream
import java.nio.file.Paths
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
import com.avsystem.commons._


  protected def handleFile(name: String, content: InputStream): Unit

  override protected def doPost(request: HttpServletRequest, response: HttpServletResponse): Unit = {
    request.getParts.asScala
      .filter(part => fileFields.contains(part.getName))
      .foreach(filePart => {
        val fileName = Paths.get(filePart.getSubmittedFileName).getFileName.toString
        val fileContent = filePart.getInputStream
        handleFile(fileName, fileContent)
        fileContent.close()
      })
  }
} 
Example 3
Source File: OpenApiServlet.scala    From udash-core   with Apache License 2.0 5 votes vote down vote up
package io.udash
package rest.openapi

import com.avsystem.commons.OptArg
import com.avsystem.commons.annotation.explicitGenerics
import com.avsystem.commons.serialization.json.JsonStringOutput
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}

object OpenApiServlet {
  @explicitGenerics def apply[RestApi: OpenApiMetadata](
    info: Info,
    components: Components = Components(),
    servers: List[Server] = Nil,
    security: List[SecurityRequirement] = Nil,
    tags: List[Tag] = Nil,
    externalDocs: OptArg[ExternalDocumentation] = OptArg.Empty
  ): OpenApiServlet = new OpenApiServlet {
    protected def render(request: HttpServletRequest): OpenApi =
      implicitly[OpenApiMetadata[RestApi]].openapi(info, components, servers, security, tags, externalDocs)
  }
}

abstract class OpenApiServlet extends HttpServlet {
  protected def render(request: HttpServletRequest): OpenApi

  override def doGet(req: HttpServletRequest, resp: HttpServletResponse): Unit = {
    resp.setContentType("application/json;charset=utf-8")
    resp.getWriter.write(JsonStringOutput.writePretty(render(req)))
  }
} 
Example 4
Source File: HttpStreamServer.scala    From spark-http-stream   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package org.apache.spark.sql.execution.streaming.http

import org.apache.spark.internal.Logging
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler

import javax.servlet.ServletConfig
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.eclipse.jetty.servlet.ServletHolder
import scala.collection.JavaConversions

class MissingRequiredRequestParameterException(paramName: String)
		extends RuntimeException(s"missing required request parameter: $paramName") {
}

class UnsupportedActionException(action: String)
		extends RuntimeException(s"unsupported action in HTTP request header: $action") {
}

object HttpStreamServer {
	def start(httpServletPath: String, httpPort: Int) = {
		val server = new HttpStreamServer(httpServletPath, httpPort);
		server.start;
		server;
	}
}


	def withActionsHandler[T <: ActionsHandler](actionsHandler: T): T = {
		this.actionsHandler = actionsHandler;
		actionsHandler;
	}

	def withBuffer(): MemoryBufferAsReceiver = {
		withActionsHandler(new MemoryBufferAsReceiver());
	}

	def withKafka(bootstrapServers: String): KafkaAsReceiver = {
		withActionsHandler(new KafkaAsReceiver(bootstrapServers));
	}

	def stop() = {
		httpStreamServlet.destroy();
		if (server != null)
			server.stop();
	}
} 
Example 5
Source File: FregeServlet.scala    From sbt-frege   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package fregeweb

import scala.language.implicitConversions

import javax.servlet.http.HttpServlet
import javax.servlet.http.{ HttpServletRequest => HSReq }
import javax.servlet.http.{ HttpServletResponse => HSRes }

import FregeWeb.TRequest
import FregeWeb.TResponse

import frege.run8.Box

class FregeServlet extends HttpServlet {

  override def service(hsReq: HSReq, hsRes: HSRes): Unit =
    hsRes service hsReq

}

object `package` {

  implicit class HSResService(val hsRes: HSRes) extends AnyVal {

    def service(hsReq: HSReq): Unit = {
      val tReq: TRequest = TRequest.mk( new Box(hsReq.method)
                                      , new Box(hsReq.uri)
                                      )
      val tRes: TResponse = FregeWeb.service(tReq).asInstanceOf[TResponse]
      write(tRes)
    }

    private def write(tRes: TResponse): Unit = {
      val status: Int = TResponse.status(tRes).asInstanceOf[Int]
      val body: String = TResponse.body(tRes).asInstanceOf[String]
      hsRes.setStatus(status)
      hsRes.getWriter().write(body)
    }

  }

  implicit class RichHSReq(val hsReq: HSReq) extends AnyVal {
    def method: String = hsReq.getMethod()
    def uri: String =
      if (hsReq.getRequestURI().startsWith(hsReq.getServletPath()))
        hsReq.getRequestURI().substring(hsReq.getServletPath().length())
      else
        hsReq.getRequestURI()
  }

}