Search in sources :

Example 1 with Action

use of org.apache.struts2.convention.annotation.Action in project qi4j-sdk by Qi4j.

the class Qi4jCodebehindPackageProvider method loadPackageConfig.

/**
     * Finds or creates the package configuration for an Action class.
     *
     * The namespace annotation is honored, if found,
     * and the namespace is checked for a parent configuration.
     *
     * @param actionNamespace The configuration namespace
     * @param actionPackage   The Java package containing our Action classes
     * @param actionClass     The Action class instance
     *
     * @return PackageConfig object for the Action class
     */
protected PackageConfig.Builder loadPackageConfig(String actionNamespace, String actionPackage, Class actionClass) {
    PackageConfig.Builder parent = null;
    // Check for the @Namespace annotation
    if (actionClass != null) {
        Namespace ns = (Namespace) actionClass.getAnnotation(Namespace.class);
        if (ns != null) {
            parent = loadPackageConfig(actionNamespace, actionPackage, null);
            actionNamespace = ns.value();
            actionPackage = actionClass.getName();
        // See if the namespace has been overridden by the @Action annotation
        } else {
            org.apache.struts2.config.Action actionAnn = (org.apache.struts2.config.Action) actionClass.getAnnotation(org.apache.struts2.config.Action.class);
            if (actionAnn != null && !actionAnn.DEFAULT_NAMESPACE.equals(actionAnn.namespace())) {
                // we pass null as the namespace in case the parent package hasn't been loaded yet
                parent = loadPackageConfig(null, actionPackage, null);
                actionPackage = actionClass.getName();
            }
        }
    }
    PackageConfig.Builder pkgConfig = packageLoader.getPackage(actionPackage);
    if (pkgConfig == null) {
        pkgConfig = new PackageConfig.Builder(actionPackage);
        pkgConfig.namespace(actionNamespace);
        if (parent == null) {
            PackageConfig cfg = configuration.getPackageConfig(defaultParentPackage);
            if (cfg != null) {
                pkgConfig.addParent(cfg);
            } else {
                throw new ConfigurationException("ClasspathPackageProvider: Unable to locate default parent package: " + defaultParentPackage);
            }
        }
        packageLoader.registerPackage(pkgConfig);
    // if the parent package was first created by a child, ensure the namespace is correct
    } else if (pkgConfig.getNamespace() == null) {
        pkgConfig.namespace(actionNamespace);
    }
    if (parent != null) {
        packageLoader.registerChildToParent(pkgConfig, parent);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("class:" + actionClass + " parent:" + parent + " current:" + (pkgConfig != null ? pkgConfig.getName() : ""));
    }
    return pkgConfig;
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) org.apache.struts2.config(org.apache.struts2.config) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 2 with Action

use of org.apache.struts2.convention.annotation.Action in project KeyBox by skavanagh.

the class OTPAction method qrImage.

@Action(value = "/admin/qrImage")
public String qrImage() {
    String username = UserDB.getUser(AuthUtil.getUserId(servletRequest.getSession())).getUsername();
    String secret = AuthUtil.getOTPSecret(servletRequest.getSession());
    AuthUtil.setOTPSecret(servletRequest.getSession(), null);
    try {
        String qrCodeText = "otpauth://totp/KeyBox%20%28" + URLEncoder.encode(servletRequest.getHeader("host").replaceAll("\\:.*$", ""), "utf-8") + "%29:" + username + "?secret=" + secret;
        QRCodeWriter qrWriter = new QRCodeWriter();
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix matrix = qrWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT, hints);
        servletResponse.setContentType("image/png");
        BufferedImage image = new BufferedImage(QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT);
        graphics.setColor(Color.BLACK);
        for (int x = 0; x < QR_IMAGE_WIDTH; x++) {
            for (int y = 0; y < QR_IMAGE_HEIGHT; y++) {
                if (matrix.get(x, y)) {
                    graphics.fillRect(x, y, 1, 1);
                }
            }
        }
        ImageIO.write(image, "png", servletResponse.getOutputStream());
        servletResponse.getOutputStream().flush();
        servletResponse.getOutputStream().close();
    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
    return null;
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) EncodeHintType(com.google.zxing.EncodeHintType) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) BufferedImage(java.awt.image.BufferedImage) Action(org.apache.struts2.convention.annotation.Action)

Example 3 with Action

use of org.apache.struts2.convention.annotation.Action in project KeyBox by skavanagh.

the class SecureShellAction method setPtyType.

@Action(value = "/admin/setPtyType")
public String setPtyType() {
    Long sessionId = AuthUtil.getSessionId(servletRequest.getSession());
    if (SecureShellAction.getUserSchSessionMap() != null) {
        UserSchSessions userSchSessions = SecureShellAction.getUserSchSessionMap().get(sessionId);
        if (userSchSessions != null && userSchSessions.getSchSessionMap() != null) {
            SchSession schSession = userSchSessions.getSchSessionMap().get(id);
            ChannelShell channel = (ChannelShell) schSession.getChannel();
            channel.setPtySize((int) Math.floor(userSettings.getPtyWidth() / 7.2981), (int) Math.floor(userSettings.getPtyHeight() / 14.4166), userSettings.getPtyWidth(), userSettings.getPtyHeight());
            schSession.setChannel(channel);
        }
    }
    return null;
}
Also used : ChannelShell(com.jcraft.jsch.ChannelShell) Action(org.apache.struts2.convention.annotation.Action)

Example 4 with Action

use of org.apache.struts2.convention.annotation.Action in project KeyBox by skavanagh.

the class SecureShellAction method createTerms.

/**
     * creates composite terminals if there are errors or authentication issues.
     */
@Action(value = "/admin/createTerms", results = { @Result(name = "success", location = "/admin/secure_shell.jsp") })
public String createTerms() {
    Long userId = AuthUtil.getUserId(servletRequest.getSession());
    Long sessionId = AuthUtil.getSessionId(servletRequest.getSession());
    if (pendingSystemStatus != null && pendingSystemStatus.getId() != null) {
        //get status
        currentSystemStatus = SystemStatusDB.getSystemStatus(pendingSystemStatus.getId(), userId);
        //if initial status run script
        if (currentSystemStatus != null && (HostSystem.INITIAL_STATUS.equals(currentSystemStatus.getStatusCd()) || HostSystem.AUTH_FAIL_STATUS.equals(currentSystemStatus.getStatusCd()) || HostSystem.PUBLIC_KEY_FAIL_STATUS.equals(currentSystemStatus.getStatusCd()))) {
            //set current session
            currentSystemStatus = SSHUtil.openSSHTermOnSystem(passphrase, password, userId, sessionId, currentSystemStatus, userSchSessionMap);
        }
        if (currentSystemStatus != null && (HostSystem.AUTH_FAIL_STATUS.equals(currentSystemStatus.getStatusCd()) || HostSystem.PUBLIC_KEY_FAIL_STATUS.equals(currentSystemStatus.getStatusCd()))) {
            pendingSystemStatus = currentSystemStatus;
        } else {
            pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
            //if success loop through systems until finished or need password
            while (pendingSystemStatus != null && currentSystemStatus != null && HostSystem.SUCCESS_STATUS.equals(currentSystemStatus.getStatusCd())) {
                currentSystemStatus = SSHUtil.openSSHTermOnSystem(passphrase, password, userId, sessionId, pendingSystemStatus, userSchSessionMap);
                pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
            }
        }
    }
    //set system list if no pending systems
    if (SystemStatusDB.getNextPendingSystem(userId) == null) {
        setSystemList(userId, sessionId);
        //set allocated systems for connect to
        SortedSet sortedSet = new SortedSet();
        sortedSet.setOrderByField(SystemDB.SORT_BY_NAME);
        if (Auth.MANAGER.equals(AuthUtil.getUserType(servletRequest.getSession()))) {
            sortedSet = SystemDB.getSystemSet(sortedSet);
        } else {
            sortedSet = SystemDB.getUserSystemSet(sortedSet, userId);
        }
        if (sortedSet != null && sortedSet.getItemList() != null) {
            allocatedSystemList = (List<HostSystem>) sortedSet.getItemList();
        }
        //set theme
        this.userSettings = UserThemeDB.getTheme(userId);
    }
    return SUCCESS;
}
Also used : SortedSet(com.keybox.manage.model.SortedSet) Action(org.apache.struts2.convention.annotation.Action)

Example 5 with Action

use of org.apache.struts2.convention.annotation.Action in project KeyBox by skavanagh.

the class UploadAndPushAction method push.

@Action(value = "/admin/push", results = { @Result(name = "success", location = "/admin/upload_result.jsp") })
public String push() {
    Long userId = AuthUtil.getUserId(servletRequest.getSession());
    Long sessionId = AuthUtil.getSessionId(servletRequest.getSession());
    try {
        //get next pending system
        pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
        if (pendingSystemStatus != null) {
            //get session for system
            SchSession session = null;
            for (Integer instanceId : SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().keySet()) {
                //if host system id matches pending system then upload
                if (pendingSystemStatus.getId().equals(SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId).getHostSystem().getId())) {
                    session = SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId);
                }
            }
            if (session != null) {
                //push upload to system
                currentSystemStatus = SSHUtil.pushUpload(pendingSystemStatus, session.getSession(), UPLOAD_PATH + "/" + uploadFileName, pushDir + "/" + uploadFileName);
                //update system status
                SystemStatusDB.updateSystemStatus(currentSystemStatus, userId);
                pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
            }
        }
        //if push has finished to all servers then delete uploaded file
        if (pendingSystemStatus == null) {
            File delFile = new File(UPLOAD_PATH, uploadFileName);
            FileUtils.deleteQuietly(delFile);
            //delete all expired files in upload path
            File delDir = new File(UPLOAD_PATH);
            if (delDir.isDirectory()) {
                //set expire time to delete all files older than 48 hrs
                Calendar expireTime = Calendar.getInstance();
                expireTime.add(Calendar.HOUR, -48);
                Iterator<File> filesToDelete = FileUtils.iterateFiles(delDir, new AgeFileFilter(expireTime.getTime()), TrueFileFilter.TRUE);
                while (filesToDelete.hasNext()) {
                    delFile = filesToDelete.next();
                    delFile.delete();
                }
            }
        }
        hostSystemList = SystemStatusDB.getAllSystemStatus(userId);
    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    return SUCCESS;
}
Also used : AgeFileFilter(org.apache.commons.io.filefilter.AgeFileFilter) SchSession(com.keybox.manage.model.SchSession) File(java.io.File) Action(org.apache.struts2.convention.annotation.Action)

Aggregations

Action (org.apache.struts2.convention.annotation.Action)6 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)2 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)2 File (java.io.File)2 org.apache.struts2.config (org.apache.struts2.config)2 EncodeHintType (com.google.zxing.EncodeHintType)1 BitMatrix (com.google.zxing.common.BitMatrix)1 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)1 ChannelShell (com.jcraft.jsch.ChannelShell)1 SchSession (com.keybox.manage.model.SchSession)1 SortedSet (com.keybox.manage.model.SortedSet)1 User (com.keybox.manage.model.User)1 ActionContext (com.opensymphony.xwork2.ActionContext)1 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)1 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)1 BufferedImage (java.awt.image.BufferedImage)1 Annotation (java.lang.annotation.Annotation)1 Hashtable (java.util.Hashtable)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AgeFileFilter (org.apache.commons.io.filefilter.AgeFileFilter)1