Skip to content

Instantly share code, notes, and snippets.

@thiagopnts
Created August 24, 2012 02:14
Show Gist options
  • Save thiagopnts/3444751 to your computer and use it in GitHub Desktop.
Save thiagopnts/3444751 to your computer and use it in GitHub Desktop.
package models
import org.apache._
import org.apache.http.HttpResponse
import org.apache.http.impl.auth._
import org.apache.http.impl.client._
import org.apache.http.client.methods._
import org.apache.http.entity.mime._
import org.apache.http.entity.mime.content._
import org.apache.http.client._
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.protocol.BasicHttpContext
import org.apache.http.entity.StringEntity
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager
import org.apache.http.params.BasicHttpParams
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.protocol.HTTP
import org.apache.http.message.BasicNameValuePair
import org.apache.http.NameValuePair
import play.api.Logger
import play.api.libs.json._
class JsonOfficeDropService extends OfficeDropService {
val client = new DefaultHttpClient()
def uploadFile(configuration: WebAppConfiguration): JsValue = {
val request = new HttpPost(configuration.host + "/ze/api/documents.json")
val entity = new MultipartEntity()
val credencial = this.getCredencial(configuration.username, configuration.password)
entity.addPart("file", new FileBody(new java.io.File("resources/file.pdf"), Utils.generateRandomPdfName, "application/pdf", "UTF-8"))
request.setEntity(entity)
request.addHeader(new BasicScheme().authenticate(credencial, request, new BasicHttpContext()))
val response = this.getClient.execute(request)
Utils.toJSON(this.getResponseContent(response))
}
def getDocumentStatus(configuration: WebAppConfiguration, documentId: Int): JsValue = {
val request = new HttpGet(configuration.host + "/ze/api/documents/" + documentId + ".json")
val credencial = this.getCredencial(configuration.username, configuration.password)
request.addHeader(new BasicScheme().authenticate(credencial, request, new BasicHttpContext()))
val response = this.getClient.execute(request)
Utils.toJSON(getResponseContent(response))
}
def deleteDocument(configuration: WebAppConfiguration, documentId: Int) = {
val params = new java.util.ArrayList[NameValuePair]()
params.add(new BasicNameValuePair("document_ids", documentId.toString))
val entity = new UrlEncodedFormEntity(params, "UTF-8")
val request = preparePostRequest(configuration, "/ze/api/documents/delete.json")
request.setEntity(entity)
val code = this.getClient.execute(request).getStatusLine.getStatusCode
if(code != 200) {
Logger.error("Document not deleted.")
} else {
Logger.info("Document deleted.")
}
}
def purgeDocument(configuration: WebAppConfiguration, documentId: Int) = {
val params = new java.util.ArrayList[NameValuePair]()
params.add(new BasicNameValuePair("document_ids", documentId.toString))
val entity = new UrlEncodedFormEntity(params, "UTF-8")
val request = preparePostRequest(configuration, "/ze/api/documents/purge.json")
request.setEntity(entity)
val code = this.getClient.execute(request).getStatusLine.getStatusCode
if(code != 200) {
Logger.error("Document not purged.")
} else {
Logger.info("Document purged.")
}
}
private def preparePostRequest(configuration: WebAppConfiguration, uri: String): HttpPost = {
val request = new HttpPost(configuration.host + uri)
val credencial = this.getCredencial(configuration.username, configuration.password)
request.addHeader(new BasicScheme().authenticate(credencial, request, new BasicHttpContext()))
return request
}
private def getCredencial(username: String, password: String) = new UsernamePasswordCredentials(username, password)
private def getResponseContent(response: HttpResponse) = io.Source.fromInputStream(response.getEntity.getContent).getLines.mkString
private def getClient = new DefaultHttpClient()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment