Search in sources :

Example 61 with SB

use of org.apache.ignite.internal.util.typedef.internal.SB in project ignite by apache.

the class GridRestProcessor method handleRequest.

/**
 * @param req Request.
 * @return Future.
 */
private IgniteInternalFuture<GridRestResponse> handleRequest(final GridRestRequest req) {
    if (startLatch.getCount() > 0) {
        try {
            startLatch.await();
        } catch (InterruptedException e) {
            return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request " + "(protocol handler was interrupted when awaiting grid start).", e));
        }
    }
    if (log.isDebugEnabled())
        log.debug("Received request from client: " + req);
    boolean authenticationEnabled = ctx.authentication().enabled();
    boolean securityEnabled = ctx.security().enabled();
    if (authenticationEnabled || securityEnabled) {
        Session ses;
        try {
            ses = session(req);
        } catch (IgniteCheckedException e) {
            GridRestResponse res = new GridRestResponse(STATUS_FAILED, e.getMessage());
            return new GridFinishedFuture<>(res);
        }
        assert ses != null;
        req.clientId(ses.clientId);
        req.sessionToken(U.uuidToBytes(ses.sesId));
        if (log.isDebugEnabled())
            log.debug("Next clientId and sessionToken were extracted according to request: " + "[clientId=" + req.clientId() + ", sesTok=" + Arrays.toString(req.sessionToken()) + "]");
        if (securityEnabled) {
            SecurityContext secCtx0 = ses.secCtx;
            try {
                if (secCtx0 == null)
                    ses.secCtx = secCtx0 = authenticate(req);
                authorize(req, secCtx0);
            } catch (SecurityException e) {
                assert secCtx0 != null;
                return new GridFinishedFuture<>(new GridRestResponse(STATUS_SECURITY_CHECK_FAILED, e.getMessage()));
            } catch (IgniteCheckedException e) {
                return new GridFinishedFuture<>(new GridRestResponse(STATUS_AUTH_FAILED, e.getMessage()));
            }
        } else {
            AuthorizationContext authCtx0 = ses.authCtx;
            try {
                if (authCtx0 == null) {
                    SecurityCredentials creds = credentials(req);
                    String login = null;
                    if (creds.getLogin() instanceof String)
                        login = (String) creds.getLogin();
                    String pwd = null;
                    if (creds.getPassword() instanceof String)
                        pwd = (String) creds.getPassword();
                    if (F.isEmpty(login) || F.isEmpty(pwd))
                        throw new IgniteAuthenticationException("The user name or password is incorrect");
                    ses.authCtx = ctx.authentication().authenticate(login, pwd);
                    req.authorizationContext(ses.authCtx);
                }
            } catch (IgniteCheckedException e) {
                return new GridFinishedFuture<>(new GridRestResponse(STATUS_AUTH_FAILED, e.getMessage()));
            }
        }
    }
    interceptRequest(req);
    GridRestCommandHandler hnd = handlers.get(req.command());
    IgniteInternalFuture<GridRestResponse> res = hnd == null ? null : hnd.handleAsync(req);
    if (res == null)
        return new GridFinishedFuture<>(new IgniteCheckedException("Failed to find registered handler for command: " + req.command()));
    return res.chain(new C1<IgniteInternalFuture<GridRestResponse>, GridRestResponse>() {

        @Override
        public GridRestResponse apply(IgniteInternalFuture<GridRestResponse> f) {
            GridRestResponse res;
            boolean failed = false;
            try {
                res = f.get();
            } catch (Exception e) {
                failed = true;
                if (!X.hasCause(e, VisorClusterGroupEmptyException.class))
                    LT.error(log, e, "Failed to handle request: " + req.command());
                if (log.isDebugEnabled())
                    log.debug("Failed to handle request [req=" + req + ", e=" + e + "]");
                // Prepare error message:
                SB sb = new SB(256);
                sb.a("Failed to handle request: [req=").a(req.command());
                if (req instanceof GridRestTaskRequest) {
                    GridRestTaskRequest tskReq = (GridRestTaskRequest) req;
                    sb.a(", taskName=").a(tskReq.taskName()).a(", params=").a(tskReq.params());
                }
                sb.a(", err=").a(e.getMessage() != null ? e.getMessage() : e.getClass().getName()).a(']');
                res = new GridRestResponse(STATUS_FAILED, sb.toString());
            }
            assert res != null;
            if (ctx.security().enabled() && !failed)
                res.sessionTokenBytes(req.sessionToken());
            interceptResponse(res, req);
            return res;
        }
    });
}
Also used : IgniteAuthenticationException(org.apache.ignite.IgniteAuthenticationException) SecurityException(org.apache.ignite.plugin.security.SecurityException) AuthorizationContext(org.apache.ignite.internal.processors.authentication.AuthorizationContext) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteAuthenticationException(org.apache.ignite.IgniteAuthenticationException) SecurityException(org.apache.ignite.plugin.security.SecurityException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) VisorClusterGroupEmptyException(org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException) SB(org.apache.ignite.internal.util.typedef.internal.SB) GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) SecurityCredentials(org.apache.ignite.plugin.security.SecurityCredentials) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestTaskRequest(org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest) SecurityContext(org.apache.ignite.internal.processors.security.SecurityContext) VisorClusterGroupEmptyException(org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException)

Example 62 with SB

use of org.apache.ignite.internal.util.typedef.internal.SB in project ignite by apache.

the class IgniteUtils method quiet.

/**
 * @param err Whether to print to {@code System.err}.
 * @param objs Objects to log in quiet mode.
 */
public static void quiet(boolean err, Object... objs) {
    assert objs != null;
    String time = SHORT_DATE_FMT.format(new java.util.Date());
    SB sb = new SB();
    for (Object obj : objs) sb.a('[').a(time).a("] ").a(obj.toString()).a(NL);
    PrintStream ps = err ? System.err : System.out;
    ps.print(compact(sb.toString()));
}
Also used : PrintStream(java.io.PrintStream) Date(java.util.Date) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 63 with SB

use of org.apache.ignite.internal.util.typedef.internal.SB in project ignite by apache.

the class IgniteUtils method formatMins.

/**
 * Pretty-formatting for minutes.
 *
 * @param mins Minutes to format.
 * @return Formatted presentation of minutes.
 */
public static String formatMins(long mins) {
    assert mins >= 0;
    if (mins == 0)
        return "< 1 min";
    SB sb = new SB();
    // 1440 mins = 60 mins * 24 hours
    long dd = mins / 1440;
    if (dd > 0)
        sb.a(dd).a(dd == 1 ? " day " : " days ");
    mins %= 1440;
    long hh = mins / 60;
    if (hh > 0)
        sb.a(hh).a(hh == 1 ? " hour " : " hours ");
    mins %= 60;
    if (mins > 0)
        sb.a(mins).a(mins == 1 ? " min " : " mins ");
    return sb.toString().trim();
}
Also used : SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 64 with SB

use of org.apache.ignite.internal.util.typedef.internal.SB in project ignite by apache.

the class IgniteUtils method addressesAsString.

/**
 * Returns string representation of addresses.
 *
 * @param addrs Addresses.
 * @param hostNames Host names.
 * @return String representation of addresses.
 */
public static String addressesAsString(Collection<String> addrs, Collection<String> hostNames) {
    if (F.isEmpty(addrs))
        return "";
    if (F.isEmpty(hostNames))
        return addrs.toString();
    SB sb = new SB("[");
    Iterator<String> hostNamesIt = hostNames.iterator();
    boolean first = true;
    for (String addr : addrs) {
        if (first)
            first = false;
        else
            sb.a(", ");
        String hostName = hostNamesIt.hasNext() ? hostNamesIt.next() : null;
        sb.a(hostName != null ? hostName : "").a('/').a(addr);
    }
    sb.a(']');
    return sb.toString();
}
Also used : SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 65 with SB

use of org.apache.ignite.internal.util.typedef.internal.SB in project ignite by apache.

the class IgniteUtils method makeMBeanName.

/**
 * Constructs JMX object name with given properties.
 * Map with ordered {@code groups} used for proper object name construction.
 *
 * @param igniteInstanceName Ignite instance name.
 * @param grp Name of the group.
 * @param name Name of mbean.
 * @return JMX object name.
 * @throws MalformedObjectNameException Thrown in case of any errors.
 */
public static ObjectName makeMBeanName(@Nullable String igniteInstanceName, @Nullable String grp, String name) throws MalformedObjectNameException {
    SB sb = new SB(JMX_DOMAIN + ':');
    appendClassLoaderHash(sb);
    appendJvmId(sb);
    if (igniteInstanceName != null && !igniteInstanceName.isEmpty())
        sb.a("igniteInstanceName=").a(igniteInstanceName).a(',');
    if (grp != null)
        sb.a("group=").a(escapeObjectNameValue(grp)).a(',');
    sb.a("name=").a(escapeObjectNameValue(name));
    return new ObjectName(sb.toString());
}
Also used : SB(org.apache.ignite.internal.util.typedef.internal.SB) ObjectName(javax.management.ObjectName)

Aggregations

SB (org.apache.ignite.internal.util.typedef.internal.SB)70 Map (java.util.Map)10 HashMap (java.util.HashMap)7 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 GridStringBuilder (org.apache.ignite.internal.util.GridStringBuilder)4 InputStreamReader (java.io.InputStreamReader)3 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 IgfsLogger (org.apache.ignite.internal.igfs.common.IgfsLogger)3 InputStream (java.io.InputStream)2 InterruptedIOException (java.io.InterruptedIOException)2 LineNumberReader (java.io.LineNumberReader)2 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2