Example usage for io.netty.util.internal StringUtil isNullOrEmpty

List of usage examples for io.netty.util.internal StringUtil isNullOrEmpty

Introduction

In this page you can find the example usage for io.netty.util.internal StringUtil isNullOrEmpty.

Prototype

public static boolean isNullOrEmpty(String s) 

Source Link

Document

Determine if a string is null or String#isEmpty() returns true .

Usage

From source file:SampleUser.java

License:Open Source License

/**
 * Determine if this name has been registered.
 *
 * @return {@code true} if registered; otherwise {@code false}.
 *//*from  w w w.ja v a  2  s  . c om*/
public boolean isRegistered() {
    return !StringUtil.isNullOrEmpty(enrollmentSecret);
}

From source file:brave.netty.http.TestHttpHandler.java

License:Apache License

private boolean writeResponse(HttpResponseStatus responseStatus, String content, ChannelHandlerContext ctx) {
    if (StringUtil.isNullOrEmpty(content)) {
        content = Unpooled.EMPTY_BUFFER.toString();
    }//from   w  w w. ja  v  a2  s .c  o m
    // Decide whether to close the connection or not.
    boolean keepAlive = isKeepAlive(httpRequest);

    if (responseStatus == null) {
        responseStatus = httpRequest.getDecoderResult().isSuccess() ? OK : BAD_REQUEST;
    }
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, responseStatus,
            Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, Values.KEEP_ALIVE);
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}

From source file:com.netty.telnet.impl.TelnetServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, String request) throws Exception {
    // Generate and write a response.
    String response;/*w w w  . j a  va 2s .c  o m*/
    boolean close = false;
    if (StringUtil.isNullOrEmpty(request)) {
        response = "Please type something.\r\n";
    } else if ("bye".equals(request.toLowerCase())) {
        response = "Have a good day!\r\n";
        close = true;
    } else {
        response = "Did you say '" + request + "'?\r\n";
    }

    // We do not need to write a ChannelBuffer here.
    // We know the encoder inserted at TelnetPipelineFactory will do the conversion.
    ChannelFuture future = ctx.write(response);

    // Close the connection after sending 'Have a good day!'
    // if the client has sent 'bye'.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.vmware.admiral.compute.container.network.NetworkUtils.java

License:Open Source License

public static void validateIpCidrNotation(String subnet) {
    if (!StringUtil.isNullOrEmpty(subnet) && !subnet.matches(REGEXP_CIDR_NOTATION)) {
        String error = String.format(FORMAT_CIDR_NOTATION_VALIDATION_ERROR, subnet);
        throw new LocalizableValidationException(error, "compute.network.validate.cidr", subnet);
    }/*ww w  .jav a  2 s.c  o m*/
}

From source file:com.vmware.admiral.compute.container.network.NetworkUtils.java

License:Open Source License

public static void validateIpAddress(String gateway) {
    if (!StringUtil.isNullOrEmpty(gateway) && !gateway.matches(REGEXP_IP_ADDRESS)) {
        String error = String.format(FORMAT_IP_VALIDATION_ERROR, gateway);
        throw new LocalizableValidationException(error, "compute.network.validate.ip", gateway);
    }/* w  ww .  j  a v  a 2  s . c o  m*/
}

From source file:com.vmware.admiral.request.compute.NetworkProfileQueryUtils.java

License:Open Source License

private static void getContextComputeNetworks(ServiceHost host, URI referer, String contextId,
        BiConsumer<Set<String>, Throwable> consumer,
        Consumer<HashMap<String, ComputeNetwork>> callbackFunction) {
    HashMap<String, ComputeNetwork> contextNetworks = new HashMap<>();
    if (StringUtil.isNullOrEmpty(contextId)) {
        callbackFunction.accept(contextNetworks);
        return;//from  w w w . j a v  a 2 s.  c  o m
    }

    // Get all ComputeNetworks that have the same context id
    List<ComputeNetwork> computeNetworks = new ArrayList<>();
    QueryTask.Query.Builder builder = QueryTask.Query.Builder.create().addKindFieldClause(ComputeNetwork.class);
    builder.addCompositeFieldClause(ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, FIELD_NAME_CONTEXT_ID_KEY,
            contextId);
    QueryUtils.QueryByPages<ComputeNetwork> query = new QueryUtils.QueryByPages<>(host, builder.build(),
            ComputeNetwork.class, null);
    query.queryDocuments(ns -> computeNetworks.add(ns)).whenComplete((v, e) -> {
        if (e != null) {
            consumer.accept(null, e);
            return;
        }
        // Get ComputeNetworkDescription of every network
        List<DeferredResult<Pair<String, ComputeNetwork>>> list = computeNetworks.stream()
                .map(cn -> host.sendWithDeferredResult(
                        Operation.createGet(host, cn.descriptionLink).setReferer(referer),
                        ComputeNetworkDescription.class).thenCompose(cnd -> {
                            DeferredResult<Pair<String, ComputeNetwork>> r = new DeferredResult<>();
                            r.complete(Pair.of(cnd.name, cn));
                            return r;
                        }))
                .collect(Collectors.toList());
        // Create a map of ComputeNetworkDescription.name to ComputeNetworkState
        DeferredResult.allOf(list).whenComplete((all, t) -> {
            all.forEach(p -> contextNetworks.put(p.getKey(), p.getValue()));
            callbackFunction.accept(contextNetworks);
        });
    });
}

From source file:com.vmware.admiral.request.ContainerPortsAllocationTaskService.java

License:Open Source License

private HostPortProfileService.HostPortProfileReservationRequest createHostPortProfileRequest(
        ContainerService.ContainerState containerState) {
    if (containerState.ports == null || containerState.ports.isEmpty()) {
        return null;
    }/*from   w ww. j  a v a2  s.c o m*/

    // get port bindings with specified host_port
    Set<Long> requestedPorts = containerState.ports.stream()
            .filter(p -> !StringUtil.isNullOrEmpty(p.hostPort) && Integer.parseInt(p.hostPort) > 0)
            .map(k -> (long) Integer.parseInt(k.hostPort)).collect(Collectors.toSet());
    // get port bindings that do not specify host_port
    long additionalPortCount = containerState.ports.stream()
            .filter((p) -> StringUtil.isNullOrEmpty(p.hostPort) || Integer.parseInt(p.hostPort) == 0).count();

    // return null if there a no ports to allocate
    if (requestedPorts.size() == 0 && additionalPortCount == 0) {
        return null;
    }

    HostPortProfileService.HostPortProfileReservationRequest request = new HostPortProfileService.HostPortProfileReservationRequest();
    request.containerLink = containerState.documentSelfLink;
    request.additionalHostPortCount = additionalPortCount;
    request.specificHostPorts = requestedPorts;
    request.mode = HostPortProfileService.HostPortProfileReservationRequestMode.ALLOCATE;
    return request;
}

From source file:com.vmware.admiral.request.ContainerPortsAllocationTaskService.java

License:Open Source License

private void updateContainerPorts(ContainerPortsAllocationTaskState state, ServiceTaskCallback taskCallback) {
    if (taskCallback == null) {
        createCounterSubTaskCallback(state, state.containerStateLinks.size(), false,
                ContainerPortsAllocationTaskState.SubStage.COMPLETED,
                (serviceTask) -> updateContainerPorts(state, serviceTask));
        return;//www. j  av a 2s  . c  o  m
    }

    for (ContainerService.ContainerState containerState : containerStates) {
        HostPortProfileService.HostPortProfileState profile = hostPortProfileStates.stream()
                .filter(p -> p.hostLink.equals(containerState.parentLink)).findFirst().orElse(null);
        if (profile == null || containerState.ports == null) {
            completeSubTasksCounter(taskCallback, null);
            continue;
        }

        // get all ports reserved for the container
        Set<Long> allocatedPorts = HostPortProfileService.getAllocatedPorts(profile,
                containerState.documentSelfLink);
        // remove explicitly defined host_ports from reservedPorts
        allocatedPorts.removeIf(p -> containerState.ports.stream().anyMatch(
                c -> !StringUtil.isNullOrEmpty(c.hostPort) && p.intValue() == (Integer.parseInt(c.hostPort))));
        // assign allocated ports to container port bindings
        Iterator<Long> hostPortStatesIterator = allocatedPorts.iterator();
        containerState.ports.stream()
                .filter(p -> StringUtil.isNullOrEmpty(p.hostPort) || Integer.parseInt(p.hostPort) == 0)
                .forEach(p -> {
                    if (hostPortStatesIterator.hasNext()) {
                        p.hostPort = hostPortStatesIterator.next().toString();
                    } else {
                        completeSubTasksCounter(taskCallback,
                                new IllegalStateException("Not enough ports allocated"));
                    }
                });

        // update container state
        sendRequest(Operation.createPatch(getHost(), containerState.documentSelfLink).setBody(containerState)
                .setCompletion((o, e) -> {
                    if (e != null) {
                        completeSubTasksCounter(taskCallback, e);
                        return;
                    }
                    ContainerService.ContainerState body = o.getBody(ContainerService.ContainerState.class);
                    logInfo("Updated ContainerState: %s ", body.documentSelfLink);
                    completeSubTasksCounter(taskCallback, null);
                }));

    }
}

From source file:org.apache.pulsar.common.util.FieldParser.java

License:Apache License

/**
 * Converts String to Integer./* w ww  .  j  a v a  2 s .  co  m*/
 *
 * @param value
 *            The String to be converted.
 * @return The converted Integer value.
 */
public static Integer stringToInteger(String val) {
    String v = trim(val);
    if (StringUtil.isNullOrEmpty(v)) {
        return null;
    } else {
        return Integer.valueOf(v);
    }
}

From source file:org.apache.pulsar.common.util.FieldParser.java

License:Apache License

/**
 * Converts String to Double./*ww w .ja v a  2  s  . co  m*/
 *
 * @param value
 *            The String to be converted.
 * @return The converted Double value.
 */
public static Double stringToDouble(String val) {
    String v = trim(val);
    if (StringUtil.isNullOrEmpty(v)) {
        return null;
    } else {
        return Double.valueOf(v);
    }
}