Example usage for org.apache.shiro SecurityUtils getSubject

List of usage examples for org.apache.shiro SecurityUtils getSubject

Introduction

In this page you can find the example usage for org.apache.shiro SecurityUtils getSubject.

Prototype

public static Subject getSubject() 

Source Link

Document

Returns the currently accessible Subject available to the calling code depending on runtime environment.

Usage

From source file:au.org.theark.core.web.component.customfieldcategory.form.SearchForm.java

License:Open Source License

/**
 * @param id/*from   w w  w .j  av a 2s  .  c  o  m*/
 */
public SearchForm(String id, CompoundPropertyModel<CustomFieldCategoryVO> cpModel, FeedbackPanel feedBackPanel,
        ArkCrudContainerVO arkCrudContainerVO) {

    super(id, cpModel, feedBackPanel, arkCrudContainerVO);
    this.cpModel = cpModel;
    this.feedbackPanel = feedBackPanel;
    this.arkCrudContainerVO = arkCrudContainerVO;
    initialiseFieldForm();
    Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    disableSearchForm(sessionStudyId, "There is no study selected. Please select a study.");
}

From source file:au.org.theark.core.web.component.customfieldcategory.form.SearchForm.java

License:Open Source License

/**
 * initialize Custom Filed Types./*from ww w.  j  a  v a2s  .co  m*/
 */
private void initCustomFieldTypeDdc() {
    Long sessionModuleId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.ARK_MODULE_KEY);
    ArkModule arkModule = iArkCommonService.getArkModuleById(sessionModuleId);
    java.util.Collection<CustomFieldType> customFieldTypeCollection = iArkCommonService
            .getCustomFieldTypes(arkModule);
    ;
    ChoiceRenderer customfieldTypeRenderer = new ChoiceRenderer(Constants.CUSTOM_FIELD_TYPE_NAME,
            Constants.CUSTOM_FIELD_TYPE_ID);
    customFieldTypeDdc = new DropDownChoice<CustomFieldType>(
            Constants.FIELDVO_CUSTOMFIELDCATEGORY_CUSTOM_FIELD_TYPE, (List) customFieldTypeCollection,
            customfieldTypeRenderer);
    customFieldTypeDdc.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            cpModel.getObject().getCustomFieldCategory()
                    .setCustomFieldType(customFieldTypeDdc.getModelObject());
            Collection<CustomFieldCategory> customFieldCategoryCollection = getParentCategoryCollectionFromModel();
            categoryPanel.remove(parentCategoryDdc);
            ChoiceRenderer customfieldCategoryRenderer = new ChoiceRenderer(Constants.CUSTOMFIELDCATEGORY_NAME,
                    Constants.CUSTOMFIELDCATEGORY_ID);
            parentCategoryDdc = new DropDownChoice<CustomFieldCategory>(
                    Constants.FIELDVO_CUSTOMFIELDCATEGORY_PARENTCATEGORY, (List) customFieldCategoryCollection,
                    customfieldCategoryRenderer);
            parentCategoryDdc.setOutputMarkupId(true);
            categoryPanel.add(parentCategoryDdc);
            target.add(parentCategoryDdc);
            target.add(categoryPanel);
        }
    });

}

From source file:au.org.theark.core.web.component.customfieldcategory.form.SearchForm.java

License:Open Source License

@Override
protected void onSearch(AjaxRequestTarget target) {
    target.add(feedbackPanel);/*  w ww.java 2 s .c om*/
    final Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    // Get a list of all Fields for the Study in context
    Study study = iArkCommonService.getStudy(sessionStudyId);
    getModelObject().getCustomFieldCategory().setStudy(study);

    long count = iArkCommonService.getCustomFieldCategoryCount(getModelObject().getCustomFieldCategory());
    if (count <= 0L) {
        this.info("No records match the specified search criteria.");
        target.add(feedbackPanel);
    }

    arkCrudContainerVO.getSearchResultPanelContainer().setVisible(true);// Make the WebMarkupContainer that houses the search results visible
    target.add(arkCrudContainerVO.getSearchResultPanelContainer());
}

From source file:au.org.theark.core.web.component.customfieldupload.CustomFieldUploadContainerPanel.java

License:Open Source License

protected WebMarkupContainer initialiseSearchResults() {
    searchResultListPanel = new SearchResultListPanel("searchResults");

    iModel = new LoadableDetachableModel<Object>() {
        private static final long serialVersionUID = 1L;

        @Override/*from www. ja  v a 2s .c  o m*/
        protected Object load() {
            // Return all Uploads for the study in context
            Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
                    .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);

            listView.removeAll();

            if (sessionStudyId != null) {
                Upload searchUpload = new Upload();
                Study study = iArkCommonService.getStudy(sessionStudyId);
                searchUpload.setStudy(study);
                searchUpload.setArkFunction(containerForm.getModelObject().getUpload().getArkFunction());

                java.util.Collection<Upload> studyUploads = iArkCommonService.searchUploads(searchUpload);
                return studyUploads;
            } else {
                return null;
            }
        }
    };

    listView = searchResultListPanel.buildPageableListView(iModel);
    listView.setReuseItems(true);
    PagingNavigator pageNavigator = new PagingNavigator("navigator", listView);
    searchResultListPanel.add(pageNavigator);
    searchResultListPanel.add(listView);
    arkCrudContainerVO.getSearchResultPanelContainer().add(searchResultListPanel);
    searchResultListPanel.setVisible(true);

    return arkCrudContainerVO.getSearchResultPanelContainer();
}

From source file:au.org.theark.core.web.component.customfieldupload.CustomFieldUploadStep1.java

License:Open Source License

private void saveFileInMemory() {
    // Set study in context
    Long studyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = iArkCommonService.getStudy(studyId);

    // Retrieve file and store as Blob in database
    // TODO: AJAX-ified and asynchronous and hit database
    FileUpload fileUpload = fileUploadField.getFileUpload();
    containerForm.getModelObject().setFileUpload(fileUpload);

    InputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    File temp = null;/*from w w w .j a va2s  .  c  om*/
    try {
        // Copy all the contents of the file upload to a temp file (to read multiple times)...
        inputStream = containerForm.getModelObject().getFileUpload().getInputStream();
        // Create temp file
        temp = File.createTempFile("customFieldUploadBlob", ".tmp");
        containerForm.getModelObject().setTempFile(temp);
        // Delete temp file when program exits (just in case manual delete fails later)
        temp.deleteOnExit();
        // Write to temp file
        outputStream = new BufferedOutputStream(new FileOutputStream(temp));
        IOUtils.copy(inputStream, outputStream);
    } catch (IOException ioe) {
        log.error("IOException " + ioe.getMessage());
        // Something failed, so there is temp file is not valid (step 2 should check for null)
        temp.delete();
        temp = null;
        containerForm.getModelObject().setTempFile(temp);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.error("Unable to close inputStream: " + e.getMessage());
            }
        }
        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                log.error("Unable to close outputStream: " + e.getMessage());
            }
        }
    }

    // Set details of Upload object
    containerForm.getModelObject().getUpload().setStudy(study);
    String filename = containerForm.getModelObject().getFileUpload().getClientFileName();
    String fileFormatName = filename.substring(filename.lastIndexOf('.') + 1).toUpperCase();
    FileFormat fileFormat = new FileFormat();
    fileFormat = iArkCommonService.getFileFormatByName(fileFormatName);
    containerForm.getModelObject().getUpload().setFileFormat(fileFormat);

    byte[] byteArray = fileUpload.getMD5();
    String checksum = getHex(byteArray);
    containerForm.getModelObject().getUpload().setChecksum(checksum);
    containerForm.getModelObject().getUpload().setFilename(fileUpload.getClientFileName());
    wizardForm.setFileName(fileUpload.getClientFileName());
}

From source file:au.org.theark.core.web.component.customfieldupload.CustomFieldUploadStep4.java

License:Open Source License

@Override
public void onStepOutNext(AbstractWizardForm<?> form, AjaxRequestTarget target) {
    // Filename seems to be lost from model when moving between steps in wizard
    containerForm.getModelObject().getUpload().setFilename(wizardForm.getFileName());

    // Perform actual upload of data
    containerForm.getModelObject().getUpload().setStartTime(new Date(System.currentTimeMillis()));
    StringBuffer uploadReport = null;
    String filename = containerForm.getModelObject().getFileUpload().getClientFileName();
    String fileFormat = filename.substring(filename.lastIndexOf('.') + 1).toUpperCase();
    FileFormat fileFormatObj;/*w w  w . ja v a  2  s.  c  o m*/
    fileFormatObj = iArkCommonService.getFileFormatByName(fileFormat);
    containerForm.getModelObject().getUpload().setFileFormat(fileFormatObj);

    char delimiterChar = containerForm.getModelObject().getUpload().getDelimiterType().getDelimiterCharacter();

    Subject currentUser = SecurityUtils.getSubject();
    Long studyId = (Long) currentUser.getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = iArkCommonService.getStudy(studyId);
    Long sessionModuleId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.ARK_MODULE_KEY);
    ArkModule arkModule = iArkCommonService.getArkModuleById(sessionModuleId);
    //We have to decide the custom filed/category goes under which function to the DB.
    //At the moment if it is subject or the lims
    //Knowing the Module and the current fuction we can dicide that
    ArkFunction currentFunction = containerForm.getModelObject().getUpload().getArkFunction();
    UploadLevel uploadLevel = containerForm.getModelObject().getUpload().getUploadLevel();
    ArkFunction adjustedArkFunctionForCustomField = null;

    //Common custom field update split to study in here.
    if (arkModule.getName().equals(au.org.theark.core.Constants.ARK_MODULE_STUDY) && currentFunction.getName()
            .equals(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_SUBJECT_CUSTOM_FIELD_UPLOAD)) {
        // Field upload
        if (uploadLevel.getName().equalsIgnoreCase(
                au.org.theark.core.web.component.customfieldupload.Constants.UPLOAD_LEVEL_FIELD)) {
            adjustedArkFunctionForCustomField = iArkCommonService
                    .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_SUBJECT_CUSTOM_FIELD);
            iCustomImporter = new CustomFieldImporter(study, adjustedArkFunctionForCustomField,
                    iArkCommonService, fileFormat, delimiterChar);
            //Category upload      
        } else if (uploadLevel.getName().equalsIgnoreCase(
                au.org.theark.core.web.component.customfieldupload.Constants.UPLOAD_LEVEL_CATEGORY)) {
            adjustedArkFunctionForCustomField = iArkCommonService.getArkFunctionByName(
                    au.org.theark.core.Constants.FUNCTION_KEY_VALUE_SUBJECT_CUSTOM_FIELD_CATEGORY);
            iCustomImporter = new CustomFieldCategoryImporter(study, adjustedArkFunctionForCustomField,
                    iArkCommonService, fileFormat, delimiterChar);
        }
    }

    //Common custom field update split to lims in here.
    if (arkModule.getName().equals(au.org.theark.core.Constants.ARK_MODULE_LIMS) && currentFunction.getName()
            .equals(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_LIMS_CUSTOM_FIELD_UPLOAD)) {
        // Field upload
        if (uploadLevel.getName().equalsIgnoreCase(
                au.org.theark.core.web.component.customfieldupload.Constants.UPLOAD_LEVEL_FIELD)) {
            adjustedArkFunctionForCustomField = iArkCommonService
                    .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_LIMS_CUSTOM_FIELD);
            iCustomImporter = new CustomFieldImporter(study, adjustedArkFunctionForCustomField,
                    iArkCommonService, fileFormat, delimiterChar);
            //Category upload      
        } else if (uploadLevel.getName().equalsIgnoreCase(
                au.org.theark.core.web.component.customfieldupload.Constants.UPLOAD_LEVEL_CATEGORY)) {
            adjustedArkFunctionForCustomField = iArkCommonService.getArkFunctionByName(
                    au.org.theark.core.Constants.FUNCTION_KEY_VALUE_LIMS_CUSTOM_FIELD_CATEGORY);
            iCustomImporter = new CustomFieldCategoryImporter(study, adjustedArkFunctionForCustomField,
                    iArkCommonService, fileFormat, delimiterChar);
        }
    }
    //Need to persist the custom field category

    try {
        log.info("Uploading data dictionary file");
        InputStream inputStream = containerForm.getModelObject().getFileUpload().getInputStream();

        if (fileFormat.equalsIgnoreCase("XLS")) {
            Workbook w;
            try {
                w = Workbook.getWorkbook(inputStream);
                inputStream = iCustomImporter.convertXlsToCsv(w);
                inputStream.reset();
            } catch (BiffException e) {
                log.error(e.getMessage());
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }

        uploadReport = iCustomImporter.uploadAndReportMatrixDataDictionaryFile(inputStream,
                containerForm.getModelObject().getFileUpload().getSize());

        // Determined FieldUpload entities
        if (iCustomImporter instanceof CustomFieldCategoryImporter)
            containerForm.getModelObject().setCustomFieldUploadCategoryCollection(
                    ((CustomFieldCategoryImporter) iCustomImporter).getFieldUploadList());
        else if (iCustomImporter instanceof CustomFieldImporter) {
            containerForm.getModelObject().setCustomFieldUploadCollection(
                    ((CustomFieldImporter) iCustomImporter).getFieldUploadList());
        }
    } catch (FileFormatException ffe) {
        log.error(Constants.FILE_FORMAT_EXCEPTION + ffe);
    } catch (IOException ioe) {
        log.error(ioe.getMessage());
    } catch (ArkSystemException ase) {
        log.error(ase.getMessage());
    }

    // Update the report
    if (uploadReport != null) {
        updateUploadReport(uploadReport.toString());
    }
    // Save all objects to the database
    save(iCustomImporter);
}

From source file:au.org.theark.core.web.component.customfieldupload.form.SearchForm.java

License:Open Source License

/**
 * //from w w w  .ja v a2s .  co  m
 * @param id
 * @param model
 * @param arkCrudContainerVO
 * @param feedBackPanel
 * @param listView
 */
public SearchForm(String id, CompoundPropertyModel<CustomFieldUploadVO> model,
        ArkCrudContainerVO arkCrudContainerVO, FeedbackPanel feedBackPanel, PageableListView<Upload> listView) {

    super(id, model, feedBackPanel, arkCrudContainerVO);

    this.cpmModel = model;
    this.listView = listView;

    initialiseFieldForm();
    Long sessionStudyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    disableSearchForm(sessionStudyId, "There is no study selected. Please select a study.");
}

From source file:au.org.theark.core.web.component.customfieldupload.form.SearchForm.java

License:Open Source License

@Override
protected void onSearch(AjaxRequestTarget target) {
    target.add(feedbackPanel);//from   ww w .jav  a  2 s.  co  m

    // Set study in context
    Long studyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    // Get a list of all Fields for the Study in context
    Study study = iArkCommonService.getStudy(studyId);

    Upload searchUpload = getModelObject().getUpload();
    searchUpload.setStudy(study);
    searchUpload.setArkFunction(
            iArkCommonService.getArkFunctionByName(Constants.FUNCTION_KEY_VALUE_DATA_DICTIONARY));

    Collection<Upload> uploadCollection = iArkCommonService.searchUploads(searchUpload);

    if (uploadCollection != null && uploadCollection.size() == 0) {
        this.info("Uploads with the specified search criteria do not exist in the system.");
        target.add(feedbackPanel);
    }

    getModelObject().setUploadCollection(uploadCollection);

    listView.removeAll();
    arkCrudContainerVO.getSearchResultPanelContainer().setVisible(true);
    target.add(arkCrudContainerVO.getSearchResultPanelContainer());
}

From source file:au.org.theark.core.web.component.customfieldupload.SearchResultListPanel.java

License:Open Source License

/**
 * //  ww w . j a v  a  2 s  .c  om
 * @param id
 */
public SearchResultListPanel(String id) {
    super(id);
    this.setOutputMarkupId(true);
    me = this;
    Long sessionModuleId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.ARK_MODULE_KEY);
    ArkModule arkModule = iArkCommonService.getArkModuleById(sessionModuleId);

    //Refer ARK-1386 programmatically pick the header details according to the module.     
    ArkDownloadTemplateButton downloadFieldTemplateButton = new ArkDownloadTemplateButton(
            "downloadTemplateField",
            (arkModule.getName().equals(au.org.theark.core.Constants.ARK_MODULE_LIMS)) ? "LIMSCustomFieldUpload"
                    : "SubjectCustomFieldUpload",
            changeCustomFieldHeaderWithModuleName(arkModule,
                    au.org.theark.core.Constants.CUSTOM_FIELD_UPLOAD_HEADER)) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            this.error(
                    "An unexpected error occurred. The download custom field template request could not be processed.");
        }
    };
    ArkDownloadTemplateButton downloadCategoryTemplateButton = new ArkDownloadTemplateButton(
            "downloadTemplateCategory", "CustomFieldCategoryUpload",
            au.org.theark.core.Constants.CUSTOM_FIELD_CATEGORY_UPLOAD_HEADER) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            this.error(
                    "An unexpected error occurred. The download custom field category request could not be processed.");
        }
    };
    initConfirmModel();
    add(downloadFieldTemplateButton);
    add(downloadCategoryTemplateButton);
}

From source file:au.org.theark.core.web.component.menu.AbstractArkTabPanel.java

License:Open Source License

/**
 * Clear the current user's session principals
 * /* w w  w .  j a va2  s. co m*/
 * @param arkModuleName
 * @param arkFunction
 */
public void processAuthorizationCache(String arkModuleName, ArkFunction arkFunction) {
    arkModule = iArkCommonService.getArkModuleByName(arkModuleName); // Place a default module into session
    SecurityUtils.getSubject().getSession().setAttribute(au.org.theark.core.Constants.ARK_FUNCTION_KEY,
            arkFunction.getId());
    SecurityUtils.getSubject().getSession().setAttribute(au.org.theark.core.Constants.ARK_MODULE_KEY,
            arkModule.getId());
    Subject currentUser = SecurityUtils.getSubject();
    arkLdapRealm.clearCachedAuthorizationInfo(currentUser.getPrincipals());
    aafRealm.clearCachedAuthorizationInfo(currentUser.getPrincipals());
}