Skip to content

Commit

Permalink
More fixes for #1529
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Mar 24, 2014
1 parent 441b4eb commit 328aedd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.cpr.AtmosphereHandler;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceImpl;
import org.atmosphere.cpr.AtmosphereResponse;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -64,7 +66,9 @@ public void onStateChange(AtmosphereResourceEvent event)
Object message = event.getMessage();
AtmosphereResource resource = event.getResource();
AtmosphereResponse r = resource.getResponse();
AtmosphereRequest request = resource.getRequest();

boolean writeAsBytes = IOUtils.isBodyBinary(request);
if (message == null) {
logger.trace("Message was null for AtmosphereEvent {}", event);
return;
Expand Down Expand Up @@ -97,6 +101,9 @@ public void onStateChange(AtmosphereResourceEvent event)
} catch (IllegalStateException e) {
isUsingStream = true;
}
if (writeAsBytes) {
throw new IllegalStateException("Cannot write bytes using PrintWriter");
}
}

if (message instanceof List) {
Expand Down Expand Up @@ -138,7 +145,7 @@ public void onStateChange(AtmosphereResourceEvent event)
}
} else {
if (isUsingStream) {
r.getOutputStream().write(message.toString().getBytes(r.getCharacterEncoding()));
r.getOutputStream().write(writeAsBytes ? (byte[])message : message.toString().getBytes(r.getCharacterEncoding()));
r.getOutputStream().flush();
} else {
r.getWriter().write(message.toString());
Expand Down
3 changes: 2 additions & 1 deletion modules/cpr/src/main/java/org/atmosphere/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public static Object readEntirely(AtmosphereResource r) {
}

public final static boolean isBodyBinary(AtmosphereRequest request) {
if (request.getHeader(FORCE_BINARY) != null || request.getHeader(X_ATMO_BINARY) != null) return true;
if (request.getContentType() != null
&& request.getContentType().equalsIgnoreCase(FORCE_BINARY) || request.getHeader(X_ATMO_BINARY) != null) return true;
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected static AtmosphereRequest.Builder constructRequest(AtmosphereResource r
AtmosphereRequest.Builder b = (new AtmosphereRequest.Builder()
.request(request)
.method(methodType)
.contentType(contentType)
.contentType(contentType == null ? request.getContentType() : contentType)
.attributes(m)
.pathInfo(pathInfo)
.requestURI(requestURI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
public class SimpleHttpProtocol implements WebSocketProtocol, Serializable {

private static final Logger logger = LoggerFactory.getLogger(SimpleHttpProtocol.class);
protected String contentType = "text/plain";
protected final static String TEXT = "text/plain";
protected String contentType = TEXT;
protected String methodType = "POST";
protected String delimiter = "@@";
protected boolean destroyable;
Expand Down Expand Up @@ -104,7 +105,7 @@ public List<AtmosphereRequest> onMessage(WebSocket webSocket, String message) {
}

List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
list.add(constructRequest(resource, pathInfo, requestURI, methodType, contentType, destroyable).body(message).build());
list.add(constructRequest(resource, pathInfo, requestURI, methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(message).build());

return list;
}
Expand All @@ -124,7 +125,7 @@ public List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] d, final in
if (!resource.isInScope()) return Collections.emptyList();

List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType, destroyable).body(d, offset, length).build());
list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(d, offset, length).build());

return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
public class StreamingHttpProtocol implements WebSocketProtocolStream {

private static final Logger logger = LoggerFactory.getLogger(StreamingHttpProtocol.class);
protected final static String TEXT = "text/plain";
protected String contentType = "text/plain";
protected String methodType = "POST";
protected String delimiter = "@@";
Expand Down Expand Up @@ -95,7 +96,7 @@ public List<AtmosphereRequest> onTextStream(WebSocket webSocket, Reader r) {

AtmosphereRequest request = resource.getRequest();
List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType, destroyable).reader(r).build());
list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).reader(r).build());

return list;
}
Expand All @@ -111,7 +112,7 @@ public List<AtmosphereRequest> onBinaryStream(WebSocket webSocket, InputStream s

AtmosphereRequest request = resource.getRequest();
List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType, destroyable).inputStream(stream).build());
list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).inputStream(stream).build());

return list;
}
Expand Down

0 comments on commit 328aedd

Please sign in to comment.