Example usage for org.springframework.web.context.request ServletRequestAttributes removeAttribute

List of usage examples for org.springframework.web.context.request ServletRequestAttributes removeAttribute

Introduction

In this page you can find the example usage for org.springframework.web.context.request ServletRequestAttributes removeAttribute.

Prototype

@Override
    public void removeAttribute(String name, int scope) 

Source Link

Usage

From source file:org.bibsonomy.webapp.controller.actions.DeliciousImportController.java

protected String createRedirect(SettingsViewCommand command, RequestWrapperContext context, Errors errors) {

    final DeliciousSignPost oAuth = signPostManager.createDeliciousSignPost();
    final ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes();
    attr.setAttribute(signPostManager.getoAuthKey(), oAuth, ServletRequestAttributes.SCOPE_SESSION);

    try {// w w w  . ja  v a 2 s. co  m
        return oAuth.getRequestToken(
                signPostManager.getCallbackBaseUrl() + "?" + "ckey=" + context.getCkey() + "&" + "overwrite="
                        + command.isOverwriteV2() + "&" + "importData=" + command.getImportDataV2());
    } catch (Exception ex) {
        attr.removeAttribute(signPostManager.getoAuthKey(), ServletRequestAttributes.SCOPE_SESSION);
        errors.reject("error.furtherInformations", new Object[] { ex.getMessage() },
                "The following error occurred: {0}");
        log.warn("Delicious-Import failed: " + ex.getMessage());
    }

    return null;
}

From source file:org.bibsonomy.webapp.controller.actions.ImportBookmarksController.java

@Override
public View workOn(final ImportCommand command) {
    final RequestWrapperContext context = command.getContext();

    /*/*from   w  w w .ja  v a2  s  .co  m*/
     * only users which are logged in might post -> send them to
     * login page
     */
    if (!context.isUserLoggedIn()) {
        throw new AccessDeniedException("please log in");
    }

    final User loginUser = context.getLoginUser();

    /*
     * check credentials to fight CSRF attacks 
     * 
     */
    if (!context.isValidCkey()) {
        errors.reject("error.field.valid.ckey");
        /*
         * FIXME: correct URL?
         * FIXME: don't do this on first call of form!
         */
        return Views.IMPORT;
    }

    if (errors.hasErrors()) {
        return Views.IMPORT;
    }

    List<Post<Bookmark>> posts = new LinkedList<Post<Bookmark>>();
    List<Tag> relations = new LinkedList<Tag>();

    final String importType = command.getImportType();
    try {
        if ("delicious".equals(importType)) {
            /*
             * TODO: we want to have checkboxes, not radio buttons!
             */
            final String importData = command.getImportData();
            /*
             * import posts/bundles from Delicious
             */
            if ("posts".equals(importData)) {
                final RemoteServiceBookmarkImporter importer = importerFactory.getBookmarkImporter();
                importer.setCredentials(command.getUserName(), command.getPassWord());
                posts = importer.getPosts();
            }
            if ("bundles".equals(importData)) {
                final RelationImporter relationImporter = importerFactory.getRelationImporter();
                relationImporter.setCredentials(command.getUserName(), command.getPassWord());
                relations = relationImporter.getRelations();
            }

        } else if ("delicious.yahoo".equals(importType)) {
            /*
             * TODO: we want to have checkboxes, not radio buttons!
             */
            final String importData = command.getImportData();
            final ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder
                    .currentRequestAttributes();
            final DeliciousSignPost oAuth = (DeliciousSignPost) attr.getAttribute(signPostManager.getoAuthKey(),
                    ServletRequestAttributes.SCOPE_SESSION);
            attr.removeAttribute(signPostManager.getoAuthKey(), ServletRequestAttributes.SCOPE_SESSION);
            oAuth.getAccessToken(command.getOauth_verifier());
            /*
             * import posts/bundles from Delicious
             */
            if ("posts".equals(importData)) {
                posts = DeliciousV2Importer.getPosts(oAuth.sign(new URL(signPostManager.getBookmarksUrl())));
            }
            if ("bundles".equals(importData)) {
                relations = DeliciousV2Importer
                        .getRelations(oAuth.sign(new URL(signPostManager.getBundlesUrl())));
            }
        } else if ("firefox".equals(importType)) {
            /*
             * import posts/relations from Firefox
             */
            final FileUploadInterface uploadFileHandler = this.uploadFactory.getFileUploadHandler(
                    Collections.singletonList(command.getFile().getFileItem()),
                    FileUploadInterface.firefoxImportExt);
            final Document document = uploadFileHandler.writeUploadedFile();
            /*
             * FileBookmarkImporter interface
             */
            final FileBookmarkImporter fileImporter = new FirefoxImporter();
            fileImporter.initialize(document.getFile(), loginUser, command.getGroup());
            posts = fileImporter.getPosts();
            /*
             * clear temporary file
             */
            document.getFile().delete();
        } else {
            log.info("unknown import type '" + importType + "'");
        }
        /*
         * FIXME: too general error keys!
         */
    } catch (final UnsupportedFileTypeException ex) {
        errors.reject("error.furtherInformations", new Object[] { ex.getMessage() },
                "The following error occurred: {0}");
    } catch (final Exception ex) {
        errors.reject("error.furtherInformations", new Object[] { ex.getMessage() },
                "The following error occurred: {0}");
        log.warn("Delicious/Firefox-Import failed: " + ex.getMessage());
    }

    /** store the posts **/
    if (present(posts)) {
        this.storePosts(command, posts);

        /** how many posts were found? **/
        command.setTotalCount(posts.size());
    }

    /** if available store relations **/
    if (present(relations)) {
        this.storeRelations(relations, command);

        /** how many bundles were found? **/
        command.setTotalCount(relations.size());
    }

    return Views.IMPORT;
}