"git@git.gitsec.cn:baidan/nomad.git" did not exist on "89a1b60c1b3b22e6f603b546cb4f47b8ee984dd5"
Unverified Commit 6fcaee26 authored by Hai Liang W's avatar Hai Liang W Committed by GitHub
Browse files

Merge pull request #674 from xiaobo9/bugfix/try-with-resource

fix: try with resource ensure resource close
parents d4152720 8b6c6dec
Showing with 110 additions and 141 deletions
+110 -141
......@@ -42,6 +42,7 @@ import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Component
public class ACDAgentService {
......@@ -93,49 +94,48 @@ public class ACDAgentService {
* @param ctx
*/
public void notifyAgentUserProcessResult(final ACDComposeContext ctx) {
if (ctx != null && StringUtils.isNotBlank(
ctx.getMessage())) {
logger.info("[onConnect] find available agent for onlineUser id {}", ctx.getOnlineUserId());
/**
* 发送消息给坐席
* 如果没有AgentService或该AgentService没有坐席或AgentService在排队中,则不发送
*/
if (ctx.getAgentService() != null && (!ctx.isNoagent()) && !StringUtils.equals(
MainContext.AgentUserStatusEnum.INQUENE.toString(),
ctx.getAgentService().getStatus())) {
// 通知消息到坐席
MainContext.getPeerSyncIM().send(MainContext.ReceiverType.AGENT,
MainContext.ChannelType.WEBIM,
ctx.getAppid(),
MainContext.MessageType.NEW,
ctx.getAgentService().getAgentno(),
ctx, true);
}
/**
* 发送消息给访客
*/
Message outMessage = new Message();
outMessage.setAgentUser(ctx.getAgentUser());
outMessage.setMessage(ctx.getMessage());
outMessage.setMessageType(MainContext.MessageType.MESSAGE.toString());
outMessage.setCalltype(MainContext.CallType.IN.toString());
outMessage.setCreatetime(MainUtils.dateFormate.format(new Date()));
outMessage.setNoagent(ctx.isNoagent());
if (ctx.getAgentService() != null) {
outMessage.setAgentserviceid(ctx.getAgentService().getId());
}
Objects.requireNonNull(ctx,"ctx can not be null");
if (StringUtils.isBlank(ctx.getMessage())) {
logger.info("[onConnect] can not find available agent for user {}", ctx.getOnlineUserId());
return;
}
logger.info("[onConnect] find available agent for onlineUser id {}", ctx.getOnlineUserId());
MainContext.getPeerSyncIM().send(MainContext.ReceiverType.VISITOR,
MainContext.ChannelType.toValue(ctx.getChannel()),
/**
* 发送消息给坐席
* 如果没有AgentService或该AgentService没有坐席或AgentService在排队中,则不发送
*/
if (ctx.getAgentService() != null && (!ctx.isNoagent()) && !StringUtils.equals(
MainContext.AgentUserStatusEnum.INQUENE.toString(),
ctx.getAgentService().getStatus())) {
// 通知消息到坐席
MainContext.getPeerSyncIM().send(MainContext.ReceiverType.AGENT,
MainContext.ChannelType.WEBIM,
ctx.getAppid(),
MainContext.MessageType.NEW, ctx.getOnlineUserId(), outMessage, true);
MainContext.MessageType.NEW,
ctx.getAgentService().getAgentno(),
ctx, true);
}
} else {
logger.info("[onConnect] can not find available agent for user {}", ctx.getOnlineUserId());
/**
* 发送消息给访客
*/
Message outMessage = new Message();
outMessage.setAgentUser(ctx.getAgentUser());
outMessage.setMessage(ctx.getMessage());
outMessage.setMessageType(MainContext.MessageType.MESSAGE.toString());
outMessage.setCalltype(MainContext.CallType.IN.toString());
outMessage.setCreatetime(MainUtils.dateFormate.format(new Date()));
outMessage.setNoagent(ctx.isNoagent());
if (ctx.getAgentService() != null) {
outMessage.setAgentserviceid(ctx.getAgentService().getId());
}
MainContext.getPeerSyncIM().send(MainContext.ReceiverType.VISITOR,
MainContext.ChannelType.toValue(ctx.getChannel()),
ctx.getAppid(),
MainContext.MessageType.NEW, ctx.getOnlineUserId(), outMessage, true);
}
/**
......
......@@ -302,11 +302,10 @@ public class RedisCommand {
public List<String> getSet(final String key) {
Set<String> s = redisSetOps.members(key);
if (s != null & s.size() > 0) {
return new ArrayList<>(s);
} else {
if (CollectionUtils.isEmpty(s)) {
return new ArrayList<>();
}
return new ArrayList<>(s);
}
}
......@@ -82,9 +82,10 @@ public class MessagingServerConfigure {
File sslFile = new File(path, "ssl/https.properties");
if (sslFile.exists()) {
Properties sslProperties = new Properties();
FileInputStream in = new FileInputStream(sslFile);
sslProperties.load(in);
in.close();
try (FileInputStream in = new FileInputStream(sslFile)) {
sslProperties.load(in);
}
if (StringUtils.isNotBlank(sslProperties.getProperty("key-store")) && StringUtils.isNotBlank(
sslProperties.getProperty("key-store-password"))) {
config.setKeyStorePassword(MainUtils.decryption(sslProperties.getProperty("key-store-password")));
......
......@@ -22,12 +22,10 @@ import com.chatopera.cc.controller.Handler;
import com.chatopera.cc.controller.api.request.RestUtils;
import com.chatopera.cc.model.AttachmentFile;
import com.chatopera.cc.model.StreamingFile;
import com.chatopera.cc.model.UploadStatus;
import com.chatopera.cc.persistence.blob.JpaBlobHelper;
import com.chatopera.cc.persistence.repository.AttachmentRepository;
import com.chatopera.cc.persistence.repository.StreamingFileRepository;
import com.chatopera.cc.util.Menu;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
......@@ -42,7 +40,6 @@ import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -100,16 +97,18 @@ public class MediaController extends Handler {
@RequestMapping("/url")
@Menu(type = "resouce", subtype = "image", access = true)
public void url(HttpServletResponse response, @Valid String url) throws IOException {
if (StringUtils.isBlank(url)) {
return;
}
byte[] data = new byte[1024];
int length = 0;
OutputStream out = response.getOutputStream();
if (StringUtils.isNotBlank(url)) {
InputStream input = new URL(url).openStream();
try (InputStream input = new URL(url).openStream()) {
while ((length = input.read(data)) > 0) {
out.write(data, 0, length);
}
input.close();
}
}
@RequestMapping("/image/upload")
......
......@@ -147,9 +147,9 @@ public class UKResultMapper extends AbstractResultMapper {
private String buildJSONFromFields(Collection<SearchHitField> values) {
JsonFactory nodeFactory = new JsonFactory();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);) {
generator.writeStartObject();
for (SearchHitField value : values) {
if (value.getValues().size() > 1) {
......
......@@ -147,9 +147,9 @@ public class XiaoEUKResultMapper extends AbstractResultMapper {
private String buildJSONFromFields(Collection<SearchHitField> values) {
JsonFactory nodeFactory = new JsonFactory();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);) {
generator.writeStartObject();
for (SearchHitField value : values) {
if (value.getValues().size() > 1) {
......
......@@ -18,7 +18,6 @@ package com.chatopera.cc.util;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
......@@ -44,6 +43,7 @@ import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
......@@ -99,39 +99,31 @@ public class HttpClientUtil {
* @param params
* @return
* @throws IOException
*/
public static String doGet(String url, Map<String, Object> params) throws IOException {
String apiUrl = url;
StringBuffer param = new StringBuffer();
int i = 0;
for (String key : params.keySet()) {
if (i == 0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(params.get(key));
i++;
}
apiUrl += param;
String result = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpPost = new HttpGet(apiUrl);
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
result = IOUtils.toString(instream, "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(httpclient!=null) {
httpclient.close();
}
*/
public static String doGet(String url, Map<String, Object> params) throws IOException {
String apiUrl = url;
StringBuffer param = new StringBuffer();
int i = 0;
for (String key : params.keySet()) {
if (i == 0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(params.get(key));
i++;
}
return result;
apiUrl += param;
String result = null;
HttpGet httpPost = new HttpGet(apiUrl);
try (CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = httpclient.execute(httpPost);
InputStream inputStream = response.getEntity().getContent();) {
result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
......@@ -141,33 +133,24 @@ public class HttpClientUtil {
* @return
*/
public static String doPost(String apiUrl, Map<String, Object> params) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String httpStr = null;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
httpPost.setConfig(requestConfig);
List<NameValuePair> pairList = new ArrayList<>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry
.getValue().toString());
pairList.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(pairList, StandardCharsets.UTF_8));
String httpStr = null;
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);) {
try {
httpPost.setConfig(requestConfig);
List<NameValuePair> pairList = new ArrayList<>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry
.getValue().toString());
pairList.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
return httpStr;
}
......@@ -179,37 +162,24 @@ public class HttpClientUtil {
* @return
* @throws IOException
*/
public static String doPost(String apiUrl, String json) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
String httpStr = null;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
try {
public static String doPost(String apiUrl, String json) throws IOException {
httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(json,"UTF-8");//解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
if(httpClient!=null) {
httpClient.close();
}
}
return httpStr;
HttpPost httpPost = new HttpPost(apiUrl);
httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(json, "UTF-8");//解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment