<commit_before>package butter4s.servlet.rest

import butter4s.servlet._
import butter4s.reflect._
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import java.io.PrintWriter
import butter4s.bind.json.JSONBind

/**
 * @author <NAME> <<EMAIL>>
 */
class RestServlet extends Servlet {
	override def post( request: Request, response: Response ) = get( request, response )

	override def get( request: Request, response: Response ) = {
		val action = request.getRequestURI.substring( request.getServletPath.length + 1 )
		val methodName = if ( action.contains( "/" ) ) action.substring( 0, action.indexOf( "/" ) ) else action
		getClass.declaredMethod( methodName ) match {
			case Some( method ) => method.annotation[RestAction] match {
				case Some( a ) => try {
					method.invoke( this, Array[AnyRef](
						new RestRequest( request, if ( a.path != RestConstants.DEFAULT ) a.path else method.name ),
						new RestResponse( response, a.produces + "; charset=" + a.charset )
						): _* )
				} catch {
					case e: RestError => response.sendError( e.code, e.message )
				}
				case None => response.sendError( HttpServletResponse.SC_NOT_FOUND, methodName + " is not a @RestAction" )
			}
			case None => response.sendError( HttpServletResponse.SC_NOT_FOUND, methodName + " not found" )
		}
	}
}

class RestRequest( impl: HttpServletRequest, path: String ) extends Request( impl ) {
	lazy val query = impl.getRequestURI.substring( impl.getServletPath.length + 1 + path.length + ( if ( path.endsWith( "/" ) ) 0 else 1 ) )
}

class RestResponse( impl: HttpServletResponse, contentType: String ) extends Response( impl ) {
	override def send( what: ( => PrintWriter ) => Unit ) = {
		impl.setContentType( contentType )
		super.send( what )
	}
}

class RestError( val code: Int, val message: String ) extends RuntimeException( code + " " + message )

trait RestServlet2 extends Servlet {
	override def post( request: Request, response: Response ) = get( request, response )

	override def get( request: Request, response: Response ) = {
		val action = request.getRequestURI.substring( request.getServletPath.length + 1 )
		val methodName = if ( action.contains( "/" ) ) action.substring( 0, action.indexOf( "/" ) ) else action
		getClass.declaredMethod( methodName ) match {
			case None => response.sendError( HttpServletResponse.SC_NOT_FOUND, methodName + " not found" )
			case Some( method ) => method.annotation[RestMethod] match {
				case None => response.sendError( HttpServletResponse.SC_NOT_FOUND, methodName + " not exposed" )
				case Some( a ) => try {
					val result = method.invoke( this, method.parameters.map( p => Convert.to( request( p.annotation[RestParam].get.value ).get, p.clazz ) ) )
					a.produces match {
						case "application/json" =>
							response.setContentType( a.produces + "; charset=" + a.charset )
							response.send( _.println( if ( a.raw ) result else JSONBind.marshal( result ) ) )
						case _ => response.setStatus( HttpServletResponse.SC_OK )
					}
				} catch {
					case e: RestError => response.sendError( e.code, e.message )
				}
			}
		}
	}

}


<commit_msg>- WIP<commit_after>package butter4s.servlet.rest

import butter4s.servlet._
import butter4s.reflect._
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import java.io.PrintWriter
import butter4s.bind.json.JSONBind

/**
 * @author <NAME> <<EMAIL>>
 */
class RestServlet extends Servlet {
	override def post( request: Request, response: Response ) = get( request, response )

	override def get( request: Request, response: Response ) = {
		val action = request.getRequestURI.substring( request.getServletPath.length + 1 )
		val methodName = if ( action.contains( "/" ) ) action.substring( 0, action.indexOf( "/" ) ) else action
		getClass.declaredMethod( methodName ) match {
			case Some( method ) => method.annotation[RestAction] match {
				case Some( a ) => try {
					method.invoke( this, Array[AnyRef](
						new RestRequest( request, if ( a.path != RestConstants.DEFAULT ) a.path else method.name ),
						new RestResponse( response, a.produces + "; charset=" + a.charset )
						): _* )
				} catch {
					case e: RestError => response.sendError( e.code, e.message )
				}
				case None => response.sendError( HttpServletResponse.SC_NOT_FOUND, methodName + " is not a @RestAction" )
			}
			case None => response.sendError( HttpServletResponse.SC_NOT_FOUND, methodName + " not found" )
		}
	}
}

class RestRequest( impl: HttpServletRequest, path: String ) extends Request( impl ) {
	lazy val query = impl.getRequestURI.substring( impl.getServletPath.length + 1 + path.length + ( if ( path.endsWith( "/" ) ) 0 else 1 ) )
}

class RestResponse( impl: HttpServletResponse, contentType: String ) extends Response( impl ) {
	override def send( what: ( => PrintWriter ) => Unit ) = {
		impl.setContentType( contentType )
		super.send( what )
	}
}

class RestError( val code: Int, val message: String ) extends RuntimeException( code + " " + message )

trait RestServlet2 extends Servlet {
	override def post( request: Request, response: Response ) = get( request, response )

	override def get( request: Request, response: Response ) = {
		val action = request.getRequestURI.substring( request.getServletPath.length + 1 )
		val methodName = if ( action.contains( "/" ) ) action.substring( 0, action.indexOf( "/" ) ) else action
		getClass.declaredMethod( methodName ) match {
			case None => response.sendError( HttpServletResponse.SC_NOT_FOUND, methodName + " not found" )
			case Some( method ) => method.annotation[RestMethod] match {
				case None => response.sendError( HttpServletResponse.SC_NOT_FOUND, methodName + " not exposed" )
				case Some( a ) => try {
					val result = method.invoke( this, method.parameters.map( p => Convert.to( request( p.annotation[RestParam].get.value ).get, p.clazz ) ) )
					a.produces match {
						case "application/json" =>
							response.setContentType( a.produces + "; charset=" + a.charset )
							response.send( _.println( if ( a.raw ) result else JSONBind.marshal( result ) ) )
						case _ => response.setStatus( if ( result == null ) HttpServletResponse.SC_OK else result.asInstanceOf[Int] )
					}
				} catch {
					case e: RestError => response.sendError( e.code, e.message )
				}
			}
		}
	}

}



{'fraction_non_alphanumeric': 0.12507936507936507, 'fraction_numerical': 0.0038095238095238095, 'mean_word_length': 3.709267563527653, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4258568', 'n_tokens_mistral': 2120, 'n_tokens_neox': 1881, 'n_words': 514}