Example usage for org.apache.commons.lang3 StringUtils trimToNull

List of usage examples for org.apache.commons.lang3 StringUtils trimToNull

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils trimToNull.

Prototype

public static String trimToNull(final String str) 

Source Link

Document

Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null .

Usage

From source file:org.bytesoft.bytejta.supports.springcloud.SpringCloudCoordinator.java

public Object invokePostCoordinator(Object proxy, Method method, Object[] args) throws Throwable {

    Class<?> returnType = method.getReturnType();
    try {//from w ww .  ja  va 2 s  .  c  o m
        RestTemplate transactionRestTemplate = SpringCloudBeanRegistry.getInstance().getRestTemplate();
        RestTemplate restTemplate = transactionRestTemplate == null ? new RestTemplate()
                : transactionRestTemplate;

        StringBuilder ber = new StringBuilder();

        int firstIndex = this.identifier.indexOf(":");
        int lastIndex = this.identifier.lastIndexOf(":");
        String prefix = firstIndex <= 0 ? null : this.identifier.substring(0, firstIndex);
        String suffix = lastIndex <= 0 ? null : this.identifier.substring(lastIndex + 1);

        ber.append("http://");
        ber.append(prefix == null || suffix == null ? null : prefix + ":" + suffix);
        ber.append("/org/bytesoft/bytejta/");
        ber.append(method.getName());
        for (int i = 0; i < args.length; i++) {
            Serializable arg = (Serializable) args[i];
            ber.append("/").append(this.serialize(arg));
        }

        ResponseEntity<?> response = restTemplate.postForEntity(ber.toString(), null, returnType,
                new Object[0]);

        return response.getBody();
    } catch (HttpClientErrorException ex) {
        throw new XAException(XAException.XAER_RMFAIL);
    } catch (HttpServerErrorException ex) {
        // int statusCode = ex.getRawStatusCode();
        HttpHeaders headers = ex.getResponseHeaders();
        String failureText = StringUtils.trimToNull(headers.getFirst("failure"));
        String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));

        Boolean failure = failureText == null ? null : Boolean.parseBoolean(failureText);
        Integer errorCode = null;
        try {
            errorCode = errorText == null ? null : Integer.parseInt(errorText);
        } catch (Exception ignore) {
            logger.debug(ignore.getMessage());
        }

        if (failure != null && errorCode != null) {
            throw new XAException(errorCode);
        } else {
            throw new XAException(XAException.XAER_RMERR);
        }
    } catch (Exception ex) {
        throw new XAException(XAException.XAER_RMERR);
    }

}

From source file:org.bytesoft.bytejta.supports.springcloud.SpringCloudCoordinator.java

public Object invokeGetCoordinator(Object proxy, Method method, Object[] args) throws Throwable {

    Class<?> returnType = method.getReturnType();
    try {/*  ww w  .  j a v a  2 s  .co  m*/
        RestTemplate transactionRestTemplate = SpringCloudBeanRegistry.getInstance().getRestTemplate();
        RestTemplate restTemplate = transactionRestTemplate == null ? new RestTemplate()
                : transactionRestTemplate;

        StringBuilder ber = new StringBuilder();

        int firstIndex = this.identifier.indexOf(":");
        int lastIndex = this.identifier.lastIndexOf(":");
        String prefix = firstIndex <= 0 ? null : this.identifier.substring(0, firstIndex);
        String suffix = lastIndex <= 0 ? null : this.identifier.substring(lastIndex + 1);

        ber.append("http://");
        ber.append(prefix == null || suffix == null ? null : prefix + ":" + suffix);
        ber.append("/org/bytesoft/bytejta/");
        ber.append(method.getName());
        for (int i = 0; i < args.length; i++) {
            Serializable arg = (Serializable) args[i];
            ber.append("/").append(this.serialize(arg));
        }

        ResponseEntity<?> response = restTemplate.getForEntity(ber.toString(), returnType, new Object[0]);

        return response.getBody();
    } catch (HttpClientErrorException ex) {
        throw new XAException(XAException.XAER_RMFAIL);
    } catch (HttpServerErrorException ex) {
        // int statusCode = ex.getRawStatusCode();
        HttpHeaders headers = ex.getResponseHeaders();
        String failureText = StringUtils.trimToNull(headers.getFirst("failure"));
        String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));

        Boolean failure = failureText == null ? null : Boolean.parseBoolean(failureText);
        Integer errorCode = null;
        try {
            errorCode = errorText == null ? null : Integer.parseInt(errorText);
        } catch (Exception ignore) {
            logger.debug(ignore.getMessage());
        }

        if (failure != null && errorCode != null) {
            throw new XAException(errorCode);
        } else {
            throw new XAException(XAException.XAER_RMERR);
        }
    } catch (Exception ex) {
        throw new XAException(XAException.XAER_RMERR);
    }

}

From source file:org.bytesoft.bytejta.supports.springcloud.web.TransactionRequestInterceptor.java

private void invokeAfterRecvResponse(ClientHttpResponse httpResponse, boolean serverFlag) throws IOException {
    SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance();
    TransactionBeanFactory beanFactory = beanRegistry.getBeanFactory();
    TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();

    HttpHeaders respHeaders = httpResponse.getHeaders();
    String respTransactionStr = respHeaders.getFirst(HEADER_TRANCACTION_KEY);
    String respPropagationStr = respHeaders.getFirst(HEADER_PROPAGATION_KEY);

    byte[] byteArray = ByteUtils.stringToByteArray(StringUtils.trimToNull(respTransactionStr));
    TransactionContext serverContext = (TransactionContext) CommonUtils.deserializeObject(byteArray);

    TransactionResponseImpl txResp = new TransactionResponseImpl();
    txResp.setTransactionContext(serverContext);
    RemoteCoordinator serverCoordinator = beanRegistry.getConsumeCoordinator(respPropagationStr);
    txResp.setSourceTransactionCoordinator(serverCoordinator);
    txResp.setParticipantDelistFlag(serverFlag ? false : true);

    transactionInterceptor.afterReceiveResponse(txResp);
}

From source file:org.bytesoft.bytetcc.supports.springcloud.ext.CompensableRequestInterceptor.java

private void invokeAfterRecvResponse(ClientHttpResponse httpResponse, boolean serverFlag) throws IOException {
    SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance();
    CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
    TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();

    HttpHeaders respHeaders = httpResponse.getHeaders();
    String respTransactionStr = respHeaders.getFirst(HEADER_TRANCACTION_KEY);
    String respPropagationStr = respHeaders.getFirst(HEADER_PROPAGATION_KEY);

    byte[] byteArray = ByteUtils.stringToByteArray(StringUtils.trimToNull(respTransactionStr));
    TransactionContext serverContext = (TransactionContext) CommonUtils.deserializeObject(byteArray);

    TransactionResponseImpl txResp = new TransactionResponseImpl();
    txResp.setTransactionContext(serverContext);
    RemoteCoordinator serverCoordinator = beanRegistry.getConsumeCoordinator(respPropagationStr);
    txResp.setSourceTransactionCoordinator(serverCoordinator);
    txResp.setParticipantDelistFlag(serverFlag ? false : true);

    transactionInterceptor.afterReceiveResponse(txResp);
}

From source file:org.bytesoft.bytetcc.supports.springcloud.SpringCloudCoordinator.java

public Object invokePostCoordinator(Object proxy, Method method, Object[] args) throws Throwable {
    Class<?> returnType = method.getReturnType();
    try {/*from w w  w .j a v  a 2s  . co  m*/
        int firstIndex = this.identifier.indexOf(":");
        int lastIndex = this.identifier.lastIndexOf(":");
        String prefix = firstIndex <= 0 ? null : this.identifier.substring(0, firstIndex);
        String suffix = lastIndex <= 0 ? null : this.identifier.substring(lastIndex + 1);

        String instanceId = prefix == null || suffix == null ? null : prefix + ":" + suffix;

        StringBuilder ber = new StringBuilder();
        ber.append("http://");
        ber.append(instanceId);
        ber.append("/org/bytesoft/bytetcc/");
        ber.append(method.getName());
        for (int i = 0; i < args.length; i++) {
            Serializable arg = (Serializable) args[i];
            ber.append("/").append(this.serialize(arg));
        }
        ResponseEntity<?> response = new RestTemplate().postForEntity(ber.toString(), null, returnType,
                new Object[0]);

        return response.getBody();
    } catch (HttpClientErrorException ex) {
        throw new XAException(XAException.XAER_RMFAIL);
    } catch (HttpServerErrorException ex) {
        // int statusCode = ex.getRawStatusCode();
        HttpHeaders headers = ex.getResponseHeaders();
        String failureText = StringUtils.trimToNull(headers.getFirst("failure"));
        String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));

        Boolean failure = failureText == null ? null : Boolean.parseBoolean(failureText);
        Integer errorCode = errorText == null || errorText.matches("\\d+") == false ? null
                : Integer.parseInt(errorText);

        if (failure != null && errorCode != null) {
            throw new XAException(errorCode);
        } else {
            throw new XAException(XAException.XAER_RMERR);
        }
    } catch (Exception ex) {
        throw new XAException(XAException.XAER_RMERR);
    }
}

From source file:org.bytesoft.bytetcc.supports.springcloud.SpringCloudCoordinator.java

public Object invokeGetCoordinator(Object proxy, Method method, Object[] args) throws Throwable {
    Class<?> returnType = method.getReturnType();
    try {/* w  w w.j  a  v  a2  s  .c  o m*/
        StringBuilder ber = new StringBuilder();
        ber.append("http://");
        ber.append(this.identifier);
        ber.append("/org/bytesoft/bytetcc/");
        ber.append(method.getName());
        for (int i = 0; i < args.length; i++) {
            Serializable arg = (Serializable) args[i];
            ber.append("/").append(this.serialize(arg));
        }
        ResponseEntity<?> response = new RestTemplate().getForEntity(ber.toString(), returnType, new Object[0]);

        return response.getBody();
    } catch (HttpClientErrorException ex) {
        throw new XAException(XAException.XAER_RMFAIL);
    } catch (HttpServerErrorException ex) {
        HttpHeaders headers = ex.getResponseHeaders();
        String failureText = StringUtils.trimToNull(headers.getFirst("failure"));
        String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));

        Boolean failure = failureText == null ? null : Boolean.parseBoolean(failureText);
        Integer errorCode = errorText == null || errorText.matches("\\d+") == false ? null
                : Integer.parseInt(errorText);

        if (failure != null && errorCode != null) {
            throw new XAException(errorCode);
        } else {
            throw new XAException(XAException.XAER_RMERR);
        }
    } catch (Exception ex) {
        throw new XAException(XAException.XAER_RMERR);
    }
}

From source file:org.camunda.bpm.spring.boot.starter.configuration.id.PrefixedUuidGenerator.java

public PrefixedUuidGenerator(final String applicationName) {
    this.prefix = requireNonNull(StringUtils.trimToNull(applicationName),
            "prefix must not be null or blank! set the spring.application.name property!");
}

From source file:org.candlepin.swagger.JAXBAnnotationsHelper.java

/**
 * Puts name space and name for XML node or attribute.
 *
 * @param ns       name space/*  w w  w .j  a  v  a 2 s .  c  om*/
 * @param name     name
 * @param property property instance to be updated
 * @return <code>true</code> if name space and name have been set
 */
private static boolean setName(String ns, String name, Property property) {
    boolean apply = false;
    final String cleanName = StringUtils.trimToNull(name);
    final String useName;
    if (!isEmpty(cleanName) && !cleanName.equals(property.getName())) {
        useName = cleanName;
        apply = true;
    } else {
        useName = null;
    }
    final String cleanNS = StringUtils.trimToNull(ns);
    final String useNS;
    if (!isEmpty(cleanNS)) {
        useNS = cleanNS;
        apply = true;
    } else {
        useNS = null;
    }
    // Set everything or nothing
    if (apply) {
        getXml(property).name(useName).namespace(useNS);
    }
    return apply;
}

From source file:org.codice.ddf.opensearch.endpoint.query.OpenSearchQuery.java

public void addStartEndTemporalFilter(String dateStart, String dateEnd) {
    addTemporalFilter(new TemporalFilter(StringUtils.trimToNull(dateStart), StringUtils.trimToNull(dateEnd)));
}

From source file:org.codice.ddf.opensearch.endpoint.query.OpenSearchQuery.java

public void addOffsetTemporalFilter(String dateOffset) {
    addTemporalFilter(new TemporalFilter(Long.parseLong(StringUtils.trimToNull(dateOffset))));
}