Skip to content

Commit da22b76

Browse files
committed
Merge pull request spring-attic#18 from skuppa/master
Added xml formatting support
2 parents 67073e7 + 55e1e81 commit da22b76

File tree

6 files changed

+111
-1
lines changed

6 files changed

+111
-1
lines changed

src/main/java/org/springframework/data/rest/shell/commands/HttpCommands.java

100644100755
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.springframework.context.ApplicationEventPublisher;
2929
import org.springframework.context.ApplicationEventPublisherAware;
3030
import org.springframework.data.rest.shell.context.ResponseEvent;
31+
import org.springframework.data.rest.shell.formatter.FormatProvider;
32+
import org.springframework.data.rest.shell.formatter.Formatter;
3133
import org.springframework.hateoas.Link;
3234
import org.springframework.http.HttpMethod;
3335
import org.springframework.http.HttpStatus;
@@ -79,6 +81,8 @@ public class HttpCommands implements CommandMarker, ApplicationEventPublisherAwa
7981
private ApplicationEventPublisher ctx;
8082
private Object lastResult;
8183
private URI requestUri;
84+
@Autowired
85+
private FormatProvider formatProvider;
8286

8387
@Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
8488
this.ctx = applicationEventPublisher;
@@ -498,7 +502,8 @@ private void outputResponse(ResponseEntity<String> response, StringBuilder buffe
498502
}
499503
buffer.append("< ").append(OsUtils.LINE_SEPARATOR);
500504
if(null != response.getBody()) {
501-
buffer.append(response.getBody());
505+
final Formatter formatter = formatProvider.getFormatter(response.getHeaders().getContentType().getSubtype());
506+
buffer.append(formatter.format(response.getBody()));
502507
}
503508
}
504509

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.springframework.data.rest.shell.formatter;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class FormatProvider {
10+
private final Collection<Formatter> availableFormatter = new ArrayList<Formatter>();
11+
private final NoOpFormatter noOpFormatter = new NoOpFormatter();
12+
13+
public FormatProvider() {
14+
availableFormatter.add(new XmlFormatter());
15+
}
16+
17+
public Formatter getFormatter(String contentType) {
18+
for (Formatter formatter : availableFormatter) {
19+
if(formatter.isSupported(contentType)) {
20+
return formatter;
21+
}
22+
}
23+
24+
return noOpFormatter;
25+
}
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.springframework.data.rest.shell.formatter;
2+
3+
public interface Formatter {
4+
boolean isSupported(String contentType);
5+
String format(String nonFormattedString);
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.springframework.data.rest.shell.formatter;
2+
3+
import java.util.Collection;
4+
5+
public abstract class FormatterSupport implements Formatter {
6+
@Override
7+
public boolean isSupported(String contentType) {
8+
boolean supported = false;
9+
for (String subType : getSupportedList()) {
10+
supported = contentType.indexOf(subType) > 0;
11+
if(supported) {
12+
break;
13+
}
14+
}
15+
return supported;
16+
}
17+
18+
public abstract Collection<String> getSupportedList();
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.springframework.data.rest.shell.formatter;
2+
3+
public class NoOpFormatter implements Formatter {
4+
@Override
5+
public boolean isSupported(String contentType) {
6+
return true;
7+
}
8+
9+
@Override
10+
public String format(String nonFormattedString) {
11+
return nonFormattedString;
12+
}
13+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.springframework.data.rest.shell.formatter;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.util.Arrays;
6+
import java.util.Collection;
7+
import java.util.List;
8+
import javax.xml.transform.OutputKeys;
9+
import javax.xml.transform.Source;
10+
import javax.xml.transform.Transformer;
11+
import javax.xml.transform.sax.SAXSource;
12+
import javax.xml.transform.sax.SAXTransformerFactory;
13+
import javax.xml.transform.stream.StreamResult;
14+
15+
import org.xml.sax.InputSource;
16+
17+
public class XmlFormatter extends FormatterSupport {
18+
private final List<String> supportedContentTypes = Arrays.asList("xml");
19+
20+
@Override
21+
public Collection<String> getSupportedList() {
22+
return supportedContentTypes;
23+
}
24+
25+
@Override
26+
public String format(String nonFormattedString) {
27+
try {
28+
Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
29+
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
30+
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
31+
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(nonFormattedString.getBytes())));
32+
StreamResult res = new StreamResult(new ByteArrayOutputStream());
33+
34+
serializer.transform(xmlSource, res);
35+
36+
return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
37+
} catch (Exception e) {
38+
return nonFormattedString;
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)