package com.socrata.soda.server.resources

import com.socrata.http.server.routing.SimpleResource
import com.socrata.http.server.{HttpRequest, HttpResponse, HttpService}
import com.socrata.http.server.implicits._
import com.socrata.http.server.responses._
import com.socrata.soda.server._
import com.socrata.soda.server.SodaUtils
import com.socrata.soda.server.responses.HttpMethodNotAllowed
import org.apache.commons.codec.binary.Base64

class SodaResource extends SimpleResource {

  override def methodNotAllowed: HttpService = { req =>
    val allowed = allowedMethods
    Header("Allow", allowed.mkString(",")) ~> SodaUtils.response(
      req,
      HttpMethodNotAllowed(req.getMethod, allowed))
  }

  def user(req: HttpRequest): String = {
    val fromHeader = for {
      authenticate <- req.header("Authenticate")
      if authenticate.startsWith("Basic ")
    } yield new String(Base64.decodeBase64(authenticate.split(" ", 1)(1)), "latin1").split(":")(0)
    fromHeader.getOrElse {
      SodaResource.log.info("Unable to get user out of Authenticate header; going with `anonymous' for now")
      "anonymous"
    }
  }

  def optionalHeader(header: String, headerValue: Option[String]): HttpResponse =
    headerValue match {
      case Some(v) => Header(header, v)
      case None => Function.const(())
    }
}

object SodaResource {
  private val log = org.slf4j.LoggerFactory.getLogger(classOf[SodaResource])
}

package com.socrata.soda.server.resources

import com.socrata.http.server.routing.SimpleResource
import com.socrata.http.server.{HttpRequest, HttpResponse, HttpService}
import com.socrata.http.server.implicits._
import com.socrata.http.server.responses._
import com.socrata.soda.server._
import com.socrata.soda.server.SodaUtils
import com.socrata.soda.server.responses.HttpMethodNotAllowed
import org.apache.commons.codec.binary.Base64

class SodaResource extends SimpleResource {

  override def methodNotAllowed: HttpService = { req =>
    val allowed = allowedMethods
    Header("Allow", allowed.mkString(",")) ~> SodaUtils.response(
      req,
      HttpMethodNotAllowed(req.getMethod, allowed))
  }

  // SF doesn't handle auth, unlike what was originally envisionsed, but is still wired up to pass user info down
  // to DC, this could be used for debugging or ripped out.
  def user(req: HttpRequest): String = "anonymous"

  def optionalHeader(header: String, headerValue: Option[String]): HttpResponse =
    headerValue match {
      case Some(v) => Header(header, v)
      case None => Function.const(())
    }
}

object SodaResource {
  private val log = org.slf4j.LoggerFactory.getLogger(classOf[SodaResource])
}

Stop logging about not getting a user passed in, we know we don't do that.
Stop logging about not getting a user passed in, we know we don't do that.

We don't pass user infomation down to sf so it just logs a silly
message.   Not ripping it out entirely because that would also require changes
in DC... we certainly could easily start passing down a user for debugging info but that
wouldn't be in the Authorization header since it isn't auth.
