Example #1
0
  @Override
  public String getSchemaJSON() {
    if (!changed && schemaJSON != null) {
      return schemaJSON;
    }

    if (schemaKeys == null) {
      schema.remove(Schema.FIELD_SCHEMA_KEYS);
    } else {
      try {
        schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys));
      } catch (JSONException ex) {
        throw new RuntimeException(ex);
      }
    }

    schemaJSON = schema.toString();
    return schemaJSON;
  }
Example #2
0
  /**
   * Executes the call to the REST Service and processes the response.
   *
   * @return The service response.
   */
  public UserInfoResponse exec() {
    // Prepare request parameters
    initClientRequest();
    clientRequest.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    clientRequest.setHttpMethod(getHttpMethod());

    if (getRequest().getAuthorizationMethod() == null
        || getRequest().getAuthorizationMethod()
            == AuthorizationMethod.AUTHORIZATION_REQUEST_HEADER_FIELD) {
      if (StringUtils.isNotBlank(getRequest().getAccessToken())) {
        clientRequest.header("Authorization", "Bearer " + getRequest().getAccessToken());
      }
    } else if (getRequest().getAuthorizationMethod()
        == AuthorizationMethod.FORM_ENCODED_BODY_PARAMETER) {
      if (StringUtils.isNotBlank(getRequest().getAccessToken())) {
        clientRequest.formParameter("access_token", getRequest().getAccessToken());
      }
    } else if (getRequest().getAuthorizationMethod() == AuthorizationMethod.URL_QUERY_PARAMETER) {
      if (StringUtils.isNotBlank(getRequest().getAccessToken())) {
        clientRequest.queryParameter("access_token", getRequest().getAccessToken());
      }
    }

    // Call REST Service and handle response
    try {
      if (getRequest().getAuthorizationMethod() == null
          || getRequest().getAuthorizationMethod()
              == AuthorizationMethod.AUTHORIZATION_REQUEST_HEADER_FIELD
          || getRequest().getAuthorizationMethod() == AuthorizationMethod.URL_QUERY_PARAMETER) {
        clientResponse = clientRequest.get(String.class);
      } else if (getRequest().getAuthorizationMethod()
          == AuthorizationMethod.FORM_ENCODED_BODY_PARAMETER) {
        clientResponse = clientRequest.post(String.class);
      }

      int status = clientResponse.getStatus();

      setResponse(new UserInfoResponse(status));

      String entity = clientResponse.getEntity(String.class);
      getResponse().setEntity(entity);
      getResponse().setHeaders(clientResponse.getHeaders());
      if (StringUtils.isNotBlank(entity)) {
        List<String> contentType = clientResponse.getHeaders().get("Content-Type");
        if (contentType != null && contentType.contains("application/jwt")) {
          String[] jwtParts = entity.split("\\.");
          if (jwtParts.length == 5) {
            byte[] sharedSymmetricKey =
                sharedKey != null ? sharedKey.getBytes(Util.UTF8_STRING_ENCODING) : null;
            Jwe jwe = Jwe.parse(entity, rsaPrivateKey, sharedSymmetricKey);
            getResponse().setClaims(jwe.getClaims().toMap());
          } else {
            Jwt jwt = Jwt.parse(entity);
            JwsValidator jwtValidator = new JwsValidator(jwt, sharedKey, jwksUri, null);
            if (jwtValidator.validateSignature()) {
              getResponse().setClaims(jwt.getClaims().toMap());
            }
          }
        } else {
          try {
            JSONObject jsonObj = new JSONObject(entity);

            if (jsonObj.has("error")) {
              getResponse()
                  .setErrorType(UserInfoErrorResponseType.fromString(jsonObj.getString("error")));
              jsonObj.remove("error");
            }
            if (jsonObj.has("error_description")) {
              getResponse().setErrorDescription(jsonObj.getString("error_description"));
              jsonObj.remove("error_description");
            }
            if (jsonObj.has("error_uri")) {
              getResponse().setErrorUri(jsonObj.getString("error_uri"));
              jsonObj.remove("error_uri");
            }

            for (Iterator<String> iterator = jsonObj.keys(); iterator.hasNext(); ) {
              String key = iterator.next();
              List<String> values = new ArrayList<String>();

              JSONArray jsonArray = jsonObj.optJSONArray(key);
              if (jsonArray != null) {
                for (int i = 0; i < jsonArray.length(); i++) {
                  String value = jsonArray.optString(i);
                  if (value != null) {
                    values.add(value);
                  }
                }
              } else {
                String value = jsonObj.optString(key);
                if (value != null) {
                  values.add(value);
                }
              }

              getResponse().getClaims().put(key, values);
            }
          } catch (JSONException e) {
            e.printStackTrace();
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      closeConnection();
    }

    return getResponse();
  }
  @Override
  public String getSchemaJSON() {
    if (!changed && schemaJSON != null) {
      // If there are no changes, return the pre computed JSON
      return schemaJSON;
    }

    if (changedSchemaKeys) {
      // If the schema keys change, recompute the schema keys portion of the JSON
      changedSchemaKeys = false;

      if (schemaKeys == null) {
        schema.remove(Schema.FIELD_SCHEMA_KEYS);
      } else {
        try {
          schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys));
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }
      }
    }

    if (changedFromTo) {
      // If the from to times have changed then recompute the time portion of the schema.
      changedFromTo = false;
      Preconditions.checkState(
          !(from == null ^ to == null),
          "Either both from and to should be set or both should be not set.");

      if (from != null) {
        Preconditions.checkState(
            to >= from, "to {} must be greater than or equal to from {}.", to, from);
      }

      if (from == null) {
        time.remove(FIELD_TIME_FROM);
        time.remove(FIELD_TIME_TO);
      } else {
        try {
          time.put(FIELD_TIME_FROM, from);
          time.put(FIELD_TIME_TO, to);
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }
      }
    }

    if (this.areEnumsUpdated) {
      // If the enums have been updated, recompute the key portion of the schema.
      for (int keyIndex = 0; keyIndex < keys.length(); keyIndex++) {
        JSONObject keyData;
        String name;

        try {
          keyData = keys.getJSONObject(keyIndex);
          name = keyData.getString(DimensionalConfigurationSchema.FIELD_KEYS_NAME);
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }

        List<Object> enumVals = currentEnumVals.get(name);

        if (enumVals == null || enumVals.isEmpty()) {
          keyData.remove(DimensionalConfigurationSchema.FIELD_KEYS_ENUMVALUES);
          continue;
        }

        JSONArray newEnumValues = new JSONArray();

        for (Object enumVal : enumVals) {
          newEnumValues.put(enumVal);
        }

        try {
          keyData.put(DimensionalConfigurationSchema.FIELD_KEYS_ENUMVALUES, newEnumValues);
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }
      }

      this.areEnumsUpdated = false;
    }

    // Rebuild the schema JSON string.
    schemaJSON = schema.toString();
    return schemaJSON;
  }