Created
November 26, 2012 20:05
-
-
Save darkfrog26/4150293 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.outr.webcommunicator.netty.handler | |
import com.outr.webcommunicator.netty.NettyWebapp | |
import org.jboss.netty.channel.{MessageEvent, ChannelHandlerContext} | |
import org.jboss.netty.handler.codec.http.{HttpChunk, HttpRequest} | |
import org.jboss.netty.handler.codec.http.multipart.{FileUpload, Attribute, DefaultHttpDataFactory, HttpPostRequestDecoder} | |
import scala.collection.JavaConversions._ | |
/** | |
* @author Matt Hicks <matt@outr.com> | |
*/ | |
trait UploadHandler extends RequestHandler { | |
private val factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE) | |
private var decoder: HttpPostRequestDecoder = _ | |
def apply(webapp: NettyWebapp, context: ChannelHandlerContext, event: MessageEvent) = event.getMessage match { | |
case request: HttpRequest => { | |
decoder = new HttpPostRequestDecoder(factory, request) | |
if (!request.isChunked) { | |
uploadFinished(context) | |
} | |
} | |
case chunk: HttpChunk => { | |
decoder.offer(chunk) | |
if (chunk.isLast) { | |
uploadFinished(context) | |
} | |
} | |
} | |
private def uploadFinished(context: ChannelHandlerContext) = { | |
val datas = decoder.getBodyHttpDatas.toList | |
datas.foreach { | |
case attribute: Attribute => receivedAttribute(attribute) | |
case upload: FileUpload => receivedFileUpload(upload) | |
} | |
sendResponse(context) | |
} | |
def receivedAttribute(attribute: Attribute): Unit | |
def receivedFileUpload(upload: FileUpload): Unit | |
def sendResponse(context: ChannelHandlerContext): Unit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment