Search in sources :

Example 1 with SessionToken

use of org.netxms.websvc.SessionToken in project netxms by netxms.

the class Sessions method onPost.

/* (non-Javadoc)
    * @see org.netxms.websvc.handlers.AbstractHandler#onPost(org.restlet.representation.Representation)
    */
@Override
@Post
public Representation onPost(Representation entity) throws Exception {
    String login = null;
    String password = null;
    if (entity != null) {
        JSONObject request = new JsonRepresentation(entity).getJsonObject();
        login = request.getString("login");
        password = request.getString("password");
    } else {
        log.warn("No POST data in login call, looking for authentication data instead...");
        String authHeader = getHeader("Authorization");
        if ((authHeader != null) && !authHeader.isEmpty()) {
            String[] values = decodeBase64(authHeader).split(":", 2);
            if (values.length == 2) {
                login = values[0];
                password = values[1];
            }
        }
    }
    if ((login == null) || (password == null)) {
        log.warn("Login or password not specified in login call");
        return new StringRepresentation(createErrorResponse(RCC.INVALID_REQUEST).toString(), MediaType.APPLICATION_JSON);
    }
    SessionToken token = login(login, password);
    log.info("Logged in to NetXMS server, assigned session id " + token.getGuid());
    getCookieSettings().add(new CookieSetting(0, "session_handle", token.getGuid().toString(), "/", null));
    getResponse().getHeaders().add(new Header("Session-Id", token.getGuid().toString()));
    JSONObject response = new JSONObject();
    response.put("session", token.getGuid().toString());
    response.put("serverVersion", getSession().getServerVersion());
    return new StringRepresentation(response.toString(), MediaType.APPLICATION_JSON);
}
Also used : CookieSetting(org.restlet.data.CookieSetting) JSONObject(org.json.JSONObject) Header(org.restlet.data.Header) StringRepresentation(org.restlet.representation.StringRepresentation) SessionToken(org.netxms.websvc.SessionToken) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Post(org.restlet.resource.Post)

Example 2 with SessionToken

use of org.netxms.websvc.SessionToken in project netxms by netxms.

the class AbstractHandler method attachToSession.

/**
 * Attach this handler to server session
 *
 * @return true if attached successfully
 */
protected boolean attachToSession() throws Exception {
    SessionToken token = findSessionToken();
    if ((token == null) && (getHeader("Authorization") != null)) {
        String value = decodeBase64(getHeader("Authorization"));
        String login = value.substring(0, value.indexOf(':'));
        String password = value.substring(value.indexOf(':') + 1, value.length());
        log.debug("Cannot find session token - re-authenticating (login=" + login + ")");
        token = login(login, password);
        getCookieSettings().add(new CookieSetting(0, "session_handle", token.getGuid().toString(), "/", null));
        getResponse().getHeaders().add(new Header("Session-Id", token.getGuid().toString()));
    } else if (token != null) {
        log.debug("Handler attached to session " + token.getGuid());
        sessionToken = token;
        session = token.getSession();
    } else {
        log.debug("Session token not found and new session cannot be created");
    }
    return session != null;
}
Also used : CookieSetting(org.restlet.data.CookieSetting) Header(org.restlet.data.Header) SessionToken(org.netxms.websvc.SessionToken)

Aggregations

SessionToken (org.netxms.websvc.SessionToken)2 CookieSetting (org.restlet.data.CookieSetting)2 Header (org.restlet.data.Header)2 JSONObject (org.json.JSONObject)1 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)1 StringRepresentation (org.restlet.representation.StringRepresentation)1 Post (org.restlet.resource.Post)1