Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#99 Added support for xml responses #149

Merged
merged 1 commit into from Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Expand Up @@ -40,4 +40,9 @@ target
diff.txt

*/.metadata
.metadata
.metadata


#netbeans
nb-configuration.xml
*/nb-configuration.xml
5 changes: 5 additions & 0 deletions rapidoid-commons/pom.xml
Expand Up @@ -37,6 +37,11 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
Expand Down
65 changes: 49 additions & 16 deletions rapidoid-commons/src/main/java/org/rapidoid/data/XML.java
@@ -1,5 +1,10 @@
package org.rapidoid.data;

import com.fasterxml.jackson.core.Base64Variants;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import org.rapidoid.RapidoidThing;

import javax.xml.bind.JAXBContext;
Expand All @@ -8,6 +13,12 @@
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.rapidoid.data.JSON.tuuidModule;
import org.rapidoid.env.Env;
import org.rapidoid.writable.ReusableWritable;

/*
* #%L
Expand Down Expand Up @@ -38,22 +49,45 @@ public class XML extends RapidoidThing {
private XML() {
}

public static String stringify(Object obj) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();


public static XmlMapper newMapper() {
XmlMapper mapper = new XmlMapper();
mapper.setBase64Variant(Base64Variants.MODIFIED_FOR_URL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
if (!Env.dev()) {
mapper.registerModule(new AfterburnerModule());
}

ByteArrayOutputStream out = new ByteArrayOutputStream();
jaxbMarshaller.marshal(obj, out);
return mapper;
}

return out.toString();


public static String stringify(Object obj) {
XmlMapper mapper = newMapper();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
mapper.writeValue(out, obj);
} catch (IOException ex) {
throw new RuntimeException(ex);
}

} catch (JAXBException e) {
throw new RuntimeException(e);
}
return out.toString();
}


public static void stringify(Object value, ReusableWritable out) {
XmlMapper mapper = newMapper();
try {
mapper.writeValue(out, value);
} catch (IOException ex) {
throw new RuntimeException(ex);
}

}


public static <T> T parse(String xml, Class<T> valueType) {
return parse(xml.getBytes(), valueType);
Expand All @@ -62,11 +96,10 @@ public static <T> T parse(String xml, Class<T> valueType) {
@SuppressWarnings("unchecked")
public static <T> T parse(byte[] xml, Class<T> valueType) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(valueType);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

return (T) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xml));


XmlMapper mapper = newMapper();
return mapper.readValue(xml, valueType);

} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.io.Serializable;
import java.util.Map;
import java.util.regex.Pattern;
import org.rapidoid.http.customize.MediaResponseRenderer;

/*
* #%L
Expand Down Expand Up @@ -219,12 +220,12 @@ public static String constructUrl(Req x, String path) {
return (Conf.ROOT.is("https") ? "https://" : "http://") + x.host() + path;
}

public static byte[] responseToBytes(Req req, Object result, MediaType contentType, JsonResponseRenderer jsonRenderer) {
if (U.eq(contentType, MediaType.JSON)) {
public static byte[] responseToBytes(Req req, Object result, MediaType contentType, MediaResponseRenderer responseRenderer) {
if (U.eq(contentType, MediaType.JSON) || U.eq(contentType, MediaType.XML_UTF_8)) {
ByteArrayOutputStream out = new ByteArrayOutputStream();

try {
jsonRenderer.renderJson(req, result, out);
responseRenderer.render(req, result, out);
} catch (Exception e) {
throw U.rte(e);
}
Expand Down
@@ -1,6 +1,7 @@
package org.rapidoid.http.customize;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
Expand Down Expand Up @@ -58,6 +59,10 @@ public class Customization extends RapidoidThing {
private volatile JsonResponseRenderer jsonResponseRenderer;

private volatile JsonRequestBodyParser jsonRequestBodyParser;

private volatile XmlResponseRenderer xmlResponseRenderer;

private volatile XmlRequestBodyParser xmlRequestBodyParser;

private volatile BeanParameterFactory beanParameterFactory;

Expand All @@ -68,6 +73,8 @@ public class Customization extends RapidoidThing {
private volatile BeanValidator validator;

private volatile ObjectMapper jackson;

private volatile XmlMapper jacksonXml;

private volatile EntityManagerProvider entityManagerProvider;

Expand Down Expand Up @@ -95,11 +102,13 @@ public synchronized void reset() {
viewResolver = null;
pageDecorator = null;
jsonResponseRenderer = null;
xmlResponseRenderer = null;
beanParameterFactory = null;
loginProvider = null;
rolesProvider = null;
validator = null;
jackson = null;
jacksonXml = null;
entityManagerProvider = null;
entityManagerFactoryProvider = null;
sessionManager = null;
Expand Down Expand Up @@ -198,6 +207,15 @@ public Customization jsonResponseRenderer(JsonResponseRenderer jsonResponseRende
this.jsonResponseRenderer = jsonResponseRenderer;
return this;
}

public XmlResponseRenderer xmlResponseRenderer(){
return xmlResponseRenderer != null || defaults == null ? xmlResponseRenderer : defaults.xmlResponseRenderer();
}

public Customization xmlResponseRenderer(XmlResponseRenderer xmlResponseRenderer){
this.xmlResponseRenderer = xmlResponseRenderer;
return this;
}

public BeanParameterFactory beanParameterFactory() {
return beanParameterFactory != null || defaults == null ? beanParameterFactory : defaults.beanParameterFactory();
Expand Down Expand Up @@ -243,7 +261,15 @@ public Customization jackson(ObjectMapper jackson) {
this.jackson = jackson;
return this;
}


public XmlMapper jacksonXml(){
return jacksonXml != null || defaults == null ? jacksonXml : defaults.jacksonXml();
}

public Customization jacksonXml(XmlMapper jacksonXml) {
this.jacksonXml = jacksonXml;
return this;
}
public EntityManagerProvider entityManagerProvider() {
return entityManagerProvider != null || defaults == null ? entityManagerProvider : defaults.entityManagerProvider();
}
Expand All @@ -270,6 +296,15 @@ public Customization jsonRequestBodyParser(JsonRequestBodyParser jsonRequestBody
this.jsonRequestBodyParser = jsonRequestBodyParser;
return this;
}

public XmlRequestBodyParser xmlRequestBodyParser() {
return xmlRequestBodyParser != null || defaults == null ? xmlRequestBodyParser : defaults.xmlRequestBodyParser();
}

public Customization xmlRequestBodyParser(XmlRequestBodyParser xmlRequestBodyParser) {
this.xmlRequestBodyParser = xmlRequestBodyParser;
return this;
}

public SessionManager sessionManager() {
return sessionManager != null || defaults == null ? sessionManager : defaults.sessionManager();
Expand Down
Expand Up @@ -28,8 +28,7 @@

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public interface JsonResponseRenderer {
public interface JsonResponseRenderer extends MediaResponseRenderer{

void renderJson(Req req, Object value, OutputStream out) throws Exception;

}
@@ -0,0 +1,34 @@

package org.rapidoid.http.customize;

import java.io.OutputStream;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.http.Req;


/*
* #%L
* rapidoid-http-fast
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

@Authors("Dan Cytermann")
@Since("5.5.0")
public interface MediaResponseRenderer {
void render(Req req, Object value, OutputStream out) throws Exception;
}
@@ -0,0 +1,35 @@
package org.rapidoid.http.customize;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.http.Req;

import java.util.Map;

/*
* #%L
* rapidoid-http-fast
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

@Authors("Dan Cytermann")
@Since("5.5.0")
public interface XmlRequestBodyParser {

Map<String, ?> parseXmlBody(Req req, byte[] body) throws Exception;

}
@@ -0,0 +1,34 @@
package org.rapidoid.http.customize;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.http.Req;

import java.io.OutputStream;

/*
* #%L
* rapidoid-http-fast
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

@Authors("Dan Cytermann")
@Since("5.5.0")
public interface XmlResponseRenderer extends MediaResponseRenderer{


}
Expand Up @@ -34,7 +34,7 @@
public class DefaultJsonResponseRenderer extends RapidoidThing implements JsonResponseRenderer {

@Override
public void renderJson(Req req, Object value, OutputStream out) throws Exception {
public void render(Req req, Object value, OutputStream out) throws Exception {
Customization.of(req).jackson().writeValue(out, value);
}

Expand Down