-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
baf53dc
commit c482443
Showing
4 changed files
with
107 additions
and
64 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/HttpResponseProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package me.chanjar.weixin.common.util.http; | ||
|
||
import jodd.http.HttpResponse; | ||
import me.chanjar.weixin.common.bean.result.WxError; | ||
import me.chanjar.weixin.common.exception.WxErrorException; | ||
import okhttp3.Response; | ||
import org.apache.http.Header; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* <pre> | ||
* 三种http框架的response代理类,方便提取公共方法 | ||
* Created by Binary Wang on 2017-8-3. | ||
* </pre> | ||
* | ||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | ||
*/ | ||
public class HttpResponseProxy { | ||
private CloseableHttpResponse apacheHttpResponse; | ||
private HttpResponse joddHttpResponse; | ||
private Response okHttpResponse; | ||
|
||
public HttpResponseProxy(CloseableHttpResponse apacheHttpResponse) { | ||
this.apacheHttpResponse = apacheHttpResponse; | ||
} | ||
|
||
public HttpResponseProxy(HttpResponse joddHttpResponse) { | ||
this.joddHttpResponse = joddHttpResponse; | ||
} | ||
|
||
public HttpResponseProxy(Response okHttpResponse) { | ||
this.okHttpResponse = okHttpResponse; | ||
} | ||
|
||
public String getFileName() throws WxErrorException { | ||
//由于对象只能由一个构造方法实现,因此三个response对象必定且只有一个不为空 | ||
if (this.apacheHttpResponse != null) { | ||
return this.getFileName(this.apacheHttpResponse); | ||
} | ||
|
||
if (this.joddHttpResponse != null) { | ||
return this.getFileName(this.joddHttpResponse); | ||
} | ||
|
||
if (this.okHttpResponse != null) { | ||
return this.getFileName(this.okHttpResponse); | ||
} | ||
|
||
//cannot happen | ||
return null; | ||
} | ||
|
||
private String getFileName(CloseableHttpResponse response) throws WxErrorException { | ||
Header[] contentDispositionHeader = response.getHeaders("Content-disposition"); | ||
if (contentDispositionHeader == null || contentDispositionHeader.length == 0) { | ||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | ||
} | ||
|
||
return this.extractFileNameFromContentString(contentDispositionHeader[0].getValue()); | ||
} | ||
|
||
private String getFileName(HttpResponse response) throws WxErrorException { | ||
String content = response.header("Content-disposition"); | ||
return this.extractFileNameFromContentString(content); | ||
} | ||
|
||
private String getFileName(Response response) throws WxErrorException { | ||
String content = response.header("Content-disposition"); | ||
return this.extractFileNameFromContentString(content); | ||
} | ||
|
||
private String extractFileNameFromContentString(String content) throws WxErrorException { | ||
if (content == null || content.length() == 0) { | ||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | ||
} | ||
|
||
Pattern p = Pattern.compile(".*filename=\"(.*)\""); | ||
Matcher m = p.matcher(content); | ||
if (m.matches()) { | ||
return m.group(1); | ||
} | ||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters