Skip to content

Commit

Permalink
Rest: Fixed some more issues with REST
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Aug 26, 2016
1 parent 4c66d6c commit c54d2d6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
35 changes: 23 additions & 12 deletions common/rest/src/main/java/com/speedment/common/rest/RestImpl.java
Expand Up @@ -21,6 +21,7 @@
import static com.speedment.common.rest.Rest.encode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -224,8 +225,10 @@ private CompletableFuture<Response> send(Method method, String path, Option[] op
}

private CompletableFuture<Response> send(Method method, String path, Option[] options, Iterator<String> stream) {
return send(method, path, options, out -> {
if (stream != NO_STREAM) {
if (stream == NO_STREAM) {
return send(method, path, options, StreamConsumer.IGNORE);
} else {
return send(method, path, options, out -> {
try (final BufferedWriter wr = new BufferedWriter(
new OutputStreamWriter(out))) {

Expand All @@ -234,11 +237,11 @@ private CompletableFuture<Response> send(Method method, String path, Option[] op
wr.append(data);
}
}
}
});
});
}
}

private CompletableFuture<Response> send(Method method, String path, Option[] options, StreamConsumer streamConsumer) {
private CompletableFuture<Response> send(Method method, String path, Option[] options, StreamConsumer outStreamConsumer) {
return CompletableFuture.supplyAsync(() -> {
final Param[] params = Stream.of(options).filter(o -> o.getType() == PARAM).toArray(Param[]::new);
final Header[] headers = Stream.of(options).filter(o -> o.getType() == HEADER).toArray(Header[]::new);
Expand Down Expand Up @@ -275,23 +278,23 @@ private CompletableFuture<Response> send(Method method, String path, Option[] op
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);

final boolean doOutput = streamConsumer != StreamConsumer.IGNORE;
if (doOutput) {
conn.setDoOutput(true);
}
final boolean doOutput = outStreamConsumer != StreamConsumer.IGNORE;
conn.setDoOutput(doOutput);

conn.connect();
if (doOutput) {
try (final OutputStream out = conn.getOutputStream()) {
streamConsumer.writeTo(out);
outStreamConsumer.writeTo(out);
}
}

final int status = conn.getResponseCode();
int status = getResponseCodeFrom(conn);
final String text;

try (final BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
new InputStreamReader(status >= 400
? conn.getErrorStream()
: conn.getInputStream()))) {

final StringBuilder sb = new StringBuilder();
String line;
Expand All @@ -313,6 +316,14 @@ private CompletableFuture<Response> send(Method method, String path, Option[] op
});
}

private static int getResponseCodeFrom(HttpURLConnection conn) throws IOException {
try {
return conn.getResponseCode();
} catch (final FileNotFoundException ex) {
return 404;
}
}

private final static Iterator<String> NO_STREAM = new Iterator<String>() {
@Override
public boolean hasNext() {
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.speedment.internal.common.lazy.specialized.LazyClass;
import static com.speedment.plugins.enums.internal.GeneratedEntityDecorator.FROM_DATABASE_METHOD;
import com.speedment.plugins.enums.internal.EnumGeneratorUtil;
import static com.speedment.plugins.enums.internal.GeneratedEntityDecorator.TO_DATABASE_METHOD;
import com.speedment.runtime.config.Column;
import com.speedment.runtime.config.mapper.TypeMapper;
import com.speedment.runtime.exception.SpeedmentException;
Expand Down Expand Up @@ -105,9 +106,9 @@ public T toJavaType(Column column, Class<?> entityType, String value) {
.orElse(null)
);

final Method method;
final Method fromDatabase;
try {
method = enumClass.getMethod(FROM_DATABASE_METHOD, String.class);
fromDatabase = enumClass.getMethod(FROM_DATABASE_METHOD, String.class);
} catch (final NoSuchMethodException ex) {
throw new SpeedmentException(
"Could not find generated '" + FROM_DATABASE_METHOD +
Expand All @@ -117,7 +118,7 @@ public T toJavaType(Column column, Class<?> entityType, String value) {

try {
@SuppressWarnings("unchecked")
final T result = (T) method.invoke(null, value);
final T result = (T) fromDatabase.invoke(null, value);
return result;
} catch (final IllegalAccessException
| IllegalArgumentException
Expand All @@ -138,24 +139,25 @@ public String toDatabaseType(T constant) {
} else {
final Class<?> enumClass = constant.getClass();

final Method method;
final Method toDatabase;
try {
method = enumClass.getMethod(FROM_DATABASE_METHOD, String.class);
toDatabase = enumClass.getMethod(TO_DATABASE_METHOD);

} catch (final NoSuchMethodException ex) {
throw new SpeedmentException(
"Could not find generated '" + FROM_DATABASE_METHOD + "'-method in enum class '" +
constant.getClass().getName() + "'."
"Could not find generated '" + TO_DATABASE_METHOD +
"'-method in enum class '" +
constant.getClass().getName() + "'.", ex
);
}

try {
@SuppressWarnings("unchecked")
final String result = (String) method.invoke(constant);
final String result = (String) toDatabase.invoke(constant);
return result;
} catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new SpeedmentException(
"Error executing '" + FROM_DATABASE_METHOD +
"Error executing '" + TO_DATABASE_METHOD +
"' in generated enum class '" + constant.getClass().getName() + "'.", ex
);
}
Expand Down

0 comments on commit c54d2d6

Please sign in to comment.