Skip to content

Commit

Permalink
update content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreampie committed Jan 19, 2016
1 parent 0b6c420 commit d0647af
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected HttpURLConnection getHttpConnection(String httpMethod) throws IOExcept

String contentType = clientRequest.getContentType();
//json请求
if (contentType.contains(ContentType.JSON)) {
if (contentType.contains(ContentType.JSON.value())) {
// 除了post意外其他请求类型都用 拼接参数方式
if (httpMethod.equals(HttpMethod.GET) || httpMethod.equals(HttpMethod.DELETE)) {
if (!"".equals(clientRequest.getJsonParam())) {
Expand All @@ -118,7 +118,7 @@ protected HttpURLConnection getHttpConnection(String httpMethod) throws IOExcept
conn = getStreamConnection(httpMethod, clientRequest);
outputParam(conn, httpMethod, clientRequest.getJsonParam(), clientRequest.getEncoding());
}
} else if (contentType.contains(ContentType.MULTIPART) || httpMethod.equals(HttpMethod.POST)) {
} else if (contentType.contains(ContentType.MULTIPART.value()) || httpMethod.equals(HttpMethod.POST)) {
//上传文件类型
conn = getStreamConnection(httpMethod, clientRequest);
//是上传文件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ClientFile(String filepath) throws FileNotFoundException {

public ClientFile(File file) throws FileNotFoundException {
this.name = file.getName();
this.contentType = HttpTyper.getContentTypeFromExtension(name);
this.contentType = HttpTyper.getContentTypeFromFileName(name);
this.inputStream = new FileInputStream(file);
}

Expand Down
42 changes: 31 additions & 11 deletions resty-common/src/main/java/cn/dreampie/common/http/ContentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,37 @@
/**
* Created by wangrenhui on 15/4/29.
*/
public class ContentType {
public enum ContentType {

public static final String TEXT = "text/plain";
public static final String HTML = "text/html";
public static final String XML = "text/xml";
public static final String JSON = "application/json";
public static final String FORM = "application/x-www-form-urlencoded";
public static final String MULTIPART = "multipart/form-data";
public static final String FILE = "application/octet-stream";
public static final String PNG = "image/png";
public static final String JPEG = "image/jpeg";
public static final String GIF = "image/gif";
TEXT("text", "text/plain"),
HTML("html", "text/html"),
XML("xml", "text/xml"),
JSON("json", "application/json"),
FORM("form", "application/x-www-form-urlencoded"),
MULTIPART("multipart", "multipart/form-data"),
FILE("file", "application/octet-stream"),
PNG("png", "image/png"),
JPEG("jpg", "image/jpeg"),
GIF("gif", "image/gif");

private String type;
private String value;

private ContentType(String type, String value) {
this.type = type;
this.value = value;
}

public static ContentType typeOf(String type) {
for (ContentType contentType : values()) {
if (contentType.type.equals(type)) {
return contentType;
}
}
throw new IllegalArgumentException("Invalid Content-Type: " + type);
}

public String value() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ public class HttpTyper {
}
}

public static String getContentTypeFromExtension(String filename) {
public static String getContentTypeFromFileName(String filename) {
String ext = filename.substring(filename.lastIndexOf('.') + 1);
return mimeType.getProperty(ext);
}

public static String getContentTypeFromExtension(String ext) {
return mimeType.getProperty(ext);
}

public static boolean isTextContentType(String contentType) {
// the list is not fully exhaustive, should cover most cases.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public RouteMatch match(HttpRequest request, HttpResponse response) {
String jsonParams = null;
String contentType = request.getContentType();
try {
if (contentType != null && contentType.toLowerCase().contains(ContentType.JSON)) {
if (contentType != null && contentType.toLowerCase().contains(ContentType.JSON.value())) {
//从 queryString 取json
if (httpMethod.equals(HttpMethod.GET) || httpMethod.equals(HttpMethod.DELETE)) {
jsonParams = request.getQueryString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void render(HttpRequest request, HttpResponse response, Object out) {
}

String fileName = file.getName();
response.setContentType(ContentType.FILE);
response.setContentType(ContentType.FILE.value());
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);

OutputStream outputStream = response.getOutputStream();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.dreampie.route.render;

import cn.dreampie.common.Render;
import cn.dreampie.common.http.ContentType;
import cn.dreampie.common.http.HttpRequest;
import cn.dreampie.common.http.HttpResponse;
import cn.dreampie.common.http.exception.WebException;
Expand Down Expand Up @@ -28,6 +29,7 @@ public void render(HttpRequest request, HttpResponse response, Object out) {
if (result == null) {
throw new WebException(HttpStatus.NOT_FOUND, "Image not support '" + out + "'.");
} else {
response.setContentType(ContentType.typeOf(result.getType()).value());
write(request, response, result.getType(), result.getResult());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class JsonRender extends Render {

public void render(HttpRequest request, HttpResponse response, Object out) {
if (out != null) {
response.setContentType(ContentType.JSON);
response.setContentType(ContentType.JSON.value());
if (out instanceof String) {
if (((String) out).startsWith("\"") || ((String) out).startsWith("{") || ((String) out).startsWith("[")) {
write(request, response, (String) out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TextRender extends Render {

public void render(HttpRequest request, HttpResponse response, Object out) {
if (out != null) {
response.setContentType(ContentType.TEXT);
response.setContentType(ContentType.TEXT.value());
write(request, response, out.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ else if (type1 != null) {
type = (type1.length() > type2.length() ? type1 : type2);
}

if (type == null || !type.toLowerCase().startsWith(ContentType.MULTIPART)) {
throw new WebException("Posted content type isn't '" + ContentType.MULTIPART + "'.");
if (type == null || !type.toLowerCase().startsWith(ContentType.MULTIPART.value())) {
throw new WebException("Posted content type isn't '" + ContentType.MULTIPART.value() + "'.");
}
// Check the content length to prevent denial of service attacks
int length = req.getContentLength();
Expand Down

0 comments on commit d0647af

Please sign in to comment.