Example #1
0
  /**
   * Method for finding the error handler for the given connector. Uses connector hierarchy to find
   * a connector with an error handler. Falls back to the VaadinSession error handler if no
   * connector has specified an error handler.
   *
   * <p>Returns a {@link DefaultErrorHandler} if no error handler was found
   *
   * @param connector The target connector
   * @return An ErrorHandler for the connector
   */
  public static ErrorHandler findErrorHandler(ClientConnector connector) {
    if (connector != null) {
      ErrorHandler errorHandler = connector.getErrorHandler();
      if (errorHandler != null) {
        return errorHandler;
      }

      ClientConnector parent = connector.getParent();
      if (parent != null) {
        return findErrorHandler(parent);
      }

      /*
       * Reached UI and found no error handler. Try session which
       * typically has one.
       */
      UI ui = connector.getUI();
      if (ui != null) {
        errorHandler = findErrorHandler(ui.getSession());
        if (errorHandler != null) {
          return errorHandler;
        }
      }
    }

    /*
     * No connector known or the connector is not attached to a session. Try
     * the current session
     */
    if (VaadinSession.getCurrent() != null) {
      ErrorHandler errorHandler = VaadinSession.getCurrent().getErrorHandler();
      if (errorHandler != null) {
        return errorHandler;
      }
    }

    /*
     * We should never really get here as at least the session should have
     * an error handler. If for some reason it does not we use the default
     * error handler.
     */
    return new DefaultErrorHandler();
  }
  /**
   * Adds a new browser level window to this application. Please note that UI doesn't have a name
   * that is used in the URL - to add a named window you should instead use {@link #addWindow(UI,
   * String)}
   *
   * @param uI the UI window to add to the application
   * @return returns the name that has been assigned to the window
   * @see #addWindow(UI, String)
   */
  public void addWindow(LegacyWindow uI) {
    if (uI.getName() == null) {
      String name = Integer.toString(namelessUIIndex++);
      uI.setName(name);
    }

    uI.setApplication(this);

    legacyUINames.put(uI.getName(), uI);
    uI.setSession(VaadinSession.getCurrent());
  }
 private void saveUser(String oauthId, String name, String email, String picUrl) {
   User user =
       UserBean.builder()
           .oauthIdentifier(oauthId)
           .firstName(name)
           .picUrl(picUrl)
           .lastName("")
           .email(email)
           .role("user")
           .build();
   try {
     UserVertex userVertex = (UserVertex) new UserVertex(user).saveOrUpdateInNewTX();
     user = userVertex.model();
     VaadinSession.getCurrent().setAttribute(User.class.getName(), user);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
 public void doInit(URL url) {
   this.url = url;
   VaadinSession.getCurrent().setErrorHandler(this);
   init();
 }
 public VaadinSession getContext() {
   return VaadinSession.getCurrent();
 }
  @Override
  public boolean handleRequest(
      VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {

    if (data.isCallbackForMe(request)) {

      String verifier = request.getParameter(data.getVerifierParameterName());
      if (verifier != null) {
        // Got verifier!
        data.setVerifier(requestToken, new Verifier(verifier));
        finish(session, response);

        Token t = data.getAccessToken();

        OAuthRequest r = new OAuthRequest(Verb.GET, data.getRequestLink());
        data.signRequest(t, r);
        Response resp = r.send();

        FacebookAnswer answer = new Gson().fromJson(resp.getBody(), FacebookAnswer.class);

        String name = answer.name;
        String picUrl = answer.picture.data.url;
        String oauthId = "facebook" + answer.id;
        saveUser(oauthId, name, "", picUrl);

        VaadinSession.getCurrent().removeRequestHandler(this);
        ((VaadinServletResponse) response)
            .getHttpServletResponse()
            .sendRedirect(data.getRedirectUrl());

        return true;
      }

      // No verifier in the parameters. That's most likely because the user
      // denied the OAuth.

      // TODO: current error message reporting (below) is not very useful

      String error = null;
      for (String errorName : data.getErrorParameterNames()) {
        error = request.getParameter(errorName);
        if (error != null) {
          break;
        }
      }

      String errorMessage;
      if (error == null) {
        errorMessage = "OAuth failed.";
      } else {
        errorMessage = "OAuth denied: " + error;
      }

      data.setDenied(errorMessage);
      finish(session, response);
    } else if (request.getParameter("code") != null) {
      String code = request.getParameter("code");
      Verifier v = new Verifier(code);
      Token t = googleService.getAccessToken(null, v);

      OAuthRequest r = new OAuthRequest(Verb.GET, "https://www.googleapis.com/plus/v1/people/me");
      googleService.signRequest(t, r);
      Response resp = r.send();

      GooglePlusAnswer answer = new Gson().fromJson(resp.getBody(), GooglePlusAnswer.class);

      String name =
          (answer.displayName != null && !answer.displayName.equals(""))
              ? answer.displayName
              : answer.emails[0].value.substring(0, answer.emails[0].value.indexOf("@"));
      String picUrl = answer.image.url;
      String oauthId = "google" + answer.id;
      saveUser(oauthId, name, answer.emails[0].value, picUrl);

      VaadinSession.getCurrent().removeRequestHandler(this);
      ((VaadinServletResponse) response)
          .getHttpServletResponse()
          .sendRedirect(data.getRedirectUrl());
      return true;
    }
    return false;
  }