Commit 75ddc67838db0d83e56d6da30f4de880cd8ed782
1 parent
4693f0a0
update...
Showing
15 changed files
with
430 additions
and
30 deletions
pom.xml
| ... | ... | @@ -133,6 +133,18 @@ |
| 133 | 133 | <artifactId>axis</artifactId> |
| 134 | 134 | <version>1.4</version> |
| 135 | 135 | </dependency> |
| 136 | + | |
| 137 | + <dependency> | |
| 138 | + <groupId>com.fasterxml.jackson.jaxrs</groupId> | |
| 139 | + <artifactId>jackson-jaxrs-json-provider</artifactId> | |
| 140 | + <version>2.8.7</version> | |
| 141 | + </dependency> | |
| 142 | + | |
| 143 | + <dependency> | |
| 144 | + <groupId>org.eclipse.jetty</groupId> | |
| 145 | + <artifactId>jetty-util</artifactId> | |
| 146 | + <version>9.3.8.v20160314</version> | |
| 147 | + </dependency> | |
| 136 | 148 | </dependencies> |
| 137 | 149 | |
| 138 | 150 | <build> | ... | ... |
src/main/java/com/bsth/CXFConfig.java
| 1 | 1 | package com.bsth; |
| 2 | 2 | |
| 3 | +import com.bsth.server_rs.AuthorizeInterceptor_IN; | |
| 4 | +import com.bsth.server_rs.exception.AesExceptionMapper; | |
| 5 | +import com.bsth.server_rs.base_info.line.LineServiceImpl; | |
| 3 | 6 | import com.bsth.server_ws.attendance.AttendanceServiceSoap; |
| 4 | 7 | import com.bsth.server_ws.park_station.CompanyServiceSoap; |
| 5 | 8 | import com.bsth.server_ws.waybill.LD_ServiceSoap; |
| 9 | +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; | |
| 6 | 10 | import org.apache.cxf.Bus; |
| 7 | 11 | import org.apache.cxf.bus.spring.SpringBus; |
| 12 | +import org.apache.cxf.endpoint.Server; | |
| 13 | +import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; | |
| 8 | 14 | import org.apache.cxf.jaxws.EndpointImpl; |
| 9 | 15 | import org.apache.cxf.transport.servlet.CXFServlet; |
| 10 | 16 | import org.springframework.boot.web.servlet.ServletRegistrationBean; |
| ... | ... | @@ -13,6 +19,7 @@ import org.springframework.context.annotation.Configuration; |
| 13 | 19 | import org.springframework.core.Ordered; |
| 14 | 20 | |
| 15 | 21 | import javax.xml.ws.Endpoint; |
| 22 | +import java.util.Arrays; | |
| 16 | 23 | |
| 17 | 24 | /** |
| 18 | 25 | * Created by panzhao on 2017/3/9. |
| ... | ... | @@ -25,21 +32,9 @@ public class CXFConfig { |
| 25 | 32 | return new SpringBus(); |
| 26 | 33 | } |
| 27 | 34 | |
| 28 | - public CompanyServiceSoap companyService(){ | |
| 29 | - return new CompanyServiceSoap(); | |
| 30 | - } | |
| 31 | - | |
| 32 | - public LD_ServiceSoap ldServiceSoap(){ | |
| 33 | - return new LD_ServiceSoap(); | |
| 34 | - } | |
| 35 | - | |
| 36 | - public AttendanceServiceSoap attendanceService(){ | |
| 37 | - return new AttendanceServiceSoap(); | |
| 38 | - } | |
| 39 | - | |
| 40 | 35 | @Bean |
| 41 | 36 | public Endpoint companyEndpoint() { |
| 42 | - EndpointImpl endpoint = new EndpointImpl(springBus(), companyService()); | |
| 37 | + EndpointImpl endpoint = new EndpointImpl(springBus(), new CompanyServiceSoap()); | |
| 43 | 38 | endpoint.publish("/CompanyService"); |
| 44 | 39 | //endpoint.getInInterceptors().add(new AuthInterceptor()); |
| 45 | 40 | return endpoint; |
| ... | ... | @@ -47,7 +42,7 @@ public class CXFConfig { |
| 47 | 42 | |
| 48 | 43 | @Bean |
| 49 | 44 | public Endpoint ldServiceEndpoint() { |
| 50 | - EndpointImpl endpoint = new EndpointImpl(springBus(), ldServiceSoap()); | |
| 45 | + EndpointImpl endpoint = new EndpointImpl(springBus(), new LD_ServiceSoap()); | |
| 51 | 46 | endpoint.publish("/LD_Service"); |
| 52 | 47 | //endpoint.getInInterceptors().add(new AuthInterceptor()); |
| 53 | 48 | return endpoint; |
| ... | ... | @@ -55,12 +50,33 @@ public class CXFConfig { |
| 55 | 50 | |
| 56 | 51 | @Bean |
| 57 | 52 | public Endpoint attendanceServiceEndpoint() { |
| 58 | - EndpointImpl endpoint = new EndpointImpl(springBus(), attendanceService()); | |
| 53 | + EndpointImpl endpoint = new EndpointImpl(springBus(), new AttendanceServiceSoap()); | |
| 59 | 54 | endpoint.publish("/attendanceService"); |
| 60 | 55 | //endpoint.getInInterceptors().add(new AuthInterceptor()); |
| 61 | 56 | return endpoint; |
| 62 | 57 | } |
| 63 | 58 | |
| 59 | + | |
| 60 | +/* @Bean | |
| 61 | + public Endpoint LineServiceEndpoint(){ | |
| 62 | + EndpointImpl endpoint = new EndpointImpl(springBus(), lineService()); | |
| 63 | + endpoint.publish("/data_line"); | |
| 64 | + //endpoint.getInInterceptors().add(new AuthInterceptor()); | |
| 65 | + return endpoint; | |
| 66 | + }*/ | |
| 67 | + | |
| 68 | + @Bean | |
| 69 | + public Server rsServer() { | |
| 70 | + JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean(); | |
| 71 | + endpoint.setBus(springBus()); | |
| 72 | + endpoint.setAddress("/rest"); | |
| 73 | + endpoint.setServiceBeans(Arrays.<Object>asList(new LineServiceImpl())); | |
| 74 | + endpoint.setProviders(Arrays.asList(new JacksonJsonProvider(), new AesExceptionMapper())); | |
| 75 | + //endpoint.setFeatures(Arrays.asList(new Swagger2Feature())); | |
| 76 | + endpoint.getInInterceptors().add(new AuthorizeInterceptor_IN()); | |
| 77 | + return endpoint.create(); | |
| 78 | + } | |
| 79 | + | |
| 64 | 80 | @Bean |
| 65 | 81 | public ServletRegistrationBean servletRegistrationBean() { |
| 66 | 82 | ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/webservice/*"); | ... | ... |
src/main/java/com/bsth/server_rs/AuthorizeInterceptor_IN.java
0 → 100644
| 1 | +package com.bsth.server_rs; | |
| 2 | + | |
| 3 | +import com.bsth.server_rs.exception.AesException; | |
| 4 | +import com.bsth.service.UserService; | |
| 5 | +import org.apache.commons.lang3.StringEscapeUtils; | |
| 6 | +import org.apache.cxf.interceptor.Fault; | |
| 7 | +import org.apache.cxf.message.Message; | |
| 8 | +import org.apache.cxf.phase.AbstractPhaseInterceptor; | |
| 9 | +import org.apache.cxf.phase.Phase; | |
| 10 | +import org.eclipse.jetty.util.MultiMap; | |
| 11 | +import org.eclipse.jetty.util.UrlEncoded; | |
| 12 | +import org.slf4j.Logger; | |
| 13 | +import org.slf4j.LoggerFactory; | |
| 14 | +import org.springframework.beans.BeansException; | |
| 15 | +import org.springframework.context.ApplicationContext; | |
| 16 | +import org.springframework.context.ApplicationContextAware; | |
| 17 | +import org.springframework.stereotype.Component; | |
| 18 | + | |
| 19 | +import java.security.MessageDigest; | |
| 20 | +import java.util.Arrays; | |
| 21 | +import java.util.HashMap; | |
| 22 | +import java.util.Map; | |
| 23 | +import java.util.Set; | |
| 24 | + | |
| 25 | +/** | |
| 26 | + * rest 接口授权校验(IN 输入拦截) | |
| 27 | + * Created by panzhao on 2017/3/28. | |
| 28 | + */ | |
| 29 | +@Component | |
| 30 | +public class AuthorizeInterceptor_IN extends AbstractPhaseInterceptor<Message> implements ApplicationContextAware { | |
| 31 | + | |
| 32 | + private static final String SIGN = "sign"; | |
| 33 | + private static final String TIMESTAMP = "timestamp"; | |
| 34 | + private static final String NONCE = "nonce"; | |
| 35 | + private static final String PASSWORD = "password"; | |
| 36 | + | |
| 37 | + static UserService userService; | |
| 38 | + | |
| 39 | + static Logger logger = LoggerFactory.getLogger(AuthorizeInterceptor_IN.class); | |
| 40 | + | |
| 41 | + public AuthorizeInterceptor_IN() { | |
| 42 | + super(Phase.RECEIVE); | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + public void handleMessage(Message message) throws Fault { | |
| 47 | + | |
| 48 | + if (message.get(Message.QUERY_STRING) == null) { | |
| 49 | + throw new AesException(AesException.MISS_SIGN); | |
| 50 | + } | |
| 51 | + | |
| 52 | + //放行wadl | |
| 53 | + if(message.get(Message.QUERY_STRING).equals("_wadl") | |
| 54 | + && message.get(Message.PATH_INFO).equals("/webservice/rest")){ | |
| 55 | + return ; | |
| 56 | + } | |
| 57 | + | |
| 58 | + //获取参数,不包括 url 路径参数 ?号之后的 | |
| 59 | + String queryString = StringEscapeUtils.unescapeHtml4(message.get(Message.QUERY_STRING).toString()); | |
| 60 | + MultiMap<String> params = new MultiMap<>(); | |
| 61 | + UrlEncoded.decodeTo(queryString, params, "utf-8"); | |
| 62 | + Map<String, String> map = multi2One(params); | |
| 63 | + | |
| 64 | + if (!map.containsKey(SIGN)) { | |
| 65 | + throw new AesException(AesException.MISS_SIGN); | |
| 66 | + } | |
| 67 | + if (!map.containsKey(TIMESTAMP)) { | |
| 68 | + throw new AesException(AesException.MISS_TIMESTAMP); | |
| 69 | + } | |
| 70 | + if (!map.containsKey(NONCE)) { | |
| 71 | + throw new AesException(AesException.MISS_NONCE); | |
| 72 | + } | |
| 73 | + if (!map.containsKey(PASSWORD)) { | |
| 74 | + throw new AesException(AesException.MISS_PWD); | |
| 75 | + } | |
| 76 | + | |
| 77 | + if (userService.get(map.get(PASSWORD)) == null) { | |
| 78 | + throw new AesException(AesException.INVALID_PWD); | |
| 79 | + } | |
| 80 | + | |
| 81 | + String sign = map.get(SIGN); | |
| 82 | + map.remove(SIGN); | |
| 83 | + String sh1 = ""; | |
| 84 | + try { | |
| 85 | + sh1 = getSHA1(map); | |
| 86 | + } catch (Exception e) { | |
| 87 | + throw new AesException(AesException.SIGN_CHECK_ERROR); | |
| 88 | + } | |
| 89 | + | |
| 90 | + if (!sign.equals(sh1)) { | |
| 91 | + throw new AesException(AesException.SIGN_CHECK_FAIL); | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + public static Map<String, String> multi2One(MultiMap<String> params) { | |
| 96 | + Map<String, String> map = new HashMap<>(); | |
| 97 | + Set<String> ks = params.keySet(); | |
| 98 | + for (String k : ks) { | |
| 99 | + map.put(k, params.getString(k)); | |
| 100 | + } | |
| 101 | + return map; | |
| 102 | + } | |
| 103 | + | |
| 104 | + | |
| 105 | + public static String getSHA1(Map<String, String> map) throws Exception { | |
| 106 | + | |
| 107 | + try { | |
| 108 | + String[] array = new String[map.size()]; | |
| 109 | + map.values().toArray(array); | |
| 110 | + StringBuffer sb = new StringBuffer(); | |
| 111 | + | |
| 112 | + // 字符串排序 | |
| 113 | + Arrays.sort(array); | |
| 114 | + for (int i = 0; i < array.length; i++) { | |
| 115 | + sb.append(array[i]); | |
| 116 | + } | |
| 117 | + String str = sb.toString(); | |
| 118 | + // SHA1签名生成 | |
| 119 | + MessageDigest md = MessageDigest.getInstance("SHA-1"); | |
| 120 | + md.update(str.getBytes()); | |
| 121 | + byte[] digest = md.digest(); | |
| 122 | + | |
| 123 | + StringBuffer hexstr = new StringBuffer(); | |
| 124 | + String shaHex = ""; | |
| 125 | + for (int i = 0; i < digest.length; i++) { | |
| 126 | + shaHex = Integer.toHexString(digest[i] & 0xFF); | |
| 127 | + if (shaHex.length() < 2) { | |
| 128 | + hexstr.append(0); | |
| 129 | + } | |
| 130 | + hexstr.append(shaHex); | |
| 131 | + } | |
| 132 | + return hexstr.toString(); | |
| 133 | + } catch (Exception e) { | |
| 134 | + logger.error("", e); | |
| 135 | + throw e; | |
| 136 | + } | |
| 137 | + } | |
| 138 | + | |
| 139 | + @Override | |
| 140 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | |
| 141 | + userService = applicationContext.getBean(UserService.class); | |
| 142 | + } | |
| 143 | +} | ... | ... |
src/main/java/com/bsth/server_rs/adapter/DateAdapter.java
0 → 100644
| 1 | +package com.bsth.server_rs.adapter; | |
| 2 | + | |
| 3 | +import org.joda.time.format.DateTimeFormat; | |
| 4 | +import org.joda.time.format.DateTimeFormatter; | |
| 5 | + | |
| 6 | +import javax.xml.bind.annotation.adapters.XmlAdapter; | |
| 7 | +import java.util.Date; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 时间格式处理 yyyy-MM-dd HH:mm:ss | |
| 11 | + * Created by panzhao on 2017/3/28. | |
| 12 | + */ | |
| 13 | +public class DateAdapter extends XmlAdapter<String, Date> { | |
| 14 | + | |
| 15 | + private static DateTimeFormatter fmtyyyyMMddHHmmss = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); | |
| 16 | + | |
| 17 | + @Override | |
| 18 | + public Date unmarshal(String v) throws Exception { | |
| 19 | + return fmtyyyyMMddHHmmss.parseDateTime(v).toDate(); | |
| 20 | + } | |
| 21 | + | |
| 22 | + @Override | |
| 23 | + public String marshal(Date v) throws Exception { | |
| 24 | + return fmtyyyyMMddHHmmss.print(v.getTime()); | |
| 25 | + } | |
| 26 | +} | ... | ... |
src/main/java/com/bsth/server_rs/base_info/car/CarService.java
0 → 100644
src/main/java/com/bsth/server_rs/base_info/line/Line.java
| 1 | 1 | package com.bsth.server_rs.base_info.line; |
| 2 | 2 | |
| 3 | +import com.bsth.server_rs.adapter.DateAdapter; | |
| 4 | + | |
| 5 | +import javax.xml.bind.annotation.XmlRootElement; | |
| 6 | +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | |
| 3 | 7 | import java.io.Serializable; |
| 4 | 8 | import java.util.Date; |
| 5 | 9 | |
| ... | ... | @@ -11,7 +15,7 @@ import java.util.Date; |
| 11 | 15 | * @Date 2016-4-11 16:06:17 |
| 12 | 16 | * @Version 公交调度系统BS版 0.1 |
| 13 | 17 | */ |
| 14 | - | |
| 18 | +@XmlRootElement | |
| 15 | 19 | public class Line implements Serializable { |
| 16 | 20 | |
| 17 | 21 | /** |
| ... | ... | @@ -411,6 +415,7 @@ public class Line implements Serializable { |
| 411 | 415 | this.descriptions = descriptions; |
| 412 | 416 | } |
| 413 | 417 | |
| 418 | + @XmlJavaTypeAdapter(DateAdapter.class) | |
| 414 | 419 | public Date getCreateDate() { |
| 415 | 420 | return createDate; |
| 416 | 421 | } | ... | ... |
src/main/java/com/bsth/server_rs/base_info/line/LineService.java deleted
100644 → 0
src/main/java/com/bsth/server_rs/base_info/line/LineServiceImpl.java
0 → 100644
| 1 | +package com.bsth.server_rs.base_info.line; | |
| 2 | + | |
| 3 | +import com.bsth.server_rs.base_info.line.buffer.LineBufferData; | |
| 4 | + | |
| 5 | +import javax.ws.rs.GET; | |
| 6 | +import javax.ws.rs.Path; | |
| 7 | +import javax.ws.rs.PathParam; | |
| 8 | +import javax.ws.rs.Produces; | |
| 9 | +import javax.ws.rs.core.MediaType; | |
| 10 | +import java.util.List; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * 线路 Rest Service | |
| 14 | + * Created by panzhao on 2017/3/27. | |
| 15 | + */ | |
| 16 | +@Path("/line") | |
| 17 | +@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) | |
| 18 | +public class LineServiceImpl { | |
| 19 | + | |
| 20 | + @GET | |
| 21 | + @Path("/company/{companyId}") | |
| 22 | + public List<Line> findAll(@PathParam("companyId") String companyId) { | |
| 23 | + return companyId.equals("-9999") ? LineBufferData.findAll() : LineBufferData.findByCompany(companyId); | |
| 24 | + } | |
| 25 | + | |
| 26 | + @GET | |
| 27 | + @Path("/{id}") | |
| 28 | + public Line findOne(@PathParam("id") Integer id) { | |
| 29 | + return LineBufferData.findOne(id); | |
| 30 | + } | |
| 31 | +} | ... | ... |
src/main/java/com/bsth/server_rs/base_info/line/buffer/LineBufferData.java
| 1 | 1 | package com.bsth.server_rs.base_info.line.buffer; |
| 2 | 2 | |
| 3 | +import com.bsth.Application; | |
| 3 | 4 | import com.bsth.server_rs.base_info.line.Line; |
| 5 | +import com.google.common.collect.ArrayListMultimap; | |
| 4 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 5 | 7 | import org.springframework.boot.CommandLineRunner; |
| 6 | 8 | import org.springframework.core.annotation.Order; |
| ... | ... | @@ -9,13 +11,14 @@ import org.springframework.stereotype.Component; |
| 9 | 11 | import java.util.HashMap; |
| 10 | 12 | import java.util.List; |
| 11 | 13 | import java.util.Map; |
| 14 | +import java.util.concurrent.TimeUnit; | |
| 12 | 15 | |
| 13 | 16 | /** |
| 14 | 17 | * 线路数据缓存(自更新) |
| 15 | 18 | * Created by panzhao on 2017/3/27. |
| 16 | 19 | */ |
| 17 | 20 | @Component |
| 18 | -@Order(10) | |
| 21 | +@Order(5) | |
| 19 | 22 | public class LineBufferData implements CommandLineRunner { |
| 20 | 23 | |
| 21 | 24 | @Autowired |
| ... | ... | @@ -23,6 +26,7 @@ public class LineBufferData implements CommandLineRunner { |
| 23 | 26 | |
| 24 | 27 | private static List<Line> data; |
| 25 | 28 | private static Map<Integer, Line> idMap; |
| 29 | + private static ArrayListMultimap<String, Line> companyListMap; | |
| 26 | 30 | |
| 27 | 31 | public static List<Line> findAll(){ |
| 28 | 32 | return data; |
| ... | ... | @@ -32,17 +36,26 @@ public class LineBufferData implements CommandLineRunner { |
| 32 | 36 | return idMap.get(id); |
| 33 | 37 | } |
| 34 | 38 | |
| 39 | + public static List<Line> findByCompany(String company){ | |
| 40 | + return companyListMap.get(company); | |
| 41 | + } | |
| 42 | + | |
| 35 | 43 | public static void replaceAll(List<Line> newData){ |
| 36 | 44 | data = newData; |
| 37 | 45 | Map<Integer, Line> idMapCopy = new HashMap<>(); |
| 46 | + ArrayListMultimap<String, Line> listMap = ArrayListMultimap.create(); | |
| 47 | + | |
| 38 | 48 | for(Line line : data){ |
| 39 | 49 | idMapCopy.put(line.getId(), line); |
| 50 | + listMap.put(line.getCompany(), line); | |
| 40 | 51 | } |
| 41 | 52 | idMap = idMapCopy; |
| 53 | + | |
| 54 | + companyListMap = listMap; | |
| 42 | 55 | } |
| 43 | 56 | |
| 44 | 57 | @Override |
| 45 | 58 | public void run(String... strings) throws Exception { |
| 46 | - //Application.mainServices.scheduleWithFixedDelay(lineRefreshThread, 10, 60 * 60, TimeUnit.SECONDS); | |
| 59 | + Application.mainServices.scheduleWithFixedDelay(lineRefreshThread, 10, 60 * 60, TimeUnit.SECONDS); | |
| 47 | 60 | } |
| 48 | 61 | } | ... | ... |
src/main/java/com/bsth/server_rs/base_info/line/buffer/LineRefreshThread.java
| 1 | 1 | package com.bsth.server_rs.base_info.line.buffer; |
| 2 | 2 | |
| 3 | 3 | import com.bsth.server_rs.base_info.line.Line; |
| 4 | +import org.slf4j.Logger; | |
| 5 | +import org.slf4j.LoggerFactory; | |
| 4 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.jdbc.core.BeanPropertyRowMapper; | |
| 5 | 8 | import org.springframework.jdbc.core.JdbcTemplate; |
| 6 | 9 | import org.springframework.stereotype.Component; |
| 7 | 10 | |
| ... | ... | @@ -16,11 +19,17 @@ public class LineRefreshThread extends Thread{ |
| 16 | 19 | @Autowired |
| 17 | 20 | JdbcTemplate jdbcTemplate; |
| 18 | 21 | |
| 22 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 23 | + | |
| 19 | 24 | @Override |
| 20 | 25 | public void run() { |
| 21 | 26 | |
| 22 | - List<Line> list = jdbcTemplate.queryForList("select * from bsth_c_line", Line.class); | |
| 23 | - if(list != null && list.size() > 0) | |
| 24 | - LineBufferData.replaceAll(list); | |
| 27 | + try { | |
| 28 | + List<Line> list = jdbcTemplate.query("select * from bsth_c_line",BeanPropertyRowMapper.newInstance(Line.class)); | |
| 29 | + if(list != null && list.size() > 0) | |
| 30 | + LineBufferData.replaceAll(list); | |
| 31 | + }catch (Exception e){ | |
| 32 | + logger.error("", e); | |
| 33 | + } | |
| 25 | 34 | } |
| 26 | 35 | } | ... | ... |
src/main/java/com/bsth/server_rs/base_info/person/PersonService.java
0 → 100644
src/main/java/com/bsth/server_rs/exception/AesException.java
0 → 100644
| 1 | +package com.bsth.server_rs.exception; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Created by panzhao on 2017/3/28. | |
| 5 | + */ | |
| 6 | +public class AesException extends RuntimeException { | |
| 7 | + | |
| 8 | + public final static int OK = 0; | |
| 9 | + public final static int MISS_SIGN = -30001; | |
| 10 | + public final static int MISS_TIMESTAMP = -30002; | |
| 11 | + public final static int MISS_NONCE = -30003; | |
| 12 | + public final static int MISS_PWD = -30004; | |
| 13 | + public final static int SIGN_CHECK_ERROR = -40001; | |
| 14 | + public final static int SIGN_CHECK_FAIL = -40002; | |
| 15 | + public final static int INVALID_PWD = -40003; | |
| 16 | + | |
| 17 | + private int code; | |
| 18 | + | |
| 19 | + private static String getMessage(int code) { | |
| 20 | + switch (code) { | |
| 21 | + case MISS_SIGN: | |
| 22 | + return "sign参数丢失"; | |
| 23 | + case MISS_TIMESTAMP: | |
| 24 | + return "timestamp参数丢失"; | |
| 25 | + case MISS_NONCE: | |
| 26 | + return "nonce参数丢失"; | |
| 27 | + case MISS_PWD: | |
| 28 | + return "密码参数丢失"; | |
| 29 | + case INVALID_PWD: | |
| 30 | + return "无效的密码"; | |
| 31 | + case SIGN_CHECK_ERROR: | |
| 32 | + return "签名校验时出现异常"; | |
| 33 | + case SIGN_CHECK_FAIL: | |
| 34 | + return "签名校验失败"; | |
| 35 | + default: | |
| 36 | + return null; | |
| 37 | + } | |
| 38 | + } | |
| 39 | + | |
| 40 | + public int getCode() { | |
| 41 | + return code; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public AesException(int code) { | |
| 45 | + super(getMessage(code)); | |
| 46 | + this.code = code; | |
| 47 | + } | |
| 48 | +} | ... | ... |
src/main/java/com/bsth/server_rs/exception/AesExceptionMapper.java
0 → 100644
| 1 | +package com.bsth.server_rs.exception; | |
| 2 | + | |
| 3 | +import org.apache.cxf.jaxrs.ext.MessageContext; | |
| 4 | + | |
| 5 | +import javax.ws.rs.core.Context; | |
| 6 | +import javax.ws.rs.core.Response; | |
| 7 | +import javax.ws.rs.ext.ExceptionMapper; | |
| 8 | +import java.util.Locale; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * cxf rest 异常处理 | |
| 12 | + * Created by panzhao on 2017/3/28. | |
| 13 | + */ | |
| 14 | +public class AesExceptionMapper implements ExceptionMapper<AesException> { | |
| 15 | + | |
| 16 | + @Context | |
| 17 | + private MessageContext mc; | |
| 18 | + | |
| 19 | + @Override | |
| 20 | + public Response toResponse(AesException e) { | |
| 21 | + StackTraceElement[] trace = new StackTraceElement[1]; | |
| 22 | + trace[0] = e.getStackTrace()[0]; | |
| 23 | + e.setStackTrace(trace); | |
| 24 | + | |
| 25 | + Response.ResponseBuilder rb = Response.status(Response.Status.INTERNAL_SERVER_ERROR); | |
| 26 | + ExceptionEntity entity = new ExceptionEntity(e.getCode() + "", e.getMessage()); | |
| 27 | + rb.entity(entity); | |
| 28 | + | |
| 29 | + String accept = mc.getHttpServletRequest().getHeader("Accept"); | |
| 30 | + rb.type(accept); | |
| 31 | + rb.language(Locale.SIMPLIFIED_CHINESE); | |
| 32 | + return rb.build(); | |
| 33 | + } | |
| 34 | +} | ... | ... |
src/main/java/com/bsth/server_rs/exception/ExceptionEntity.java
0 → 100644
| 1 | +package com.bsth.server_rs.exception; | |
| 2 | + | |
| 3 | +import javax.xml.bind.annotation.XmlRootElement; | |
| 4 | +import java.io.Serializable; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Created by panzhao on 2017/3/28. | |
| 8 | + */ | |
| 9 | +@XmlRootElement | |
| 10 | +public class ExceptionEntity implements Serializable { | |
| 11 | + | |
| 12 | + /*错误码,默认为0*/ | |
| 13 | + private int errCode = 0; | |
| 14 | + | |
| 15 | + /*错误子码,自定义该值*/ | |
| 16 | + private String errSubCode; | |
| 17 | + | |
| 18 | + private String message; | |
| 19 | + | |
| 20 | + public ExceptionEntity(String errSubCode, String message) { | |
| 21 | + super(); | |
| 22 | + this.errSubCode = errSubCode; | |
| 23 | + this.message = message; | |
| 24 | + } | |
| 25 | + | |
| 26 | + public ExceptionEntity(){} | |
| 27 | + | |
| 28 | + public int getErrCode() { | |
| 29 | + return errCode; | |
| 30 | + } | |
| 31 | + | |
| 32 | + public void setErrCode(int errCode) { | |
| 33 | + this.errCode = errCode; | |
| 34 | + } | |
| 35 | + | |
| 36 | + public String getErrSubCode() { | |
| 37 | + return errSubCode; | |
| 38 | + } | |
| 39 | + | |
| 40 | + public void setErrSubCode(String errSubCode) { | |
| 41 | + this.errSubCode = errSubCode; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public String getMessage() { | |
| 45 | + return message; | |
| 46 | + } | |
| 47 | + | |
| 48 | + public void setMessage(String message) { | |
| 49 | + this.message = message; | |
| 50 | + } | |
| 51 | +} | ... | ... |