protected OAuthToken getOAuthToken(
      SecurityToken securityToken, String serviceName, String tokenName) throws GadgetException {

    long userId = GetterUtil.getLong(securityToken.getViewerId());

    User user = null;

    try {
      user = UserLocalServiceUtil.getUser(userId);
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }

    Gadget gadget = null;

    try {
      gadget = GadgetLocalServiceUtil.getGadget(user.getCompanyId(), securityToken.getAppUrl());
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }

    OAuthToken oAuthToken = null;

    try {
      oAuthToken =
          OAuthTokenLocalServiceUtil.getOAuthToken(
              userId, gadget.getGadgetId(), serviceName, securityToken.getModuleId(), tokenName);
    } catch (Exception e) {
    }

    return oAuthToken;
  }
  protected OAuthConsumer getOAuthConsumer(SecurityToken securityToken, String serviceName)
      throws GadgetException {

    long userId = GetterUtil.getLong(securityToken.getViewerId());

    User user = null;

    try {
      user = UserLocalServiceUtil.getUser(userId);
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }

    Gadget gadget = null;

    try {
      gadget = GadgetLocalServiceUtil.getGadget(user.getCompanyId(), securityToken.getAppUrl());
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }

    OAuthConsumer oAuthConsumer = null;

    try {
      oAuthConsumer =
          OAuthConsumerLocalServiceUtil.getOAuthConsumer(gadget.getGadgetId(), serviceName);
    } catch (Exception e) {
      return _oAuthConsumer;
    }

    if (oAuthConsumer.getKeyType().equals(OAuthConsumerConstants.KEY_TYPE_RSA_PRIVATE)) {

      if (_oAuthConsumer == null) {
        throw new GadgetException(
            GadgetException.Code.INTERNAL_SERVER_ERROR, "No OAuth key specified");
      }

      oAuthConsumer.setConsumerSecret(_oAuthConsumer.getConsumerSecret());
    }

    return oAuthConsumer;
  }
  public void setTokenInfo(
      SecurityToken securityToken,
      ConsumerInfo consumerInfo,
      String serviceName,
      String tokenName,
      TokenInfo tokenInfo)
      throws GadgetException {

    long userId = GetterUtil.getLong(securityToken.getViewerId());

    User user = null;

    try {
      user = UserLocalServiceUtil.getUser(userId);
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }

    Gadget gadget = null;

    try {
      gadget = GadgetLocalServiceUtil.getGadget(user.getCompanyId(), securityToken.getAppUrl());
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }

    try {
      OAuthTokenLocalServiceUtil.addOAuthToken(
          userId,
          gadget.getGadgetId(),
          serviceName,
          securityToken.getModuleId(),
          tokenInfo.getAccessToken(),
          tokenName,
          tokenInfo.getTokenSecret(),
          tokenInfo.getSessionHandle(),
          tokenInfo.getTokenExpireMillis());
    } catch (Exception e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
    }
  }