Example usage for javax.script ScriptException ScriptException

List of usage examples for javax.script ScriptException ScriptException

Introduction

In this page you can find the example usage for javax.script ScriptException ScriptException.

Prototype

public ScriptException(Exception e) 

Source Link

Document

Creates a ScriptException wrapping an Exception thrown by an underlying interpreter.

Usage

From source file:org.wso2.carbon.hostobjects.sso.SAMLSSORelyingPartyObject.java

/**
 * Invalidate current browser authenticated session based on SAML log out request session index value.
 *
 * @param cx//from w w w. j  a v  a 2s .co  m
 * @param thisObj
 * @param args
 * @param funObj
 * @throws Exception
 */
public static void jsFunction_invalidateSessionBySAMLResponse(Context cx, Scriptable thisObj, Object[] args,
        Function funObj) throws Exception {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. SAML log out request is missing.");
    }
    String decodedString = Util.decode((String) args[0]);

    SAMLSSORelyingPartyObject relyingPartyObject = (SAMLSSORelyingPartyObject) thisObj;
    XMLObject samlObject = Util.unmarshall(decodedString);
    String sessionIndex = null;
    if (samlObject instanceof LogoutRequest) {
        // if log out request
        LogoutRequest samlLogoutRequest = (LogoutRequest) samlObject;
        List<SessionIndex> sessionIndexes = samlLogoutRequest.getSessionIndexes();
        if (sessionIndexes != null && sessionIndexes.size() > 0) {
            sessionIndex = sessionIndexes.get(0).getSessionIndex();
        }
    }

    if (sessionIndex == null) {
        throw new Exception("Failed to get session index from session indexes in SAML logout request.");
    }

    relyingPartyObject.invalidateSessionBySessionIndex(sessionIndex);
    // this is to invalidate relying party object after user log out. To release memory allocations.
    invalidateRelyingPartyObject(relyingPartyObject.getSSOProperty(SSOConstants.ISSUER_ID));
}

From source file:org.wso2.carbon.hostobjects.sso.SAMLSSORelyingPartyObject.java

/**
 * Invalidate current browser authenticated session based on session id.
 * Session will be invalidated after user log out request get succeeded.
 *
 * @param cx//from   ww  w.j av  a  2 s  .  com
 * @param thisObj
 * @param args
 * @param funObj
 * @throws Exception
 */
public static void jsFunction_invalidateSessionBySessionId(Context cx, Scriptable thisObj, Object[] args,
        Function funObj) throws Exception {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. Session id is missing.");
    }
    SAMLSSORelyingPartyObject relyingPartyObject = (SAMLSSORelyingPartyObject) thisObj;

    relyingPartyObject.invalidateSessionBySessionId((String) args[0]);
    // this is to invalidate relying party object after user log out. To release memory allocations.
    invalidateRelyingPartyObject(relyingPartyObject.getSSOProperty(SSOConstants.ISSUER_ID));

}

From source file:org.wso2.carbon.hostobjects.sso.SAMLSSORelyingPartyObject.java

/**
 * Set the current session as authenticated by mapping with current session id to session index.
 *
 * @param cx/*from  w w w  .  j a  v  a 2  s.c o m*/
 * @param thisObj
 * @param args    -args[0]- current session id, args[1]-SAML response
 * @param funObj
 * @throws Exception
 */
public static void jsFunction_setSessionAuthenticated(Context cx, Scriptable thisObj, Object[] args,
        Function funObj) throws Exception {
    int argLength = args.length;
    if (argLength != 2 || !(args[0] instanceof String) || !(args[1] instanceof String)) {
        throw new ScriptException("Invalid argument. Current session id and SAML response are missing.");
    }
    String decodedString = Util.decode((String) args[1]);
    SAMLSSORelyingPartyObject relyingPartyObject = (SAMLSSORelyingPartyObject) thisObj;
    XMLObject samlObject = Util.unmarshall(decodedString);
    String sessionIndex = null;
    String username = null;
    if (samlObject instanceof Response) {
        Response samlResponse = (Response) samlObject;
        List<Assertion> assertions = samlResponse.getAssertions();

        // extract the session index
        if (assertions != null && assertions.size() > 0) {
            List<AuthnStatement> authenticationStatements = assertions.get(0).getAuthnStatements();
            AuthnStatement authnStatement = authenticationStatements.get(0);
            if (authnStatement != null) {
                if (authnStatement.getSessionIndex() != null) {
                    sessionIndex = authnStatement.getSessionIndex();
                }
            }
        }

        // extract the username
        if (assertions != null && assertions.size() > 0) {
            Subject subject = assertions.get(0).getSubject();
            if (subject != null) {
                if (subject.getNameID() != null) {
                    username = subject.getNameID().getValue();
                }
            }
        }
    }
    if (sessionIndex == null) {
        throw new Exception("Failed to get session index from authentication statement in SAML response.");
    }
    if (username == null) {
        throw new Exception("Failed to get subject assertion from SAML response.");
    }

    SessionInfo sessionInfo = new SessionInfo((String) args[0]);
    sessionInfo.setSessionIndex(sessionIndex);
    sessionInfo.setLoggedInUser(username);
    sessionInfo.setSamlToken((String) args[1]);//We expect an encoded SamlToken here.
    relyingPartyObject.addSessionInfo(sessionInfo);

}

From source file:org.wso2.carbon.hostobjects.sso.SAMLSSORelyingPartyObject.java

/**
 * Get SSO configuration properties.//from  w  w w .j a va2  s  .c o m
 *
 * @param cx
 * @param thisObj
 * @param args    -args[0]-configuration key
 * @param funObj
 * @return
 * @throws ScriptException
 */
public static String jsFunction_getProperty(Context cx, Scriptable thisObj, Object[] args, Function funObj)
        throws ScriptException {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. SSO configuratin key is missing.");
    }
    SAMLSSORelyingPartyObject relyingPartyObject = (SAMLSSORelyingPartyObject) thisObj;
    return relyingPartyObject.getSSOProperty((String) args[0]);

}

From source file:org.wso2.carbon.hostobjects.sso.SAMLSSORelyingPartyObject.java

/**
 * Set relay state property with requested uri.
 *
 * @param cx/*from   www.  ja  va 2  s  . com*/
 * @param thisObj
 * @param args
 * @param funObj
 * @throws ScriptException
 */
public static void jsFunction_setRelayStateProperty(Context cx, Scriptable thisObj, Object[] args,
        Function funObj) throws ScriptException {
    int argLength = args.length;
    if (argLength != 2 || !(args[0] instanceof String) || !(args[1] instanceof String)) {
        throw new ScriptException("Invalid argument. RelayState and requested URI are missing.");
    }
    relayStateMap.put((String) args[0], (String) args[1]);

}

From source file:org.wso2.carbon.hostobjects.sso.SAMLSSORelyingPartyObject.java

/**
 * Get requested URI for relay state. And relay state value is removed, as relay state is unique and onetime value.
 *
 * @param cx//from  ww  w . j  av  a  2  s.c  o m
 * @param thisObj
 * @param args
 * @param funObj
 * @return
 * @throws ScriptException
 */
public static String jsFunction_getRelayStateProperty(Context cx, Scriptable thisObj, Object[] args,
        Function funObj) throws ScriptException {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. Relay state value is missing.");
    }
    String requestedURI = relayStateMap.get((String) args[0]);
    relayStateMap.remove((String) args[0]);
    return requestedURI;

}

From source file:org.wso2.carbon.hostobjects.sso.SAMLSSORelyingPartyObject.java

public static String jsFunction_xmlDecode(Context cx, Scriptable thisObj, Object[] args, Function funObj)
        throws ScriptException {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. Relay state value is missing.");
    }// w  w w  . j a  v a 2 s  .  c o m
    String xmlString = (String) args[0];
    xmlString = xmlString.replaceAll("&gt;", ">").replaceAll("&lt;", "<");
    //                .replaceAll("&apos;", "'").replaceAll("&quot;", "\"").replaceAll("&amp;", "&");

    return xmlString;

}

From source file:org.wso2.carbon.hostobjects.sso.SAMLSSORelyingPartyObject.java

public static String jsFunction_xmlEncode(Context cx, Scriptable thisObj, Object[] args, Function funObj)
        throws ScriptException {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. Relay state value is missing.");
    }/* www.  j av  a  2  s  .  c o  m*/
    String xmlString = (String) args[0];
    xmlString = xmlString.replaceAll(">", "&gt;").replaceAll("<", "&lt;");
    //                .replaceAll("'","&apos;").replaceAll("\"","&quot;").replaceAll("&","&amp;");

    return xmlString;

}

From source file:org.wso2telco.dep.nashornmediator.NashornMessageContext.java

/**
 * Set the JSON payload to the message context
 *
 * @param jsonPayload JSON payload passed as a Nashorn JSON object
 * @throws ScriptException Throws exception when internal error happens when setting the payload to axis2 message
 *                         context/*www.java 2 s .  c  o m*/
 */
public void setPayloadJSON(Object jsonPayload) throws ScriptException {
    try {
        ScriptObjectMirror json = (ScriptObjectMirror) scriptEngine.eval("JSON");
        String jsonString = (String) json.callMember("stringify", jsonPayload);
        jsonString = getClearString(jsonString);
        InputStream stream = new ByteArrayInputStream(jsonString.getBytes());
        org.apache.axis2.context.MessageContext axis2mc = ((Axis2MessageContext) messageContext)
                .getAxis2MessageContext();
        JsonUtil.getNewJsonPayload(axis2mc, stream, true, true);
        setJsonObject(messageContext, jsonPayload);
    } catch (AxisFault axisFault) {
        throw new ScriptException(axisFault);
    } catch (ScriptException scriptException) {
        throw scriptException;
    }
}

From source file:org.zaproxy.zap.extension.zest.ZestAuthenticationRunner.java

@Override
public HttpMessage authenticate(AuthenticationHelper helper, Map<String, String> paramsValues,
        GenericAuthenticationCredentials credentials) throws ScriptException {

    this.helper = helper;

    try {//from   w w  w  .  jav  a 2 s.  c o m
        paramsValues.put(USERNAME, credentials.getParam(USERNAME));
        paramsValues.put(PASSWORD, credentials.getParam(PASSWORD));

        this.run(script.getZestScript(), paramsValues);

        String respUrl = this.getVariable(ZestVariables.RESPONSE_URL);
        HttpMessage msg = new HttpMessage(new URI(respUrl, true));
        msg.setRequestHeader(this.getVariable(ZestVariables.REQUEST_METHOD) + " "
                + this.getVariable(ZestVariables.REQUEST_URL) + " " + msg.getRequestHeader().getVersion()
                + HttpHeader.CRLF + this.getVariable(ZestVariables.REQUEST_HEADER));
        msg.setRequestBody(this.getVariable(ZestVariables.REQUEST_BODY));
        msg.getRequestHeader().setContentLength(msg.getRequestBody().length());
        msg.setResponseHeader(this.getVariable(ZestVariables.RESPONSE_HEADER));
        msg.setResponseBody(this.getVariable(ZestVariables.RESPONSE_BODY));
        // Make sure the proper requesting user is set on the returned message
        msg.setRequestingUser(helper.getRequestingUser());

        return msg;

    } catch (Exception e) {
        throw new ScriptException(e);
    }
}