Search in sources :

Example 1 with JSMap

use of com.servoy.j2db.scripting.JSMap in project servoy-client by Servoy.

the class NativeObjectSerializer method unmarshallJSONObject.

public Object unmarshallJSONObject(SerializerState state, Class clazz, JSONObject jso) throws UnmarshallException {
    // legacy, remove prefixes
    boolean hasJavaClass = jso.has("javaClass");
    JSMap<Object, Object> no = new JSMap<Object, Object>(NativeArray.class.equals(clazz) ? "Array" : null);
    for (String jsonKey : Utils.iterate(jso.keys())) {
        String jsonProperty = jsonKey;
        if (hasJavaClass) {
            // legacy
            if (jsonKey.equals("javaClass"))
                continue;
            if (jsonKey.startsWith(PROPERTY_MARK)) {
                jsonProperty = jsonKey.substring(PROPERTY_MARK.length());
            }
        }
        Object jsonValue;
        try {
            jsonValue = jso.get(jsonKey);
        } catch (JSONException e) {
            // $NON-NLS-1$
            throw new UnmarshallException("JSONException: " + e.getMessage(), e);
        }
        jsonValue = getUnmarshalled(state, jsonValue);
        try {
            int jsonIntKey = Integer.parseInt(jsonProperty);
            no.put(Integer.valueOf(jsonIntKey), jsonValue);
        } catch (NumberFormatException ex) {
            // property key is a string
            no.put(jsonProperty, jsonValue);
        }
    }
    return no;
}
Also used : JSONException(org.json.JSONException) IdScriptableObject(org.mozilla.javascript.IdScriptableObject) NativeObject(org.mozilla.javascript.NativeObject) JSONObject(org.json.JSONObject) JSMap(com.servoy.j2db.scripting.JSMap) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 2 with JSMap

use of com.servoy.j2db.scripting.JSMap in project servoy-client by Servoy.

the class MapSerializer method convertFromMap.

public static Object convertFromMap(Map<String, Object> objAsMap) {
    JSMap<String, Object> retval = new JSMap<String, Object>();
    Iterator<Map.Entry<String, Object>> it = objAsMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, Object> entry = it.next();
        Object propertyValue = entry.getValue();
        if (propertyValue instanceof Map) {
            propertyValue = convertFromMap((Map<String, Object>) propertyValue);
        }
        retval.put(entry.getKey(), propertyValue);
    }
    return retval;
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) IdScriptableObject(org.mozilla.javascript.IdScriptableObject) JSMap(com.servoy.j2db.scripting.JSMap) Map(java.util.Map) JSMap(com.servoy.j2db.scripting.JSMap) HashMap(java.util.HashMap)

Example 3 with JSMap

use of com.servoy.j2db.scripting.JSMap in project servoy-client by Servoy.

the class TusServlet method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    tusFileUploadService.process(req, resp);
    String uploadURI = req.getRequestURI();
    UploadInfo uploadInfo = null;
    try {
        uploadInfo = this.tusFileUploadService.getUploadInfo(uploadURI);
    } catch (IOException | TusException e) {
        e.printStackTrace();
    }
    if (uploadInfo != null && !uploadInfo.isUploadInProgress()) {
        try {
            Path uploadedPath = this.tusFileUploadService.getUploadedPath(uploadURI);
            File file = uploadedPath.toFile();
            TusFileItem fileItem = new TusFileItem(file, uploadInfo, uploadURI);
            AbstractMediaResourceServlet.FILE_CLEANING_TRACKER.track(file, file, new TusFileDeleteStategy(fileItem));
            String path = req.getPathInfo();
            if (path.startsWith("/"))
                path = path.substring(1);
            String[] paths = path.split("/");
            int clientnr = paths[1].length() == 0 ? -1 : Integer.parseInt(paths[1]);
            final INGClientWebsocketSession wsSession = getSession(req, clientnr);
            if (wsSession != null) {
                // clear out the tus id that is the last path entry (so it is not confused with the row id)
                paths[paths.length - 1] = null;
                JSMap<String, String> map = new JSMap<>();
                map.putAll(uploadInfo.getMetadata());
                callClient(req, paths, wsSession, map, fileItem);
            }
        } catch (IOException | TusException e) {
            Debug.error(e);
        }
    }
}
Also used : UploadInfo(me.desair.tus.server.upload.UploadInfo) Path(java.nio.file.Path) JSMap(com.servoy.j2db.scripting.JSMap) IOException(java.io.IOException) TusException(me.desair.tus.server.exception.TusException) IFile(com.servoy.j2db.plugins.IFile) File(java.io.File)

Example 4 with JSMap

use of com.servoy.j2db.scripting.JSMap in project servoy-client by Servoy.

the class MediaResourcesServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String path = req.getPathInfo();
    if (path.startsWith("/"))
        path = path.substring(1);
    String[] paths = path.split("/");
    String reqEncoding = req.getCharacterEncoding() == null ? "UTF-8" : req.getCharacterEncoding();
    if ((paths.length == 2 || paths.length >= 5) && paths[0].equals("upload")) {
        if (req.getHeader("Content-Type") != null && req.getHeader("Content-Type").startsWith("multipart/form-data")) {
            int clientnr = paths[1].length() == 0 ? -1 : Integer.parseInt(paths[1]);
            final INGClientWebsocketSession wsSession = getSession(req, clientnr);
            try {
                if (wsSession != null) {
                    Settings settings = Settings.getInstance();
                    File fileUploadDir = null;
                    String uploadDir = settings.getProperty("servoy.ng_web_client.temp.uploadir");
                    if (uploadDir != null) {
                        fileUploadDir = new File(uploadDir);
                        if (!fileUploadDir.exists() && !fileUploadDir.mkdirs()) {
                            fileUploadDir = null;
                            Debug.error("Couldn't use the property 'servoy.ng_web_client.temp.uploadir' value: '" + uploadDir + "', directory could not be created or doesn't exists");
                        }
                    }
                    int tempFileThreshold = Utils.getAsInteger(settings.getProperty("servoy.ng_web_client.tempfile.threshold", "50"), false) * 1000;
                    DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(tempFileThreshold, fileUploadDir);
                    diskFileItemFactory.setFileCleaningTracker(FILE_CLEANING_TRACKER);
                    ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
                    upload.setHeaderEncoding(reqEncoding);
                    long maxUpload = Utils.getAsLong(settings.getProperty("servoy.webclient.maxuploadsize", "0"), false);
                    if (maxUpload > 0)
                        upload.setFileSizeMax(maxUpload * 1000);
                    Iterator<FileItem> iterator = upload.parseRequest(req).iterator();
                    final List<FileUploadData> aFileUploadData = new ArrayList<FileUploadData>();
                    List<FileItem> formFields = new ArrayList<>();
                    while (iterator.hasNext()) {
                        FileItem item = iterator.next();
                        if (item.isFormField()) {
                            formFields.add(item);
                        } else {
                            String encoding = StringUtils.defaultString(req.getCharacterEncoding(), "UTF-8");
                            JSMap<String, String> fieldsMap = new JSMap<>();
                            for (FileItem fileItem : formFields) {
                                try {
                                    fieldsMap.put(fileItem.getFieldName(), fileItem.getString(encoding));
                                } catch (UnsupportedEncodingException e) {
                                    Debug.error(e);
                                }
                            }
                            if (callClient(req, paths, wsSession, fieldsMap, item)) {
                                formFields = new ArrayList<>();
                            } else {
                                // it is a file from the built-in file selector
                                aFileUploadData.add(new FileUploadData(item));
                            }
                        }
                    }
                    if (aFileUploadData.size() > 0) {
                        final IMediaUploadCallback mediaUploadCallback = ((NGClient) wsSession.getClient()).getMediaUploadCallback();
                        if (mediaUploadCallback != null) {
                            // leave time for this request to finish, before executing the callback, so the file
                            // dialog can do its close
                            ((NGClient) wsSession.getClient()).invokeLater(new Runnable() {

                                @Override
                                public void run() {
                                    mediaUploadCallback.uploadComplete(aFileUploadData.toArray(new FileUploadData[aFileUploadData.size()]));
                                    mediaUploadCallback.onSubmit();
                                }
                            });
                        }
                    }
                }
            } catch (FileSizeLimitExceededException ex) {
                res.setStatus(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
                if (wsSession != null)
                    res.getWriter().print(wsSession.getClient().getI18NMessage("servoy.filechooser.sizeExceeded", new Object[] { ex.getPermittedSize() / 1000 + "KB" }));
            } catch (FileUploadException ex) {
                ex.printStackTrace();
                throw new ServletException(ex.toString());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) FileSizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException) ServletException(javax.servlet.ServletException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) IMediaUploadCallback(com.servoy.j2db.plugins.IMediaUploadCallback) Settings(com.servoy.j2db.util.Settings) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSMap(com.servoy.j2db.scripting.JSMap) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

JSMap (com.servoy.j2db.scripting.JSMap)4 File (java.io.File)2 IdScriptableObject (org.mozilla.javascript.IdScriptableObject)2 NativeObject (org.mozilla.javascript.NativeObject)2 IFile (com.servoy.j2db.plugins.IFile)1 IMediaUploadCallback (com.servoy.j2db.plugins.IMediaUploadCallback)1 Settings (com.servoy.j2db.util.Settings)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 TusException (me.desair.tus.server.exception.TusException)1 UploadInfo (me.desair.tus.server.upload.UploadInfo)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileSizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)1