Commit 56e6ff0a authored by 曲欣亮's avatar 曲欣亮

修改文档,增加新的翻译接口,修改签名算法

parent 7f199290
package com.egolm.lang;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.egolm.common.FileUtil;
import com.egolm.common.GsonUtil;
import com.egolm.common.HttpUtil;
import com.egolm.lang.config.interceptor.SigUtil;
public class ImportJson {
public static void main(String[] args) throws UnsupportedEncodingException {
String json = new String(FileUtil.fileToBytes(new File("/home/quxl/Desktop/APOS.Language.900.json")), "GBK");
Map<String, Object> map = GsonUtil.toMap(json);
for(String key : map.keySet()) {
Map<?, ?> cmap = (Map<?, ?>)map.get(key);
String Caption = (String)cmap.get("Caption");
cmap.remove("Caption");
Map<String, Object> tmp = new HashMap<String, Object>();
tmp.put("sCaption", Caption);
List<Object> ary = new ArrayList<Object>();
for(Object ckey : cmap.keySet()) {
String sText = (String)ckey;
String sTarget = null;
if(cmap.get(ckey) instanceof String) {
sTarget = (String)cmap.get(ckey);
} else {
sTarget = (String)((Map<?, ?>)cmap.get(ckey)).get("");
}
Map<String, String> doc = new HashMap<String, String>();
doc.put("sText", sText);
doc.put("sTarget", sTarget);
ary.add(doc);
}
tmp.put("sDocuments", ary);
String sContentText = GsonUtil.toJson(tmp);
String sLink = "APOS";
String appid = "44EB5B79174D45F48BF32C09E6F0C4AA";
String secret = "4117B10F6D48494595EE5D567333BB32";
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("sLink", sLink + "/" + key);
parameters.put("appid", appid);
parameters.put("sContentText", sContentText);
String sig = SigUtil.sig(parameters, secret);
parameters.put("sig", sig);
String result = HttpUtil.post("http://localhost:8090/lang/api/update", parameters);
System.out.println(result);
}
}
}
......@@ -271,7 +271,7 @@ public class LanguageController {
}
@ResponseBody
@PostMapping("translate")
@PostMapping("translateByArray")
@ApiOperation("翻译文档")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "appid", dataType = "string", required = false, value = "appid"),
......@@ -279,7 +279,7 @@ public class LanguageController {
@ApiImplicitParam(paramType = "query", name = "sLink", dataType = "string", required = true, value = "sLink"),
@ApiImplicitParam(paramType = "query", name = "sDocuments", dataType = "string", value = "翻译文档数组", allowMultiple=true)
})
public Object translate(String sLink, String[] sDocuments, String sCaption) {
public Object translateByArray(String sLink, String[] sDocuments, String sCaption) {
List<Map<String, String>> tmp = new ArrayList<Map<String, String>>();
if(sDocuments != null) {
for(String doc : sDocuments) {
......@@ -340,7 +340,53 @@ public class LanguageController {
@ApiImplicitParam(paramType = "header", name = "sig", dataType = "string", required = false, value = "sig")
})
public Object translateByJson(@RequestBody ContentArgs content) {
return this.translate(content.getsLink(), content.getsDocuments(), content.getsCaption());
return this.translateByArray(content.getsLink(), content.getsDocuments(), content.getsCaption());
}
@ResponseBody
@PostMapping("translate")
@ApiOperation("翻译文档")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "appid", dataType = "string", required = false, value = "appid"),
@ApiImplicitParam(paramType = "header", name = "sig", dataType = "string", required = false, value = "sig"),
@ApiImplicitParam(paramType = "query", name = "sLink", dataType = "string", required = false, value = "sLink"),
@ApiImplicitParam(paramType = "query", name = "sDocument", dataType = "string", required = false, value = "sDocument")
})
public Object translate(String sLink, String sDocument) {
String sNodeID = service.getNodeIDByLink(sLink, true);
Map<String, Object> nodeMap = service.getNodeMapByID(sNodeID);
String sContentText = (String)nodeMap.get("sContentText");
Map<String, Object> textMap = GsonUtil.toMap(sContentText);
String sCaption = (String)textMap.get("sCaption");
List<?> objList = (List<?>)textMap.get("sDocuments");
List<Object> list = objList == null ? new ArrayList<Object>() : new ArrayList<Object>(objList);
String dst = null;
if(list != null && list.size() > 0) {
for(Object obj : list) {
Map<?, ?> objMap = (Map<?, ?>) obj;
String sText = (String)objMap.get("sText");
String sTarget = (String)objMap.get("sTarget");
if(sDocument.equals(sText) && StringUtil.isNotBlank(sTarget)) {
dst = sTarget;
}
}
}
boolean isUpdate = false;
if(dst == null) {
dst = translate.st(sDocument, "auto", "en");
isUpdate = true;
Map<String, String> map = new HashMap<String, String>();
map.put("sText", sDocument);
map.put("sTarget", dst);
list.add(map);
}
if(isUpdate) {
Map<String, Object> tmp = new HashMap<String, Object>();
tmp.put("sCaption", sCaption);
tmp.put("sDocuments", list);
service.update(sNodeID, JSON.toJSONString(tmp, true));
}
return Rjx.jsonOk().setData(dst);
}
public LanguageService getService() {
......
......@@ -27,7 +27,7 @@ public class SwaggerConfig {
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("翻译树在线文档")
.description("翻译树在线文档,提供文档翻译缓存,在线实时翻译等服务")
.description("翻译树在线文档,提供文档翻译缓存,在线实时翻译等服务,签名规则:将全部参数按参数名的字典顺序排序,然后拼接成查询字符串,然后在字符串后面拼接SECRET的值,最后求MD5值,例如:sig=MD5(\"appid={appid}&sDocument=xxx&sLink=xxx{secret}\"), {}中的值使用实际值进行替换或填充,APPID=44EB5B79174D45F48BF32C09E6F0C4AA,SECRET=4117B10F6D48494595EE5D567333BB32")
.termsOfServiceUrl("NO terms of service")
.version("1.0")
.build();
......
package com.egolm.lang.config.interceptor;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
......@@ -11,7 +8,6 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.egolm.common.HttpUtil;
import com.egolm.common.StringUtil;
import com.egolm.lang.bean.LoginToken;
import com.egolm.lang.config.Constant;
......@@ -44,10 +40,7 @@ public class OpenInterceptor extends HandlerInterceptorAdapter {
} else if(StringUtil.isBlank(sig)) {
throw new XException("参数[sig]不能为空");
}
Map<String, String[]> parameters = new TreeMap<String, String[]>(req.getParameterMap());
parameters.remove("sig");
String queryString = HttpUtil.formatToQueryString(parameters);
String vsig = StringUtil.toSHA1String(queryString + secret);
String vsig = SigUtil.sig(req.getParameterMap(), secret);
if(vsig.toUpperCase().equals(sig.toUpperCase())) {
return super.preHandle(req, resp, handler);
} else {
......
......@@ -8,11 +8,11 @@ import com.egolm.common.StringUtil;
public class SigUtil {
public static String sig(Map<String, Object> parameters, String secret) {
Map<String, Object> parameterMap = new TreeMap<String, Object>(parameters);
public static String sig(Map<?, ?> parameters, String secret) {
Map<Object, Object> parameterMap = new TreeMap<Object, Object>(parameters);
parameterMap.remove("sig");
String queryString = HttpUtil.formatToQueryString(parameterMap);
String vsig = StringUtil.toSHA1String(queryString + secret);
String vsig = StringUtil.toMD5HexString(queryString);
return vsig;
}
......
Markdown is supported
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