This is continuous of my problem in this post:
https://community.bonitasoft.com/questions-and-answers/groovy-script-how-get-temporary-file-upload-widget#comment-13733
Summary:
- My problem: With Upload widget, I need to verify the file content before accept uploading. Default upload widget only connect to servlet "formUpload" and I cannot access it to add the checking code.
- My solution: Write a REST API to verify the file content. If correct, call servlet "formUpload" to upload the file and return data normally.
I have writen a REST API to validate the file data, but unable to call servlet "formUpload".
My current code is as below:
RestApiResponse doHandle(HttpServletRequest request, RestApiResponseBuilder apiResponseBuilder, RestAPIContext context) { try { String server = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); InputStream inputStream = request.getInputStream() File file = File.createTempFile("temp", ".tmp"); FileOutputStream outputStream = null try { outputStream = new FileOutputStream(file, false) int read; byte[] bytes = new byte[DEFAULT_BUFFER_SIZE]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } } finally { if (outputStream != null) { outputStream.close(); } } if(file != null){ // do checking // if correct call formUpload as below //HTTPBuilder http = new HTTPBuilder(server) //def bonitaToken = login(http) //def temporaryFileOnServer = uploadFile(http, inputStream, data, "temp.tmp") } else{ LOGGER.info("file is null") } //=========================== // 正常終了のレスポンスを返す //=========================== apiResponseBuilder.withResponseStatus(HttpServletResponse.SC_OK) //200 def json = new groovy.json.JsonBuilder() json {"send" "ok"} buildResponse apiResponseBuilder, json.toPrettyString() } catch (Exception e) { return buildErrorResponse(apiResponseBuilder, e.message) } }
I try to follow the example on GitHub in https://github.com/Bonitasoft-Community/example-upload-sevlet
However, when I try to import the require lib as below, I have error.
import groovyx.net.http.HTTPBuilder import static groovyx.net.http.Method.POST import static groovyx.net.http.ContentType.* import org.apache.http.entity.mime.MultipartEntityBuilder import org.apache.http.entity.ContentType
Error:
28-Apr-2022 17:19:04.279 SEVERE [http-nio-8080-exec-4] org.restlet.resource.ServerResource.doCatch Exception or error caught in server resource org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: D:\Gakken\develop\05.Coding\workspace\tomcat\server\temp\bonita_portal_13212@IVS-P0280\tenants\1\pages\custompage_SendMail\AttachFile.groovy: 21: unable to resolve class groovyx.net.http.HTTPBuilder @ line 21, column 1. import groovyx.net.http.HTTPBuilder ^ D:\Gakken\develop\05.Coding\workspace\tomcat\server\temp\bonita_portal_13212@IVS-P0280\tenants\1\pages\custompage_SendMail\AttachFile.groovy: 23: unable to resolve class groovyx.net.http.ContentType @ line 23, column 1. import static groovyx.net.http.ContentType.* ^ D:\Gakken\develop\05.Coding\workspace\tomcat\server\temp\bonita_portal_13212@IVS-P0280\tenants\1\pages\custompage_SendMail\AttachFile.groovy: 22: unable to resolve class groovyx.net.http.Method @ line 22, column 1. import static groovyx.net.http.Method.POST ^ D:\Gakken\develop\05.Coding\workspace\tomcat\server\temp\bonita_portal_13212@IVS-P0280\tenants\1\pages\custompage_SendMail\AttachFile.groovy: 23: unable to resolve class groovyx.net.http.ContentType @ line 23, column 1. import static groovyx.net.http.ContentType.* ^
Is there any step that I need to do before import these classes?