All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.knightsoftnet.validators.client.rest.helper.AbstractRestCallback Maven / Gradle / Ivy

Go to download

The GWT Bean Validators is a collection of JSR-303/JSR-349/JSR 380 bean validators. It can be used on server and with the help of GWT even on client side. This packages contains rest services and validator for phone numbers based on gwtp-rest dispatcher and spring to reduce client side code.

There is a newer version: 2.3.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package de.knightsoftnet.validators.client.rest.helper;

import de.knightsoftnet.navigation.client.session.Session;
import de.knightsoftnet.validators.client.converter.ValidationResultDataConverter;
import de.knightsoftnet.validators.client.factory.ValidationFactory;
import de.knightsoftnet.validators.shared.data.ValidationResultInterface;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Response;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.gwtplatform.dispatch.rest.client.RestCallback;
import com.gwtplatform.dispatch.shared.ActionException;

/**
 * Async callback implementation with error handling.
 *
 * @author Manfred Tremmel
 *
 */
public abstract class AbstractRestCallback, R>
    implements RestCallback {

  protected final V view;
  protected final D data;
  protected final Session session;
  private final ValidationResultDataConverter validationConverter;
  private Response response;

  /**
   * constructor.
   *
   * @param pview view
   * @param pdata date to handle
   * @param psession session data
   */
  public AbstractRestCallback(final V pview, final D pdata, final Session psession) {
    super();
    this.view = pview;
    this.data = pdata;
    this.session = psession;
    this.validationConverter = new ValidationResultDataConverter<>();
  }

  @Override
  public void onFailure(final Throwable pcaught) {
    try {
      throw pcaught;
    } catch (final ActionException e) {
      switch (this.response.getStatusCode()) {
        case 400:
          final ValidationFactory validationFactory = GWT.create(ValidationFactory.class);
          final AutoBean validationResultWraper = AutoBeanCodex
              .decode(validationFactory, ValidationResultInterface.class, this.response.getText());
          this.view.setConstraintViolations(
              this.validationConverter.convert(validationResultWraper.as(), this.data));
          break;
        case 401:
          this.session.readSessionData();
          break;
        default:
          this.view.showMessage(e.getMessage());
          break;
      }
    } catch (final Throwable e) {
      this.view.showMessage(e.getMessage());
    }
  }

  @Override
  public void setResponse(final Response presponse) {
    this.response = presponse;
  }

  @Override
  public abstract void onSuccess(final R presult);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy