Created
August 7, 2013 14:00
-
-
Save axeda/6174282 to your computer and use it in GitHub Desktop.
Store a file in the FileStore
This file contains hidden or 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
| import com.axeda.drm.sdk.Context | |
| import com.axeda.drm.sdk.data.* | |
| import com.axeda.drm.sdk.device.* | |
| import com.axeda.drm.sdk.mobilelocation.CurrentMobileLocationFinder | |
| import com.axeda.drm.sdk.mobilelocation.MobileLocation | |
| import com.axeda.drm.sdk.mobilelocation.MobileLocationFinder | |
| import com.axeda.sdk.v2.bridge.FileInfoBridge | |
| import com.axeda.services.v2.FileInfoCriteria | |
| import com.axeda.services.v2.ModelCriteria | |
| import static com.axeda.sdk.v2.dsl.Bridges.* | |
| import com.axeda.services.v2.ExecutionResult | |
| import com.axeda.services.v2.FileInfo | |
| import com.axeda.services.v2.FileInfoReference | |
| import com.axeda.services.v2.FileUploadSession | |
| import net.sf.json.JSONObject | |
| import groovy.json.JsonBuilder | |
| import net.sf.json.JSONArray | |
| import com.axeda.drm.sdk.scripto.Request | |
| import org.apache.commons.io.IOUtils | |
| import org.apache.commons.lang.exception.ExceptionUtils | |
| import com.axeda.common.sdk.id.Identifier | |
| import com.axeda.services.v2.ExternalCredentialCriteria | |
| import com.axeda.services.v2.NamedValue | |
| import com.axeda.services.v2.ExtendedMap | |
| import com.axeda.services.v2.ExtendedMapCriteria | |
| import groovyx.net.http.* | |
| import static groovyx.net.http.ContentType.* | |
| import static groovyx.net.http.Method.* | |
| import groovy.json.* | |
| import com.axeda.services.v2.DownloadFileInstruction | |
| import com.axeda.services.v2.SoftwarePackage | |
| import com.axeda.services.v2.SoftwarePackageCriteria | |
| import com.axeda.services.v2.CompressionMethod | |
| /* | |
| FileStore entry point to post and store files | |
| */ | |
| def contentType = "application/json" | |
| final def serviceName = "StoreFile" | |
| // Create a JSON Builder | |
| def json = new JsonBuilder() | |
| def outerMap = [:] | |
| // Global try/catch. Gotta have it, you never know when your code will be exceptional! | |
| try { | |
| Context CONTEXT = Context.getSDKContext() | |
| def filesList = [] | |
| def datestring = new Date().time | |
| FileInfoBridge fib = fileInfoBridge | |
| // all of our Request Parameters are available here | |
| def params = Request.parameters.size() > 0 ? Request.parameters : parameters | |
| def filename = Request?.headers?.'Content-Disposition' ? | |
| Request?.headers?.'Content-Disposition' : "file___" + datestring + ".txt" | |
| def filelabel = params.filelabel ?: filename | |
| def description = params.description ?: filename | |
| def contType = Request.headers?."content-type" ?: "text/plain" | |
| def tag = params.tag ?: "packagefile" | |
| def domain = params.domain ?: "http://yourdomain.axeda.com" | |
| // Software Package parameters | |
| def softwarePackageName = params.softwarePackageName ?: "myNewSoftwarePackage" | |
| def destinationDirectory = params.destinationDirectory ?: "C:\temp" | |
| def modelName = params.modelName ?: "your_model" | |
| def compressed = params.compressed ?: true | |
| def compressionMethod = params.compressionMethod ?: "TAR_GZ" | |
| def executable = params.executable ?: true | |
| def pathRelative = params.pathRelative ?: false | |
| def overwriteExistingEnabled = params.overwriteExistingEnabled ?: true | |
| def fileSizeLimit = 10000000 | |
| byte[] bytes = IOUtils.toByteArray(Request.inputStream); | |
| def bytesize = bytes.size() | |
| if (Request.inputStream.available() > 0) { | |
| if (bytesize > fileSizeLimit){ | |
| throw new Exception("$serviceName: File $filelabel over file size limit, byte size $bytesize") | |
| } | |
| outerMap.byteCount = bytesize | |
| FileInfo mypackageFile = new FileInfo(filelabel: filelabel, | |
| filename: filename, | |
| filesize: bytesize, | |
| description: description, | |
| tags: tag | |
| ) | |
| mypackageFile.contentType = contType | |
| FileUploadSession fus = new FileUploadSession(); | |
| fus.files = [mypackageFile] | |
| ExecutionResult fer = fileUploadSessionBridge.create(fus); | |
| mypackageFile.sessionId = fer.succeeded.getAt(0)?.id | |
| ExecutionResult fileInfoResult = fib.create(mypackageFile) | |
| if (fileInfoResult.successful) { | |
| outerMap.fileInfoSave = "File Info Saved" | |
| outerMap.sessionId = "File Upload SessionID: "+fer.succeeded.getAt(0)?.id | |
| outerMap.fileInfoId = "FileInfo ID: "+fileInfoResult?.succeeded.getAt(0)?.id | |
| ExecutionResult er = fib.saveOrUpdate(fileInfoResult.succeeded.getAt(0).id,new ByteArrayInputStream(bytes)) | |
| def fileInfoId = fileInfoResult?.succeeded.getAt(0)?.id | |
| String url = "${domain}/services/v1/rest/Scripto/execute/DownloadFile?fileId=${fileInfoId}" | |
| if (er.successful) { | |
| outerMap.url = url | |
| } else { | |
| outerMap.save = "false" | |
| logFailure(er,outerMap) | |
| } | |
| } else { | |
| logFailure(fileInfoResult, outerMap) | |
| } | |
| } else { | |
| outerMap.bytesAvail = "No bytes found to upload $bytesize" | |
| } | |
| // return the JSONBuilder contents | |
| // we specify the content type, and any object as the return (even an outputstream!) | |
| return ["Content-Type": contentType,"Content":JSONArray.fromObject(outerMap).toString(2)] | |
| // alternately you may just want to serial an Object as JSON: | |
| // return ["Content-Type": contentType,"Content":JSONArray.fromObject(invertedMessages).toString(2)] | |
| } catch (Exception e) { | |
| // I knew you were exceptional! | |
| // we'll capture the output of the stack trace and return it in JSON | |
| json.Exception( | |
| description: "Execution Failed!!! An Exception was caught...", | |
| stack: ExceptionUtils.getFullStackTrace(e) | |
| ) | |
| // return the output | |
| return ["Content-Type": contentType, "Content": json.toPrettyString()] | |
| } | |
| private void logFailure(ExecutionResult fileInfoResult, LinkedHashMap outerMap) { | |
| outerMap.message = fileInfoResult.failures.getAt(0)?.message | |
| outerMap.source = fileInfoResult.failures.getAt(0)?.sourceOfFailure | |
| outerMap.details = fileInfoResult.failures.getAt(0)?.details?.toString() | |
| outerMap.fileInfoSave = "false" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment