Example usage for org.apache.wicket.markup.html.form FormComponent FormComponent

List of usage examples for org.apache.wicket.markup.html.form FormComponent FormComponent

Introduction

In this page you can find the example usage for org.apache.wicket.markup.html.form FormComponent FormComponent.

Prototype

public FormComponent(final String id, IModel<T> model) 

Source Link

Usage

From source file:de.alpharogroup.wicket.components.captcha.recaptcha.ReCaptchaPanel.java

License:Apache License

/**
 * Factory method for creating a new {@link FormComponent} for the recaptcha. This method is
 * invoked in the constructor from the derived classes and can be overridden so users can
 * provide their own version of a {@link FormComponent}.
 *
 * @param id/*  w  w  w  . j  av  a2  s.c om*/
 *            the id
 * @param model
 *            the model
 * @return the new {@link FormComponent} for the recaptcha.
 */
protected FormComponent<Serializable> newCaptchaFormComponent(final String id,
        final IModel<Serializable> model) {
    return new FormComponent<Serializable>(id, model) {

        /** The Constant serialVersionUID. */
        private static final long serialVersionUID = 1L;

        /**
         * {@inheritDoc}
         */
        @Override
        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
            replaceComponentTagBody(markupStream, openTag, newReCaptcha(getPublicKey(), getPrivateKey(), false)
                    .createRecaptchaHtml("errorText", "clean", null));
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void validate() {
            final ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
            reCaptcha.setPrivateKey(getPrivateKey());
            final String challenge = PageParametersExtensions
                    .getParameter(PARAMETER_KEY_RECAPTCHA_CHALLENGE_FIELD);
            String uresponse = PageParametersExtensions.getParameter(PARAMETER_KEY_RECAPTCHA_RESPONSE_FIELD);
            if (uresponse == null) {
                uresponse = "";
            }
            final String remoteAddress = WicketComponentExtensions.getHttpServletRequest().getRemoteAddr();
            final ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddress, challenge,
                    uresponse);

            if (!reCaptchaResponse.isValid()) {
                error(ResourceModelFactory.newResourceModel(
                        ResourceBundleKey.builder().key("kaptcha.invalid.label")
                                .defaultValue("Incorrect answer, type the words from the image again!").build(),
                        this).getObject());
            }
        }
    };
}

From source file:eu.esdihumboldt.hale.server.templates.war.components.RecaptchaPanel.java

License:Open Source License

/**
 * Constructor./*from   w ww . j av a2s.co m*/
 * 
 * @param id the component ID
 */
public RecaptchaPanel(String id) {
    super(id);

    add(new FormComponent<String>("imagePassword", new Model<String>()) {

        private static final long serialVersionUID = 6622368671409426173L;

        @Override
        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
            String privateCaptchaKey = getPrivateKey();
            String publicCaptchaKey = getPublicKey();
            ReCaptcha recaptcha = ReCaptchaFactory.newReCaptcha(publicCaptchaKey, privateCaptchaKey, false);

            Properties properties = new Properties();
            properties.put("theme", "clean");
            replaceComponentTagBody(markupStream, openTag, recaptcha.createRecaptchaHtml(null, properties));
        }

        @Override
        public void validate() {
            WebRequest request = (WebRequest) RequestCycle.get().getRequest();
            HttpServletRequest servletRequest = (HttpServletRequest) getRequest().getContainerRequest();

            // FIXME find proxied address when running behind proxy?
            String remoteAddr = servletRequest.getRemoteAddr();

            String challenge = request.getRequestParameters().getParameterValue("recaptcha_challenge_field")
                    .toString();
            String response = request.getRequestParameters().getParameterValue("recaptcha_response_field")
                    .toString();

            if (response == null || response.isEmpty()) {
                error("Please enter the Captcha or log in to upload a template.");
                return;
            }

            String privateCaptchaKey = getPrivateKey();
            String publicCaptchaKey = getPublicKey();
            ReCaptcha recaptcha = ReCaptchaFactory.newReCaptcha(publicCaptchaKey, privateCaptchaKey, false);
            ReCaptchaResponse reCaptchaResponse = recaptcha.checkAnswer(remoteAddr, challenge, response);

            if (!reCaptchaResponse.isValid()) {
                error("The Captcha was not entered correctly. Please enter the Captcha or log in to upload a template.");
            }
        }
    });
}

From source file:org.apache.syncope.client.console.panels.search.SearchClausePanel.java

License:Apache License

public SearchClausePanel(final String id, final String name, final Model<SearchClause> clause,
        final boolean required, final IModel<List<SearchClause.Type>> types, final IModel<List<String>> anames,
        final IModel<List<String>> dnames, final IModel<Map<String, String>> groupNames,
        final IModel<List<String>> roleNames, final IModel<List<String>> resourceNames) {

    super(id, name, clause);

    this.clause = clause == null ? new Model<SearchClause>(null) : clause;

    this.required = required;
    this.types = types;
    this.anames = anames;
    this.dnames = dnames;
    this.groupNames = groupNames;
    this.roleNames = roleNames;
    this.resourceNames = resourceNames;

    searchButton = new AjaxSubmitLink("search") {

        private static final long serialVersionUID = 5538299138211283825L;

        @Override//from  ww  w  .j a v  a  2s  .  c om
        protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
            if (resultContainer == null) {
                send(this, Broadcast.BUBBLE, new SearchEvent(target));
            } else {
                send(resultContainer, Broadcast.EXACT, new SearchEvent(target));
            }
        }
    };

    searchButtonFragment = new Fragment("operator", "searchButtonFragment", this);
    searchButtonFragment.add(searchButton.setEnabled(false));

    operatorFragment = new Fragment("operator", "operatorFragment", this);

    field = new FormComponent<SearchClause>("container", this.clause) {

        private static final long serialVersionUID = -8204140666393922700L;

    };

    add(field);

    comparators = new LoadableDetachableModel<List<Comparator>>() {

        private static final long serialVersionUID = 5275935387613157437L;

        @Override
        protected List<Comparator> load() {
            if (field.getModel().getObject() == null || field.getModel().getObject().getType() == null) {
                return Collections.<Comparator>emptyList();
            }

            switch (field.getModel().getObject().getType()) {
            case ATTRIBUTE:
                return Arrays.asList(SearchClause.Comparator.values());

            case ROLE_MEMBERSHIP:
            case GROUP_MEMBERSHIP:
            case GROUP_MEMBER:
            case RESOURCE:
                return Arrays.asList(SearchClause.Comparator.EQUALS, SearchClause.Comparator.NOT_EQUALS);

            case RELATIONSHIP:
                return Arrays.asList(SearchClause.Comparator.IS_NOT_NULL, SearchClause.Comparator.IS_NULL,
                        SearchClause.Comparator.EQUALS, SearchClause.Comparator.NOT_EQUALS);
            default:
                return Collections.<Comparator>emptyList();
            }
        }
    };

    properties = new LoadableDetachableModel<List<String>>() {

        private static final long serialVersionUID = 5275935387613157437L;

        @Override
        protected List<String> load() {
            if (field.getModel().getObject() == null || field.getModel().getObject().getType() == null) {
                return Collections.<String>emptyList();
            }

            switch (field.getModel().getObject().getType()) {
            case ATTRIBUTE:
                final List<String> names = new ArrayList<>(dnames.getObject());
                if (anames != null && anames.getObject() != null && !anames.getObject().isEmpty()) {
                    names.addAll(anames.getObject());
                }
                Collections.sort(names);
                return names;

            case GROUP_MEMBERSHIP:
                final List<String> groups = CollectionUtils.collect(groupNames.getObject().keySet(),
                        StringValueTransformer.<String>stringValueTransformer(), new ArrayList<String>());

                Collections.sort(groups);
                return groups;

            case ROLE_MEMBERSHIP:
                final List<String> roles = new ArrayList<>(roleNames.getObject());
                Collections.sort(roles);
                return roles;

            case RESOURCE:
                final List<String> resources = new ArrayList<>(resourceNames.getObject());
                Collections.sort(resources);
                return resources;

            case RELATIONSHIP:
                final List<String> relations = CollectionUtils.collect(
                        SyncopeConsoleSession.get().getService(RelationshipTypeService.class).list(),
                        new Transformer<RelationshipTypeTO, String>() {

                            @Override
                            public String transform(final RelationshipTypeTO input) {
                                return input.getKey();
                            }
                        }, new ArrayList<String>());
                return relations;

            default:
                return Collections.<String>emptyList();
            }
        }
    };
}