View Javadoc
1   /*
2    * Copyright 2017 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.dccon.repository.cli;
18  
19  import java.io.Serializable;
20  import java.util.StringTokenizer;
21  import lombok.AllArgsConstructor;
22  import lombok.EqualsAndHashCode;
23  import lombok.Getter;
24  import lombok.ToString;
25  import org.springframework.util.StringUtils;
26  
27  /**
28   * The command executor response.
29   *
30   * @author Christian Bremer
31   */
32  @Getter
33  @ToString
34  @EqualsAndHashCode
35  @AllArgsConstructor
36  @SuppressWarnings("WeakerAccess")
37  public class CommandExecutorResponse implements Serializable {
38  
39    /**
40     * Return {@link #toOneLine()} if response is not {@code null}.
41     *
42     * @param response the response (can be {@code null})
43     * @return the response in one line
44     */
45    public static String toExceptionMessage(CommandExecutorResponse response) {
46      return response == null ? "" : response.toOneLine();
47    }
48  
49    private String stdout;
50  
51    private String stderr;
52  
53    /**
54     * Check whether stdout has text or not.
55     *
56     * @return {@code true} if stdout has text, otherwise {@code false}
57     */
58    @SuppressWarnings("unused")
59    public boolean stdoutHasText() {
60      return hasText(stdout);
61    }
62  
63    /**
64     * Check whether stderr has text or not.
65     *
66     * @return {@code true} if stderr has text, otherwise {@code false}
67     */
68    @SuppressWarnings("unused")
69    public boolean stderrHasText() {
70      return hasText(stderr);
71    }
72  
73    private boolean hasText(String value) {
74      return value != null && value.trim().length() > 0;
75    }
76  
77    /**
78     * Returns stdout and stderr in one line.
79     *
80     * @return the line
81     */
82    public String toOneLine() {
83      final String out = stdoutToOneLine();
84      final String err = stderrToOneLine();
85      final StringBuilder sb = new StringBuilder();
86      if (StringUtils.hasText(out)) {
87        sb.append("stdout=[").append(out).append("]");
88        if (StringUtils.hasText(err)) {
89          sb.append(" ");
90        }
91      }
92      if (StringUtils.hasText(err)) {
93        sb.append("stderr=[").append(err).append("]");
94      }
95      return sb.toString();
96    }
97  
98    private String toOneLine(String value) {
99  
100     if (!hasText(value)) {
101       return "";
102     }
103     StringBuilder sb = new StringBuilder();
104     StringTokenizer tokenizer = new StringTokenizer(value.trim(), "\n\r\f");
105     int n = 0;
106     while (tokenizer.hasMoreTokens()) {
107       if (n > 0) {
108         sb.append(" | ");
109       }
110       sb.append(tokenizer.nextToken().trim());
111       n++;
112     }
113     return sb.toString();
114   }
115 
116   /**
117    * Stdout to one line.
118    *
119    * @return the line
120    */
121   public String stdoutToOneLine() {
122     return toOneLine(stdout);
123   }
124 
125   /**
126    * Stderr to one line.
127    *
128    * @return the line
129    */
130   public String stderrToOneLine() {
131     return toOneLine(stderr);
132   }
133 
134 }