Created
August 24, 2012 02:14
-
-
Save thiagopnts/3444751 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 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