Commit 790a2c30b1ea06806f87244e7e084a9eb565a0c5

Authored by 王通
1 parent 7dd529ff

1.数知梦接口需要扩展进/external

src/main/java/com/bsth/CXFConfig.java
... ... @@ -98,6 +98,8 @@ public class CXFConfig {
98 98 }
99 99  
100 100 @Autowired
  101 + private AuthorizeInterceptor_IN authorizeInterceptorIn;
  102 + @Autowired
101 103 ScheduleRealService scheduleRealService;
102 104 @Autowired
103 105 StationRestService stationRestService;
... ... @@ -166,7 +168,7 @@ public class CXFConfig {
166 168 bxRestService));
167 169 endpoint.setProviders(Arrays.asList(new JacksonJsonProvider(), new AesExceptionMapper()));
168 170 //endpoint.setFeatures(Arrays.asList(new Swagger2Feature()));
169   - endpoint.getInInterceptors().add(new AuthorizeInterceptor_IN());
  171 + endpoint.getInInterceptors().add(authorizeInterceptorIn);
170 172 return endpoint.create();
171 173 }
172 174  
... ...
src/main/java/com/bsth/server_rs/AuthorizeInterceptor_IN.java
... ... @@ -5,6 +5,7 @@ import com.bsth.entity.Resource;
5 5 import com.bsth.server_rs.exception.AesException;
6 6 import com.bsth.service.SystemParamService;
7 7 import com.bsth.service.UserService;
  8 +import com.bsth.util.ThreadLocalUtils;
8 9 import org.apache.commons.lang3.StringEscapeUtils;
9 10 import org.apache.cxf.interceptor.Fault;
10 11 import org.apache.cxf.message.Message;
... ... @@ -15,17 +16,20 @@ import org.eclipse.jetty.util.UrlEncoded;
15 16 import org.slf4j.Logger;
16 17 import org.slf4j.LoggerFactory;
17 18 import org.springframework.beans.BeansException;
  19 +import org.springframework.beans.factory.annotation.Value;
18 20 import org.springframework.context.ApplicationContext;
19 21 import org.springframework.context.ApplicationContextAware;
20 22 import org.springframework.stereotype.Component;
21 23 import org.springframework.util.AntPathMatcher;
22 24 import org.springframework.util.PathMatcher;
23 25  
  26 +import java.io.BufferedReader;
  27 +import java.io.File;
  28 +import java.io.FileReader;
  29 +import java.io.IOException;
24 30 import java.security.MessageDigest;
25   -import java.util.Arrays;
26   -import java.util.HashMap;
27   -import java.util.Map;
28   -import java.util.Set;
  31 +import java.util.*;
  32 +import java.util.concurrent.ConcurrentHashMap;
29 33  
30 34 /**
31 35 * rest 接口授权校验(IN 输入拦截)
... ... @@ -49,6 +53,15 @@ public class AuthorizeInterceptor_IN extends AbstractPhaseInterceptor<Message> i
49 53  
50 54 private static PathMatcher matcher = new AntPathMatcher();
51 55  
  56 + @Value("${path.limits}")
  57 + private String pathLimits;
  58 +
  59 + private Map<String, Set<String>> pwd2lines = new ConcurrentHashMap<>();
  60 +
  61 + private Map<String, Set<String>> pwd2device = new ConcurrentHashMap<>();
  62 +
  63 + private ThreadLocal<String> passwords = new ThreadLocal<>();
  64 +
52 65 public AuthorizeInterceptor_IN() {
53 66 super(Phase.RECEIVE);
54 67 }
... ... @@ -121,12 +134,15 @@ public class AuthorizeInterceptor_IN extends AbstractPhaseInterceptor&lt;Message&gt; i
121 134 throw new AesException(AesException.SIGN_CHECK_FAIL);
122 135 }
123 136  
  137 + ThreadLocalUtils.setPassword(map.get(PASSWORD));
124 138 validate(map, message);
125 139 }
126 140  
127   - private static void validate(Map<String, String> map, Message message) {
128   - PasswordUser user = userService.get(map.get(PASSWORD));
  141 + private void validate(Map<String, String> map, Message message) {
  142 + String password = map.get(PASSWORD);
  143 + PasswordUser user = userService.get(password);
129 144 if (user.getResources().size() > 0) {
  145 + loadLimit(password);
130 146 boolean isMatch = false;
131 147 String uri = (String) message.get(Message.REQUEST_URI);
132 148 for (Resource resource : user.getResources()) {
... ... @@ -141,6 +157,49 @@ public class AuthorizeInterceptor_IN extends AbstractPhaseInterceptor&lt;Message&gt; i
141 157 }
142 158 }
143 159  
  160 + public void loadLimit(String password) {
  161 + if (!pwd2lines.containsKey(password)) {
  162 + Set<String> set = new HashSet<>();
  163 + try {
  164 + BufferedReader reader = new BufferedReader(new FileReader(pathLimits + String.format("lines-%s.txt", password)));
  165 + String line = null;
  166 + while ((line = reader.readLine()) != null) {
  167 + set.add(line);
  168 + }
  169 + } catch (IOException e) {
  170 + logger.error("加载受限线路信息异常", e);
  171 + } finally {
  172 + pwd2lines.put(password, set);
  173 + }
  174 + }
  175 + if (!pwd2device.containsKey(password)) {
  176 + Set<String> set = new HashSet<>();
  177 + try {
  178 + BufferedReader reader = new BufferedReader(new FileReader(pathLimits + String.format("devices-%s.txt", password)));
  179 + String line = null;
  180 + while ((line = reader.readLine()) != null) {
  181 + set.add(line);
  182 + }
  183 + } catch (IOException e) {
  184 + logger.error("加载受限设备信息异常", e);
  185 + } finally {
  186 + pwd2device.put(password, set);
  187 + }
  188 + }
  189 + }
  190 +
  191 + public Set<String> getLimitLines(String password) {
  192 + return pwd2lines.get(password);
  193 + }
  194 +
  195 + public Set<String> getLimitDevices(String password) {
  196 + return pwd2device.get(password);
  197 + }
  198 +
  199 + public ThreadLocal<String> getPasswords() {
  200 + return passwords;
  201 + }
  202 +
144 203 public static Map<String, String> multi2One(MultiMap<String> params) {
145 204 Map<String, String> map = new HashMap<>();
146 205 Set<String> ks = params.keySet();
... ...
src/main/java/com/bsth/server_rs/dks/BxRestService.java
1 1 package com.bsth.server_rs.dks;
2 2  
3   -import com.bsth.common.SystemParamKeys;
  3 +import com.bsth.server_rs.AuthorizeInterceptor_IN;
4 4 import com.bsth.server_rs.base_info.car.Car;
5 5 import com.bsth.server_rs.base_info.car.buffer.CarBufferData;
6 6 import com.bsth.server_rs.base_info.carpark.Carpark;
... ... @@ -8,26 +8,24 @@ import com.bsth.server_rs.base_info.carpark.buffer.CarparkBufferData;
8 8 import com.bsth.server_rs.base_info.line.Line;
9 9 import com.bsth.server_rs.base_info.line.buffer.LineBufferData;
10 10 import com.bsth.server_rs.base_info.section.buffer.LD_SectionBufferData;
11   -import com.bsth.server_rs.base_info.section.entity.LD_Section;
12 11 import com.bsth.server_rs.base_info.section.entity.LD_SectionRoute;
13 12 import com.bsth.server_rs.base_info.station.buffer.StationBufferData;
14 13 import com.bsth.server_rs.base_info.station.entity.StationRotue;
15 14 import com.bsth.server_rs.gps.buffer.BasicDataBuffer;
16 15 import com.bsth.server_rs.gps.buffer.GpsRealDataBuffer;
  16 +import com.bsth.server_rs.gps.dao.HistoryGpsDao;
17 17 import com.bsth.server_rs.gps.entity.GpsEntity;
18   -import com.bsth.server_rs.gps.entity.LineInfo;
19   -import com.bsth.service.SystemParamService;
  18 +import com.bsth.server_rs.gps.entity.HistoryArrivalEntity;
  19 +import com.bsth.util.ThreadLocalUtils;
  20 +import org.apache.commons.lang3.StringUtils;
20 21 import org.slf4j.Logger;
21 22 import org.slf4j.LoggerFactory;
22 23 import org.springframework.beans.factory.annotation.Autowired;
23 24 import org.springframework.stereotype.Component;
24 25  
25   -import javax.ws.rs.GET;
26   -import javax.ws.rs.Path;
27   -import javax.ws.rs.Produces;
  26 +import javax.ws.rs.*;
28 27 import javax.ws.rs.core.MediaType;
29 28 import java.util.*;
30   -import java.util.concurrent.ConcurrentHashMap;
31 29  
32 30 /**
33 31 * @author Hill
... ... @@ -41,22 +39,26 @@ public class BxRestService {
41 39 private final static Logger log = LoggerFactory.getLogger(BxRestService.class);
42 40  
43 41 @Autowired
44   - private SystemParamService systemParamService;
  42 + private LD_SectionBufferData ldSectionBufferData;
45 43  
46 44 @Autowired
47   - private LD_SectionBufferData ldSectionBufferData;
  45 + private HistoryGpsDao historyGpsDao;
  46 +
  47 + @Autowired
  48 + private AuthorizeInterceptor_IN authorizeInterceptorIn;
48 49  
49 50 @GET
50 51 @Path("/line")
51 52 public List<Map<String, Object>> findAllLine() {
52 53 List<Map<String, Object>> result = new ArrayList<>();
53   - String limitLines = systemParamService.getValue(SystemParamKeys.LIMIT_LINES);
54   - if (limitLines == null) {
  54 + String password = ThreadLocalUtils.getPassword();
  55 + Set<String> limitLines = authorizeInterceptorIn.getLimitLines(password);
  56 + if (limitLines == null || limitLines.isEmpty()) {
55 57 return result;
56 58 }
57 59 for (Line line : LineBufferData.findAll()) {
58 60 String lineCode = line.getLineCode();
59   - if (limitLines.indexOf(String.format(",%s,", lineCode)) > -1) {
  61 + if (limitLines.contains(lineCode) || limitLines.contains("ALL")) {
60 62 Map<String, Object> lineMap = new HashMap<>();
61 63 Collection<LD_SectionRoute> sectionRoutes = ldSectionBufferData.findByLineCode(lineCode).get(String.format("%s_0", lineCode));
62 64 StringBuilder sb = new StringBuilder();
... ... @@ -68,6 +70,7 @@ public class BxRestService {
68 70 lineMap.put("branch", BasicDataBuffer.getBusinessByCode(String.format("%s_%s", line.getCompany(), line.getBrancheCompany())));
69 71 lineMap.put("lineName", line.getName());
70 72 lineMap.put("lineCode", line.getShanghaiLinecode());
  73 + lineMap.put("lineCodeExternal", lineCode);
71 74 lineMap.put("startStation", line.getStartStationName());
72 75 lineMap.put("endStation", line.getEndStationName());
73 76 lineMap.put("lineLength", line.getTotalMileage());
... ... @@ -87,16 +90,23 @@ public class BxRestService {
87 90 @Path("/station")
88 91 public List<Map<String, Object>> findAllStation() {
89 92 List<Map<String, Object>> result = new ArrayList<>();
90   - String limitLines = systemParamService.getValue(SystemParamKeys.LIMIT_LINES);
91   - if (limitLines == null) {
  93 + String password = ThreadLocalUtils.getPassword();
  94 + Set<String> limitLines = new HashSet<>(authorizeInterceptorIn.getLimitLines(password));
  95 + if (limitLines == null || limitLines.isEmpty()) {
92 96 return result;
93 97 }
94   - for (String lineCode : limitLines.split(",")) {
95   - if (!"".equals(lineCode)) {
  98 + if (limitLines.contains("ALL")) {
  99 + for (Line line : LineBufferData.findAll()) {
  100 + limitLines.add(line.getLineCode());
  101 + }
  102 + }
  103 + for (String lineCode : limitLines) {
  104 + if (StringUtils.isNotEmpty(lineCode)) {
96 105 Map<String, Object> lineMap = new HashMap<>();
97 106 Line line = LineBufferData.findOne(lineCode);
98 107 lineMap.put("lineName", line.getName());
99 108 lineMap.put("lineCode", line.getShanghaiLinecode());
  109 + lineMap.put("lineCodeExternal", lineCode);
100 110  
101 111 int count = 1;
102 112 List<Map<String, Object>> upStations = new ArrayList<>(), downStations = new ArrayList<>();
... ... @@ -105,7 +115,7 @@ public class BxRestService {
105 115 stopMap.put("levelNo", count);
106 116 stopMap.put("levelName", route.getStationName());
107 117 stopMap.put("levelLonlat", String.format("%f %f", route.getStation().getLon(), route.getStation().getLat()));
108   - stopMap.put("stationCode", null);
  118 + stopMap.put("stationCode", route.getStationCode());
109 119 upStations.add(stopMap);
110 120 count++;
111 121 }
... ... @@ -115,7 +125,7 @@ public class BxRestService {
115 125 stopMap.put("levelNo", count);
116 126 stopMap.put("levelName", route.getStationName());
117 127 stopMap.put("levelLonlat", String.format("%f %f", route.getStation().getLon(), route.getStation().getLat()));
118   - stopMap.put("stationCode", null);
  128 + stopMap.put("stationCode", route.getStationCode());
119 129 downStations.add(stopMap);
120 130 count++;
121 131 }
... ... @@ -132,22 +142,19 @@ public class BxRestService {
132 142 @GET
133 143 @Path("/vehicle")
134 144 public List<Map<String, Object>> findAllVehicle() {
135   - StringBuilder sb = new StringBuilder();
  145 + List<Map<String, Object>> result = new ArrayList<>();
136 146 Map<String, String> device2line = GpsRealDataBuffer.getDevice2Line();
137   - String limitLines = systemParamService.getValue(SystemParamKeys.LIMIT_LINES);
138   - for (Map.Entry<String, String> entry : device2line.entrySet()) {
139   - String deviceId = entry.getKey(), lineCode = entry.getValue();
140   - if (limitLines.indexOf(String.format(",%s,", lineCode)) > -1) {
141   - sb.append(deviceId).append(",");
142   - }
  147 + String password = ThreadLocalUtils.getPassword();
  148 + Set<String> limitLines = authorizeInterceptorIn.getLimitLines(password);
  149 + Set<String> limitDevices = authorizeInterceptorIn.getLimitDevices(password);
  150 + if (limitLines == null || limitLines.isEmpty()) {
  151 + return result;
143 152 }
144   - String limitDevices = sb.toString();
145   - List<Map<String, Object>> result = new ArrayList<>();
146   - if (limitDevices.equals("")) {
  153 + if (limitDevices == null || limitDevices.isEmpty()) {
147 154 return result;
148 155 }
149 156 for (Car car : CarBufferData.findAll()) {
150   - if (limitDevices.indexOf(String.format("%s,", car.getEquipmentCode())) > -1) {
  157 + if (limitDevices.contains(car.getEquipmentCode()) || limitDevices.contains("ALL")) {
151 158 Map<String, Object> carMap = new HashMap<>();
152 159 Line line = LineBufferData.findOne(device2line.get(car.getEquipmentCode()));
153 160 if (line == null) {
... ... @@ -155,6 +162,7 @@ public class BxRestService {
155 162 }
156 163 carMap.put("lineName", line.getName());
157 164 carMap.put("lineCode", line.getShanghaiLinecode());
  165 + carMap.put("lineCodeExternal", line.getLineCode());
158 166 carMap.put("vehicleId", car.getCarPlate());
159 167 carMap.put("vehicleNo", car.getNbbm());
160 168 carMap.put("vehicleType", car.getVehicleType());
... ... @@ -171,14 +179,15 @@ public class BxRestService {
171 179 @Path("/parking")
172 180 public List<Map<String, Object>> findAllParking() {
173 181 List<Map<String, Object>> result = new ArrayList<>();
174   - String limitLines = systemParamService.getValue(SystemParamKeys.LIMIT_LINES);
175   - if (limitLines == null) {
  182 + String password = ThreadLocalUtils.getPassword();
  183 + Set<String> limitLines = authorizeInterceptorIn.getLimitLines(password);
  184 + if (limitLines == null || limitLines.isEmpty()) {
176 185 return result;
177 186 }
178 187 StringBuilder sb = new StringBuilder();
179 188 for (Line line : LineBufferData.findAll()) {
180 189 String lineCode = line.getLineCode();
181   - if (limitLines.indexOf(String.format(",%s,", lineCode)) > -1) {
  190 + if (limitLines.contains(lineCode) || limitLines.contains("ALL")) {
182 191 sb.append(line.getCarPark()).append(",");
183 192 }
184 193 }
... ... @@ -205,13 +214,15 @@ public class BxRestService {
205 214 @Path("/gps/all")
206 215 public List<Map<String, Object>> findAllGps() {
207 216 List<Map<String, Object>> result = new ArrayList<>();
208   - String limitLines = systemParamService.getValue(SystemParamKeys.LIMIT_LINES);
209   - if (limitLines == null) {
  217 + String password = ThreadLocalUtils.getPassword();
  218 + Set<String> limitLines = authorizeInterceptorIn.getLimitLines(password);
  219 + Set<String> limitDevices = authorizeInterceptorIn.getLimitDevices(password);
  220 + if (limitLines == null || limitLines.isEmpty()) {
210 221 return result;
211 222 }
212 223 for (GpsEntity gps : GpsRealDataBuffer.all()) {
213 224 String lineCode = gps.getLineId(), deviceId = gps.getDeviceId();
214   - if (limitLines.indexOf(String.format(",%s,", lineCode)) > -1) {
  225 + if ((limitLines.contains(lineCode) || limitLines.contains("ALL")) && (limitDevices.contains(deviceId) || limitDevices.contains("ALL"))) {
215 226 Line line = LineBufferData.findOne(lineCode);
216 227 Car car = CarBufferData.findByDevice(deviceId);
217 228 if (car == null) {
... ... @@ -220,6 +231,7 @@ public class BxRestService {
220 231 Map<String, Object> gpsMap = new HashMap<>();
221 232 gpsMap.put("lineName", line.getName());
222 233 gpsMap.put("lineCode", line.getShanghaiLinecode());
  234 + gpsMap.put("lineCodeExternal", line.getLineCode());
223 235 gpsMap.put("vehicleId", car.getCarPlate());
224 236 gpsMap.put("gpsTime", gps.getTimestamp());
225 237 gpsMap.put("gpsLonlat", String.format("%f %f", gps.getLon(), gps.getLat()));
... ... @@ -231,4 +243,39 @@ public class BxRestService {
231 243  
232 244 return result;
233 245 }
  246 + @GET
  247 + @Path("/history-arrival/{line}/{st}/{et}")
  248 + public List<HistoryArrivalEntity> historyArrival(@PathParam("line") String line, @PathParam("st") String st, @PathParam("et") String et) {
  249 + List<HistoryArrivalEntity> result = new ArrayList<>();
  250 + String password = ThreadLocalUtils.getPassword();
  251 + Set<String> limitLines = authorizeInterceptorIn.getLimitLines(password);
  252 + Set<String> limitDevices = authorizeInterceptorIn.getLimitDevices(password);
  253 + if (limitLines == null || limitLines.isEmpty() || !limitLines.contains(line)) {
  254 + return result;
  255 + }
  256 + if (limitDevices == null || limitDevices.isEmpty()) {
  257 + return result;
  258 + }
  259 + List<HistoryArrivalEntity> arrivals = historyGpsDao.queryArrival(line, Long.parseLong(st), Long.parseLong(et));
  260 + for (HistoryArrivalEntity arrival : arrivals) {
  261 + String lineCode = String.valueOf(arrival.getLineId()), deviceId = arrival.getDeviceId();
  262 + Line line1 = LineBufferData.findOne(lineCode);
  263 + if (line1 == null) {
  264 + continue;
  265 + }
  266 + arrival.setLineName(line1.getName());
  267 + if ((limitLines.contains(lineCode) || limitLines.contains("ALL")) && (limitDevices.contains(deviceId) || limitDevices.contains("ALL"))) {
  268 + result.add(arrival);
  269 + }
  270 + }
  271 +
  272 + return result;
  273 + }
  274 +
  275 + @GET
  276 + @Path("/limits/reload")
  277 + public void reload() {
  278 + String password = ThreadLocalUtils.getPassword();
  279 + authorizeInterceptorIn.loadLimit(password);
  280 + }
234 281 }
... ...
src/main/java/com/bsth/server_rs/gps/entity/HistoryArrivalEntity.java
... ... @@ -6,6 +6,8 @@ public class HistoryArrivalEntity {
6 6  
7 7 private int lineId;
8 8  
  9 + private String lineName;
  10 +
9 11 private String stopNo;
10 12  
11 13 private int upDown;
... ... @@ -30,6 +32,14 @@ public class HistoryArrivalEntity {
30 32 this.lineId = lineId;
31 33 }
32 34  
  35 + public String getLineName() {
  36 + return lineName;
  37 + }
  38 +
  39 + public void setLineName(String lineName) {
  40 + this.lineName = lineName;
  41 + }
  42 +
33 43 public String getStopNo() {
34 44 return stopNo;
35 45 }
... ...
src/main/resources/application.properties
... ... @@ -15,3 +15,6 @@ server.compression.mime-types=application/json,application/xml,text/html,text/xm
15 15  
16 16 #redis
17 17 cache.days=15
  18 +
  19 +#limits
  20 +path.limits=limits/
... ...