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;
}
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;
}
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);
}
}
}
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());
}
}
}
}
Aggregations