|
| 1 | +/* |
| 2 | + * Copyright 2013 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.springframework.xd.shell.command; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URI; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.http.HttpStatus; |
| 26 | +import org.springframework.http.ResponseEntity; |
| 27 | +import org.springframework.http.client.ClientHttpResponse; |
| 28 | +import org.springframework.shell.core.CommandMarker; |
| 29 | +import org.springframework.shell.core.annotation.CliAvailabilityIndicator; |
| 30 | +import org.springframework.shell.core.annotation.CliCommand; |
| 31 | +import org.springframework.shell.core.annotation.CliOption; |
| 32 | +import org.springframework.shell.support.util.OsUtils; |
| 33 | +import org.springframework.stereotype.Component; |
| 34 | +import org.springframework.web.client.ResourceAccessException; |
| 35 | +import org.springframework.web.client.ResponseErrorHandler; |
| 36 | +import org.springframework.web.client.RestTemplate; |
| 37 | +import org.springframework.xd.shell.XDShell; |
| 38 | + |
| 39 | +/** |
| 40 | + * Http commands. |
| 41 | + * |
| 42 | + * @author Jon Brisbin |
| 43 | + * @author Ilayaperumal Gopinathan |
| 44 | + */ |
| 45 | + |
| 46 | +@Component |
| 47 | +public class HttpCommands implements CommandMarker { |
| 48 | + |
| 49 | + private static final String POST_HTTPSOURCE = "post httpsource"; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private XDShell xdShell; |
| 53 | + |
| 54 | + @CliAvailabilityIndicator({ POST_HTTPSOURCE }) |
| 55 | + public boolean available() { |
| 56 | + return xdShell.getSpringXDOperations() != null; |
| 57 | + } |
| 58 | + |
| 59 | + @CliCommand(value = { POST_HTTPSOURCE }, help = "POST data to http endpoint") |
| 60 | + public String postHttp( |
| 61 | + @CliOption(mandatory = true, key = "target") String target, |
| 62 | + @CliOption(mandatory = true, key = "data") String data) { |
| 63 | + final StringBuilder buffer = new StringBuilder(); |
| 64 | + URI requestURI = URI.create(target); |
| 65 | + RestTemplate restTemplate = new RestTemplate(); |
| 66 | + try { |
| 67 | + restTemplate.setErrorHandler(new ResponseErrorHandler() { |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean hasError(ClientHttpResponse response) |
| 71 | + throws IOException { |
| 72 | + HttpStatus status = response.getStatusCode(); |
| 73 | + return (status == HttpStatus.BAD_GATEWAY |
| 74 | + || status == HttpStatus.GATEWAY_TIMEOUT || status == HttpStatus.INTERNAL_SERVER_ERROR); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void handleError(ClientHttpResponse response) |
| 79 | + throws IOException { |
| 80 | + outputError(response.getStatusCode(), buffer); |
| 81 | + } |
| 82 | + }); |
| 83 | + outputRequest("POST", requestURI, data, buffer); |
| 84 | + ResponseEntity<String> response = restTemplate.postForEntity( |
| 85 | + requestURI, data, String.class); |
| 86 | + outputResponse(response, buffer); |
| 87 | + String status = (response.getStatusCode().equals(HttpStatus.OK) ? "Success" |
| 88 | + : "Error"); |
| 89 | + return String.format(buffer.toString() + status |
| 90 | + + " sending data '%s' to target '%s'", data, target); |
| 91 | + } |
| 92 | + catch (ResourceAccessException e) { |
| 93 | + return String.format(buffer.toString() |
| 94 | + + "Failed to access http endpoint %s", target); |
| 95 | + } |
| 96 | + catch (Exception e) { |
| 97 | + return String.format(buffer.toString() |
| 98 | + + "Failed to send data to http endpoint %s", target); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void outputRequest(String method, URI requestUri, |
| 103 | + String requestData, StringBuilder buffer) { |
| 104 | + buffer.append("> ") |
| 105 | + .append(method) |
| 106 | + .append(" ") |
| 107 | + .append(requestUri.toString()) |
| 108 | + .append(" ") |
| 109 | + .append(requestData) |
| 110 | + .append(OsUtils.LINE_SEPARATOR); |
| 111 | + } |
| 112 | + |
| 113 | + private void outputResponse(ResponseEntity<String> response, |
| 114 | + StringBuilder buffer) { |
| 115 | + buffer.append("> ") |
| 116 | + .append(response.getStatusCode().value()) |
| 117 | + .append(" ") |
| 118 | + .append(response.getStatusCode().name()) |
| 119 | + .append(OsUtils.LINE_SEPARATOR); |
| 120 | + for (Map.Entry<String, List<String>> entry : response.getHeaders() |
| 121 | + .entrySet()) { |
| 122 | + buffer.append("> ").append(entry.getKey()).append(": "); |
| 123 | + boolean first = true; |
| 124 | + for (String s : entry.getValue()) { |
| 125 | + if (!first) { |
| 126 | + buffer.append(","); |
| 127 | + } else { |
| 128 | + first = false; |
| 129 | + } |
| 130 | + buffer.append(s); |
| 131 | + } |
| 132 | + buffer.append(OsUtils.LINE_SEPARATOR); |
| 133 | + } |
| 134 | + buffer.append("> ").append(OsUtils.LINE_SEPARATOR); |
| 135 | + if (null != response.getBody()) { |
| 136 | + buffer.append(response.getBody()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private void outputError(HttpStatus status, StringBuilder buffer) { |
| 141 | + buffer.append("> ") |
| 142 | + .append(status.value()) |
| 143 | + .append(" ") |
| 144 | + .append(status.name()) |
| 145 | + .append(OsUtils.LINE_SEPARATOR); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments