private static Iterator<String> getScriptsToRun(JSONObject requestObject, Map configuration)
      throws IOException {

    if (configuration == null) {
      logger.warn(
          "No scripts are configured for the Gremlin Extension so 'load' query string parameter will be ignored");
      return null;
    }

    if (!configuration.containsKey("scripts")) {
      logger.warn(
          "The configuration suppled for the Gremlin Extension does not contain a 'scripts' key so 'load' query string parameter will be ignored");
      return null;
    }

    boolean scriptsAreCached = areScriptsCached(configuration);

    String scriptLocation = (String) configuration.get("scripts");

    final JSONArray jsonArray = requestObject != null ? requestObject.optJSONArray(LOAD) : null;

    Iterator<String> scripts = null;
    if (jsonArray != null) {
      List<String> scriptList = new ArrayList<String>();
      for (int ix = 0; ix < jsonArray.length(); ix++) {
        final String locationAndScriptFile =
            scriptLocation + File.separator + jsonArray.optString(ix) + ".gremlin";

        String script = cachedScripts.get(locationAndScriptFile);
        if (script == null) {
          script = readFile(locationAndScriptFile);

          if (scriptsAreCached) {
            cachedScripts.put(locationAndScriptFile, script);
          }
        }
        scriptList.add(script);
      }

      scripts = scriptList.iterator();
    }

    return scripts;
  }
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();
  }