Search in sources :

Example 1 with HttpContext

use of com.genexus.internet.HttpContext in project JavaClasses by genexuslabs.

the class UserInformation method getLocalUtil.

public LocalUtil getLocalUtil() {
    String androidLanguage = com.artech.base.services.AndroidContext.ApplicationContext.getLanguageName();
    if (localUtil != null && androidLanguage != null) {
        String androidLanguageCode = Application.getClientPreferences().getProperty("language|" + androidLanguage, "code", (String) null);
        if (androidLanguageCode != null && !localUtil._language.equals(androidLanguageCode)) {
            // re create localUtil, language has changed.
            System.out.println("change LocalUtil language: " + androidLanguage + "");
            ModelContext tempContext = ModelContext.getModelContext();
            if (tempContext != null) {
                int res = ((HttpContext) tempContext.getHttpContext()).setLanguage(androidLanguage);
                this.setLocalUtil(tempContext.getHttpContext().getLanguageProperty("decimal_point").charAt(0), tempContext.getHttpContext().getLanguageProperty("date_fmt"), tempContext.getHttpContext().getLanguageProperty("time_fmt"), tempContext.getClientPreferences().getYEAR_LIMIT(), tempContext.getHttpContext().getLanguageProperty("code"));
            }
        }
    }
    return localUtil;
}
Also used : ModelContext(com.genexus.ModelContext) HttpContext(com.genexus.internet.HttpContext)

Example 2 with HttpContext

use of com.genexus.internet.HttpContext in project JavaClasses by genexuslabs.

the class GXPreparedStatement method setGXDbFileURI.

public void setGXDbFileURI(int index, String fileName, String blobPath, int length, String tableName, String fieldName) throws SQLException {
    ExternalProvider storageProvider = Application.getExternalProvider();
    fileName = fileName.trim();
    blobPath = blobPath.trim();
    String uploadNameValue = SpecificImplementation.GXutil.getUploadNameValue(blobPath);
    // EMPTY BLOB
    if (blobPath == null || blobPath.trim().length() == 0) {
        setVarchar(index, ExternalProviderCommon.getProviderObjectAbsoluteUriSafe(storageProvider, fileName), length, false);
        return;
    }
    String fileUri = "";
    String multimediaTemporalPath = com.genexus.Preferences.getDefaultPreferences().getMultimediaPath();
    if (fileName.length() > 0 && !(blobPath.startsWith(multimediaTemporalPath) && GXutil.isAbsoluteURL(fileName))) {
        fileUri = GXDbFile.generateUri(fileName, !GXDbFile.hasToken(fileName), true);
    } else if (blobPath.trim().length() > 0) {
        blobPath = com.genexus.GXutil.cutUploadPrefix(blobPath);
        File file = new File(blobPath);
        fileUri = GXDbFile.generateUri(uploadNameValue.isEmpty() ? file.getName() : uploadNameValue, !GXDbFile.hasToken(blobPath), true);
    }
    boolean externalStorageEnabled = storageProvider != null;
    // Should improve this condition in order to not depend on tableName and FieldName.
    boolean attInExternalStorage = externalStorageEnabled && (tableName != null && fieldName != null);
    if (!attInExternalStorage) {
        setVarchar(index, fileUri, length, false);
        return;
    }
    // External Storage is ENABLED. We have the following cases:
    // - 1. WebUpload: The URL is an External Storage URL in the Private Temp Storage Folder.
    // - 2. External URL: An absolute URL (outside External Storage).
    // - 3. Transaction Update Mode (nothing changed): An Storage URL that is already in External Storage (except in private), should not be uploaded nor copied.
    // - 4. Upload Resource from local drive to External Storage: Ex: Image.FromImage(ActionDelete)
    String storageObjectName = ExternalProviderCommon.getProviderObjectName(storageProvider, blobPath);
    boolean resourceAlreadyOnStorage = storageObjectName != null;
    ResourceAccessControlList defaultAcl = ResourceAccessControlList.Default;
    String storageTargetObjectName = blobPath;
    boolean isPrivateTempUpload = storageTargetObjectName.startsWith(Application.getClientPreferences().getTMPMEDIA_DIR());
    String folder = Application.getGXServices().get(GXServices.STORAGE_SERVICE).getProperties().get("FOLDER_NAME");
    if (!resourceAlreadyOnStorage && (fileName.equals(blobPath) || GXutil.isAbsoluteURL(fileName))) {
        // - 2. External URL: An absolute URL (outside External Storage).
        try (InputStream is = new URL(fileName).openStream()) {
            int idx = storageTargetObjectName.lastIndexOf("/");
            if ((idx != -1) && (idx < storageTargetObjectName.length() - 1)) {
                storageTargetObjectName = storageTargetObjectName.substring(idx + 1);
            }
            storageTargetObjectName = com.genexus.PrivateUtilities.getTempFileName("", CommonUtil.getFileName(storageTargetObjectName), CommonUtil.getFileType(storageTargetObjectName), true);
            storageTargetObjectName = folder + "/" + tableName + "/" + fieldName + "/" + storageTargetObjectName;
            fileUri = Application.getExternalProvider().upload(storageTargetObjectName, is, defaultAcl);
        } catch (IOException e) {
            throw new SQLException("An error occurred while downloading data from url: " + fileName + e.getMessage());
        }
    } else {
        if (resourceAlreadyOnStorage && !isPrivateTempUpload) {
            // - 3. Transaction Update Mode (nothing changed): An Storage URL that is already in External Storage (except in private), should not be uploaded nor copied.
            fileUri = storageObjectName;
        } else {
            // Every temporal file is saved as private.
            GXFile gxFile = new GXFile(storageTargetObjectName, ResourceAccessControlList.Private);
            if (gxFile.exists()) {
                // - 1. WebUpload: The URL is an External Storage URL in the Private Temp Storage Folder.
                fileName = uploadNameValue.isEmpty() ? gxFile.getName() : uploadNameValue;
                int idx = fileName.lastIndexOf("/");
                if ((idx != -1) && (idx < fileName.length() - 1)) {
                    fileName = fileName.substring(idx + 1);
                }
                fileName = com.genexus.PrivateUtilities.getTempFileName("", CommonUtil.getFileName(fileName), CommonUtil.getFileType(fileName), true);
                fileUri = storageProvider.copy(gxFile.getAbsoluteName(), fileName, tableName, fieldName, defaultAcl);
            } else {
                // - 4. Upload Resource from local drive to External Storage: Ex: Image.FromImage(ActionDelete)
                int idx = blobPath.lastIndexOf("/");
                if ((idx != -1) && (idx < blobPath.length() - 1)) {
                    fileName = blobPath.substring(idx + 1);
                }
                fileName = com.genexus.PrivateUtilities.getTempFileName("", CommonUtil.getFileName(fileName), CommonUtil.getFileType(fileName), true);
                fileName = folder + "/" + tableName + "/" + fieldName + "/" + fileName;
                if (con.getContext() != null) {
                    com.genexus.internet.HttpContext webContext = (HttpContext) con.getContext().getHttpContext();
                    if ((webContext != null) && (webContext instanceof com.genexus.webpanels.HttpContextWeb) && (blobPath.startsWith(webContext.getContextPath()) || blobPath.startsWith(webContext.getDefaultPath()))) {
                        blobPath = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(blobPath);
                    }
                }
                fileUri = storageProvider.upload(blobPath, fileName, defaultAcl);
            }
        }
    }
    setVarchar(index, fileUri, length, false);
}
Also used : SQLException(java.sql.SQLException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpContext(com.genexus.internet.HttpContext) IOException(java.io.IOException) HttpContext(com.genexus.internet.HttpContext) GXFile(com.genexus.util.GXFile) URL(java.net.URL) GXFile(com.genexus.util.GXFile) File(java.io.File)

Example 3 with HttpContext

use of com.genexus.internet.HttpContext in project JavaClasses by genexuslabs.

the class GXPreparedStatement method setBLOBFile.

public void setBLOBFile(int index, String fileName, boolean isMultiMedia) throws SQLException {
    if (isMultiMedia && Application.getGXServices().get(GXServices.STORAGE_SERVICE) != null) {
        fileName = "";
    }
    if (fileName != null && !fileName.trim().equals("") && !fileName.toLowerCase().trim().endsWith("about:blank") && Application.getGXServices().get(GXServices.STORAGE_SERVICE) == null) {
        fileName = com.genexus.GXutil.cutUploadPrefix(fileName);
        try {
            if (fileName.toLowerCase().startsWith("http://") || fileName.toLowerCase().startsWith("https://")) {
                int queryIndex = fileName.lastIndexOf('?');
                if (queryIndex > -1)
                    fileName = fileName.substring(0, queryIndex + 1) + PrivateUtilities.encodeURL(fileName.substring(queryIndex + 1));
                URL fileURL = new URL(fileName);
                String blobPath = com.genexus.Preferences.getDefaultPreferences().getBLOB_PATH();
                fileName = com.genexus.PrivateUtilities.getTempFileName(blobPath, CommonUtil.getFileName(fileName), CommonUtil.getFileType(fileName), true);
                com.genexus.PrivateUtilities.InputStreamToFile(fileURL.openStream(), fileName);
            }
        } catch (MalformedURLException e) {
            throw new SQLException("Malformed URL " + fileName);
        } catch (IOException e) {
            throw new SQLException("An error occurred while downloading data from url: " + fileName + e.getMessage());
        }
        if (con.getContext() != null) {
            com.genexus.internet.HttpContext webContext = (HttpContext) con.getContext().getHttpContext();
            if (webContext != null) {
                if (webContext instanceof com.genexus.webpanels.HttpContextWeb) {
                    fileName = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(fileName);
                } else {
                    if (!webContext.getDefaultPath().isEmpty() && !new File(fileName).isAbsolute()) {
                        if (fileName.startsWith(webContext.getContextPath())) {
                            fileName = fileName.substring(webContext.getContextPath().length() + 1);
                        }
                        fileName = webContext.getDefaultPath() + File.separator + fileName;
                    }
                }
            }
        }
    }
    if (skipSetBlobs) {
        if (blobFiles == null) {
            blobFiles = new String[index];
        } else if (blobFiles.length < index) {
            String[] temp = blobFiles;
            blobFiles = new String[index];
            System.arraycopy(temp, 0, blobFiles, 0, temp.length);
        }
        blobFiles[index - 1] = fileName;
    } else {
        if (fileName != null && !fileName.trim().equals("") && !fileName.toLowerCase().trim().endsWith("about:blank")) {
            if (Application.getExternalProvider() == null) {
                try {
                    File file = new File(fileName);
                    BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
                    setBinaryStream(index, inputStream, (int) file.length());
                } catch (IOException e) {
                    throw new SQLException("The filename does not exists in url " + fileName);
                }
            } else {
                GXFile gxFile = new GXFile(fileName, ResourceAccessControlList.Private);
                if (gxFile.exists()) {
                    InputStream is = gxFile.getStream();
                    setBinaryStream(index, is, (int) gxFile.getLength());
                } else {
                    try {
                        File localFile = new File(fileName);
                        BufferedInputStream localInputStream = new BufferedInputStream(new FileInputStream(localFile));
                        setBinaryStream(index, localInputStream, (int) localFile.length());
                    } catch (IOException e) {
                        throw new SQLException("The filename does not exists in url " + fileName);
                    }
                }
            }
        } else {
            if (con.getDBMS().getId() == GXDBMS.DBMS_ORACLE) {
                BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(new byte[] { 0 }));
                setBinaryStream(index, inputStream, (int) 1);
            } else {
                BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(new byte[] {}));
                setBinaryStream(index, inputStream, (int) 0);
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) SQLException(java.sql.SQLException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpContext(com.genexus.internet.HttpContext) IOException(java.io.IOException) HttpContext(com.genexus.internet.HttpContext) GXFile(com.genexus.util.GXFile) URL(java.net.URL) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) com.genexus(com.genexus) GXFile(com.genexus.util.GXFile) File(java.io.File)

Example 4 with HttpContext

use of com.genexus.internet.HttpContext in project JavaClasses by genexuslabs.

the class GXDbFile method pathToUrl.

public static String pathToUrl(String path, boolean forceAbsPath) {
    ModelContext context = ModelContext.getModelContext();
    com.genexus.internet.HttpContext webContext = (HttpContext) context.getHttpContext();
    return pathToUrl(path, webContext, forceAbsPath);
}
Also used : HttpContext(com.genexus.internet.HttpContext) HttpContext(com.genexus.internet.HttpContext)

Example 5 with HttpContext

use of com.genexus.internet.HttpContext in project JavaClasses by genexuslabs.

the class GXutil method charToTimeREST.

public static Date charToTimeREST(String value) {
    try {
        if (value == null || value.equals("0000-00-00T00:00:00") || value.equals("0000-00-00T00:00:00.000")) {
            return nullDate();
        } else {
            if (value.indexOf('/') >= 0) {
                return Application.getClientLocalUtil().ctot(value);
            } else {
                String valuetmp = value.replace("T", " ");
                // has milliseconds ?
                int milIndex = valuetmp.length();
                SimpleDateFormat dateFormat = null;
                if ((milIndex == 23) && (valuetmp.substring(milIndex - 4, milIndex - 3).equals("."))) {
                    dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                } else {
                    dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                }
                Date valueAux = dateFormat.parse(valuetmp);
                if (Application.getClientPreferences().useTimezoneFix()) {
                    boolean isRest = !ModelContext.getModelContext().isTimezoneSet() || ((HttpContext) ModelContext.getModelContext().getHttpContext()).isRestService();
                    isRest = CommonUtil.shouldConvertDateTime(valueAux, isRest);
                    if (isRest) {
                        if (ModelContext.getModelContext().isTimezoneSet())
                            valueAux = CommonUtil.ConvertDateTime(valueAux, TimeZone.getTimeZone("GMT"), ModelContext.getModelContext().getClientTimeZone());
                    }
                }
                return valueAux;
            }
        }
    } catch (ParseException ex) {
        return nullDate();
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) HttpContext(com.genexus.internet.HttpContext) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

HttpContext (com.genexus.internet.HttpContext)30 File (java.io.File)5 WebSession (com.genexus.webpanels.WebSession)4 IOException (java.io.IOException)4 ModelContext (com.genexus.ModelContext)3 GXFile (com.genexus.util.GXFile)3 HttpContextWeb (com.genexus.webpanels.HttpContextWeb)3 com.genexus (com.genexus)2 HttpContextNull (com.genexus.internet.HttpContextNull)2 HttpRequest (com.genexus.internet.HttpRequest)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 SQLException (java.sql.SQLException)2 IExtensionModelContext (com.genexus.common.interfaces.IExtensionModelContext)1 IGxJSONAble (com.genexus.internet.IGxJSONAble)1 GXResult (com.genexus.security.GXResult)1 ServletException (com.genexus.servlet.ServletException)1