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

Gae support #48

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions client/.project
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<projectDescription>
<name>restfulie</name>
<comment>Sonatype helps open source projects to set up maven repositories on http://oss.sonatype.org. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>restfulie</name>
<comment>Sonatype helps open source projects to set up maven repositories on http://oss.sonatype.org. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
9 changes: 9 additions & 0 deletions client/src/main/java/br/com/caelum/restfulie/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,14 @@ public interface RestClient extends RequestEntry {
* Returns the required object for asynchronous requests of this client.
*/
ExecutorService getThreads();
/**
* Set custom charset
*/
RestClient withCharset(String charset);
/**
* Returns the default charset
*/
String charset();


}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class DefaultRestClient implements RestClient {
private RequestDispatcher dispatcher;

private Pluralizer inflector;

private String charset = "UTF-8";

private URI lastURI = null;

Expand Down Expand Up @@ -129,4 +131,13 @@ public ExecutorService getThreads() {
return threads;
}

public RestClient withCharset(String charset) {
this.charset = charset;
return null;
}

public String charset() {
return charset;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,18 @@ public Response process(Request details, String method, URI uri,
"You should set a content type prior to sending some payload.");
}

// ContentProducer cp = new ContentProducer() {
// public void writeTo(OutputStream outstream) throws IOException {
// Writer writer = new OutputStreamWriter(outstream, "UTF-8");
// String type = headers.get("Content-type");
// handlerFor(type).marshal(payload, writer);
// writer.flush();
// }
// };

StringWriter writer = new StringWriter();
String type = headers.get("Content-type");
try {

handlerFor(type).marshal(payload, writer, client);
writer.flush();

HttpEntityEnclosingRequestBase verb = (HttpEntityEnclosingRequestBase) verbFor(method, uri);
add(verb, headers);
String string = writer.getBuffer().toString();
verb.setEntity(new StringEntity(string));
verb.setEntity(new StringEntity(string,client.charset()));
return execute(details, verb);
} catch (IOException e) {
throw new RestfulieException("Unable to marshal entity.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public MapHeaders(Map<String, List<String>> fields) {
}

public List<String> get(String key) {
return fields.get(key);
List<String> values = fields.get(key);
if(values==null) values = fields.get(key.toLowerCase());
return values;
}

public String getMain(String key) {
if(!fields.containsKey(key)) {
if(!fields.containsKey(key)&&!fields.containsKey(key.toLowerCase())) {
throw new IllegalArgumentException("Unable to parse as field does not exist.");
}
return get(key).get(0).split(";")[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.net.URLEncoder;
import java.util.Map;

import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;

import br.com.caelum.restfulie.RestClient;

/**
Expand All @@ -27,9 +29,9 @@ public <T> void marshal(T payload, Writer writer, RestClient client) throws IOEx
Map<String, String> params = (Map<String, String>) payload;
int at = 0;
for (String key : params.keySet()) {
writer.append(URLEncoder.encode(key));
writer.append(URLEncoder.encode(key,client.charset()));
writer.append("=");
writer.append(URLEncoder.encode(params.get(key)));
writer.append(URLEncoder.encode(params.get(key),client.charset()));
if (++at != params.size()) {
writer.append("&");
}
Expand All @@ -40,4 +42,8 @@ public <T> T unmarshal(String content, RestClient client) {
return null;
}

public MediaType withReflectionProvider(ReflectionProvider refletionProvider) {
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import br.com.caelum.restfulie.relation.Enhancer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

/**
Expand Down Expand Up @@ -71,5 +72,10 @@ protected List<Class> getTypesToEnhance() {
protected List<String> getCollectionNames() {
return Collections.emptyList();
}

public MediaType withReflectionProvider(ReflectionProvider refletionProvider) {
helper.setReflectionProvider(refletionProvider);
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.io.Writer;

import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;

import br.com.caelum.restfulie.RestClient;

/**
Expand All @@ -24,4 +26,6 @@ public interface MediaType {
*/
<T> T unmarshal(String content, RestClient client);


MediaType withReflectionProvider(ReflectionProvider refletionProvider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class MediaTypes {
private final LinkedList<MediaType> types = new LinkedList<MediaType>();

public MediaType forContentType(String searching) {
searching = searching.split(";")[0];
for (MediaType type : types) {
if (type.answersTo(searching)) {
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import br.com.caelum.restfulie.relation.Enhancer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import com.thoughtworks.xstream.converters.reflection.ReflectionProviderWrapper;
import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider;
Expand Down Expand Up @@ -80,6 +81,7 @@ public boolean shouldSerializeMember(Class definedIn, String fieldName) {

private final HierarchicalStreamDriver driver;
private final Enhancer enhancer;
private ReflectionProvider reflectionProvider = new Sun14ReflectionProvider();

@SuppressWarnings("rawtypes")
private final Map<Class, Class> realTypes = new HashMap<Class, Class>();
Expand All @@ -89,15 +91,6 @@ public XStreamHelper(HierarchicalStreamDriver driver, Enhancer enhancer) {
this.enhancer = enhancer;
}

/**
* Extension point to define your own provider.
*
* @return
*/
protected ReflectionProvider getProvider() {
return new EnhancedLookupProvider(new Sun14ReflectionProvider());
}

/**
* Extension point for configuring your xstream instance.
*
Expand Down Expand Up @@ -142,5 +135,13 @@ protected MapperWrapper wrapMapper(MapperWrapper next) {

return xstream;
}

private ReflectionProvider getProvider() {
return new EnhancedLookupProvider(reflectionProvider);
}

public void setReflectionProvider(ReflectionProvider refletionProvider) {
this.reflectionProvider = refletionProvider;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import br.com.caelum.restfulie.relation.Enhancer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import com.thoughtworks.xstream.io.xml.QNameMap;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
Expand Down Expand Up @@ -121,4 +122,9 @@ public void withCollectionName(String... names) {
}
}

public MediaType withReflectionProvider(ReflectionProvider refletionProvider) {
helper.setReflectionProvider(refletionProvider);
return this;
}

}