changed project structure, added models, logics, service, requests and responses

This commit is contained in:
strawmanbobi
2019-06-08 12:02:59 +08:00
parent 3011291070
commit 85145090db
30 changed files with 1193 additions and 26 deletions

View File

@@ -97,5 +97,7 @@
<orderEntry type="library" scope="RUNTIME" name="Maven: org.jetbrains.kotlin:kotlin-stdlib:1.2.71" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.6.2" level="project" />
<orderEntry type="library" name="Maven: javax.ws.rs:javax.ws.rs-api:2.0.1" level="project" />
</component>
</module>

12
pom.xml
View File

@@ -9,7 +9,7 @@
<packaging>jar</packaging>
<name>IRext Private Server</name>
<description>Decoding IR binaries online</description>
<description>IRext Private Server</description>
<parent>
<groupId>org.springframework.boot</groupId>
@@ -80,6 +80,16 @@
<artifactId>okio</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,62 @@
package net.irext.server.redis.model;
import java.io.Serializable;
/**
* Filename: CachedAdmin.java
* Revised: Date: 2017-04-27
* Revision: Revision: 1.0
* <p>
* Description: CachedAdmin DAO for redis
* <p>
* Revision log:
* 2017-04-27: created by strawmanbobi
*/
public class CachedAdmin implements Serializable {
private static final long serialVersionUID = 1L;
private static final String OBJECT_KEY = "ADMIN";
private String id;
private String token;
public CachedAdmin() {
}
public CachedAdmin(String id) {
}
public CachedAdmin(String id, String token) {
this.id = id;
this.token = token;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String toString() {
return "CachedAdmin [id=" + id + ", token=" + token + "]";
}
public String getKey() {
return getId();
}
public String getObjectKey() {
return OBJECT_KEY;
}
}

View File

@@ -0,0 +1,66 @@
package net.irext.server.redis.model;
import java.io.Serializable;
/**
* Filename: CachedBinary.java
* Revised: Date: 2017-05-16
* Revision: Revision: 1.0
* <p>
* Description: IR binary cache for performance optimization on decoding
* <p>
* Revision log:
* 2017-05-16: created by strawmanbobi
*/
public class CachedBinary implements Serializable {
private static final long serialVersionUID = 1L;
private static final String OBJECT_KEY = "BINARY";
private int id;
private byte[] binaries;
public CachedBinary() {
}
public CachedBinary(String id) {
}
public CachedBinary(int id, byte[] binaries) {
this.id = id;
this.binaries = binaries;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public byte[] getBinaries() {
return binaries;
}
public void setBinaries(byte[] binaries) {
this.binaries = binaries;
}
@Override
public String toString() {
return "CachedBinary{" +
"id='" + id + '\'' +
", binaries=" + binaries.length +
'}';
}
public int getKey() {
return getId();
}
public String getObjectKey() {
return OBJECT_KEY;
}
}

View File

@@ -0,0 +1,50 @@
package net.irext.server.redis.service;
import net.irext.server.redis.model.CachedAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
/**
* Filename: AdminService.java
* Revised: Date: 2017-04-27
* Revision: Revision: 1.0
* <p>
* Description: CachedAdmin redis service
* <p>
* Revision log:
* 2017-04-27: created by strawmanbobi
*/
@Repository
public class AdminService {
@Autowired
RedisTemplate<String, CachedAdmin> redisTemplate;
public RedisTemplate<String, CachedAdmin> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, CachedAdmin> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void put(CachedAdmin cachedAdmin) {
ValueOperations<String, CachedAdmin> valueOper = redisTemplate.opsForValue();
valueOper.set(cachedAdmin.getId(), cachedAdmin);
}
public void delete(String id) {
ValueOperations<String, CachedAdmin> valueOper = redisTemplate.opsForValue();
RedisOperations<String, CachedAdmin> RedisOperations = valueOper.getOperations();
RedisOperations.delete(id);
}
public CachedAdmin get(String id) {
ValueOperations<String, CachedAdmin> valueOper = redisTemplate.opsForValue();
return valueOper.get(id);
}
}

View File

@@ -0,0 +1,50 @@
package net.irext.server.redis.service;
import net.irext.server.redis.model.CachedBinary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
/**
* Filename: BinaryService.java
* Revised: Date: 2017-04-27
* Revision: Revision: 1.0
* <p>
* Description: CachedBinary redis service
* <p>
* Revision log:
* 2017-04-27: created by strawmanbobi
*/
@Repository
public class BinaryService {
@Autowired
RedisTemplate<Integer, CachedBinary> redisTemplate;
public RedisTemplate<Integer, CachedBinary> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<Integer, CachedBinary> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void put(CachedBinary cachedBinary) {
ValueOperations<Integer, CachedBinary> valueOper = redisTemplate.opsForValue();
valueOper.set(cachedBinary.getId(), cachedBinary);
}
public void delete(Integer id) {
ValueOperations<Integer, CachedBinary> valueOper = redisTemplate.opsForValue();
RedisOperations<Integer, CachedBinary> RedisOperations = valueOper.getOperations();
RedisOperations.delete(id);
}
public CachedBinary get(Integer id) {
ValueOperations<Integer, CachedBinary> valueOper = redisTemplate.opsForValue();
return valueOper.get(id);
}
}

View File

@@ -0,0 +1,22 @@
package net.irext.server.service.aspect;
import net.irext.server.service.response.ServiceResponse;
/**
* Filename: TokenValidation.java
* Revised: Date: 2017-05-08
* Revision: Revision: 1.0
* <p>
* Description: Token validation aspect logic
* <p>
* Revision log:
* 2017-05-08: created by strawmanbobi
*/
public interface TokenValidation {
ServiceResponse validateToken(String userId, String token);
<T extends ServiceResponse> T validateToken(String userId, String token,
Class<T> c);
}

View File

@@ -1,7 +1,9 @@
package net.irext.server.service.businesslogic;
import net.irext.server.service.mapper.RemoteIndexMapper;
import net.irext.server.service.model.Category;
import net.irext.server.service.model.RemoteIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
@@ -17,23 +19,11 @@ import java.util.List;
* 2018-12-08: created by strawmanbobi
*/
@Controller
public class IndexLogic {
private static IndexLogic indexLogic;
public class IndexingLogic {
@Autowired
private RemoteIndexMapper remoteIndexMapper;
public static IndexLogic getInstance(RemoteIndexMapper remoteIndexMapper) {
if (null == indexLogic) {
indexLogic = new IndexLogic(remoteIndexMapper);
}
return indexLogic;
}
public IndexLogic(RemoteIndexMapper remoteIndexMapper) {
this.remoteIndexMapper = remoteIndexMapper;
}
public RemoteIndex getRemoteIndex(int indexId) {
List<RemoteIndex> remoteIndexList = remoteIndexMapper.getRemoteIndexById(indexId);
if (null != remoteIndexList && remoteIndexList.size() > 0) {
@@ -41,4 +31,8 @@ public class IndexLogic {
}
return null;
}
public List<Category> listCategories(int lang, int from, int count) {
return null;
}
}

View File

@@ -0,0 +1,94 @@
package net.irext.server.service.model;
public class Brand {
private Integer id;
private String name;
private Integer categoryId;
private String categoryName;
private Byte status;
private String updateTime;
private Integer priority;
private String nameEn;
private String nameTw;
private String contributor;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Byte getStatus() {
return status;
}
public void setStatus(Byte status) {
this.status = status;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public String getNameEn() {
return nameEn;
}
public void setNameEn(String nameEn) {
this.nameEn = nameEn;
}
public String getNameTw() {
return nameTw;
}
public void setNameTw(String nameTw) {
this.nameTw = nameTw;
}
public String getContributor() {
return contributor;
}
public void setContributor(String contributor) {
this.contributor = contributor;
}
}

View File

@@ -0,0 +1,67 @@
package net.irext.server.service.model;
public class Category {
private Integer id;
private String name;
private Byte status;
private String updateTime;
private String nameEn;
private String nameTw;
private String contributor;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getStatus() {
return status;
}
public void setStatus(Byte status) {
this.status = status;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public String getNameEn() {
return nameEn;
}
public void setNameEn(String nameEn) {
this.nameEn = nameEn;
}
public String getNameTw() {
return nameTw;
}
public void setNameTw(String nameTw) {
this.nameTw = nameTw;
}
public String getContributor() {
return contributor;
}
public void setContributor(String contributor) {
this.contributor = contributor;
}
}

View File

@@ -0,0 +1,67 @@
package net.irext.server.service.model;
public class City {
private Integer id;
private String code;
private String name;
private Double longitude;
private Double latitude;
private Byte status;
private String nameTw;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Byte getStatus() {
return status;
}
public void setStatus(Byte status) {
this.status = status;
}
public String getNameTw() {
return nameTw;
}
public void setNameTw(String nameTw) {
this.nameTw = nameTw;
}
}

View File

@@ -0,0 +1,67 @@
package net.irext.server.service.model;
public class StbOperator {
private Integer id;
private String operatorId;
private String operatorName;
private String cityCode;
private String cityName;
private Byte status;
private String operatorNameTw;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOperatorId() {
return operatorId;
}
public void setOperatorId(String operatorId) {
this.operatorId = operatorId;
}
public String getOperatorName() {
return operatorName;
}
public void setOperatorName(String operatorName) {
this.operatorName = operatorName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Byte getStatus() {
return status;
}
public void setStatus(Byte status) {
this.status = status;
}
public String getOperatorNameTw() {
return operatorNameTw;
}
public void setOperatorNameTw(String operatorNameTw) {
this.operatorNameTw = operatorNameTw;
}
}

View File

@@ -0,0 +1,48 @@
package net.irext.server.service.request;
import com.google.gson.Gson;
/**
* Filename: BaseRequest.java
* Revised: Date: 2017-04-07
* Revision: Revision: 1.0
* <p>
* Description: authentication factors included
* <p>
* Revision log:
* 2017-04-07: created by strawmanbobi
*/
public class BaseRequest {
private int id;
private String token;
public BaseRequest(int id, String token) {
this.id = id;
this.token = token;
}
BaseRequest() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String toJson() {
return new Gson().toJson(this, this.getClass());
}
}

View File

@@ -10,7 +10,7 @@ package net.irext.server.service.request;
* Revision log:
* 2018-12-18: created by strawmanbobi
*/
public class CloseRequest {
public class CloseRequest extends BaseRequest {
private String sessionId;

View File

@@ -12,7 +12,7 @@ import net.irext.server.sdk.bean.ACStatus;
* Revision log:
* 2017-05-16: created by strawmanbobi
*/
public class DecodeRequest {
public class DecodeRequest extends BaseRequest{
private int remoteIndexId;
private ACStatus acStatus;

View File

@@ -10,7 +10,7 @@ package net.irext.server.service.request;
* Revision log:
* 2017-04-14: created by strawmanbobi
*/
public class DownloadBinaryRequest {
public class DownloadBinaryRequest extends BaseRequest {
private int indexId;

View File

@@ -10,7 +10,7 @@ package net.irext.server.service.request;
* Revision log:
* 2019-02-14: created by strawmanbobi
*/
public class GetACParametersRequest {
public class GetACParametersRequest extends BaseRequest {
private int remoteIndexId;
private String sessionId;

View File

@@ -0,0 +1,52 @@
package net.irext.server.service.request;
/**
* Filename: ListBrandsRequest.java
* Revised: Date: 2017-04-07
* Revision: Revision: 1.0
* <p>
* Description: HTTP list brands request
* <p>
* Revision log:
* 2017-04-07: created by strawmanbobi
*/
public class ListBrandsRequest extends BaseRequest {
private int categoryId;
private int from;
private int count;
public ListBrandsRequest(int categoryId, int from, int count) {
this.categoryId = categoryId;
this.from = from;
this.count = count;
}
public ListBrandsRequest() {
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}

View File

@@ -0,0 +1,42 @@
package net.irext.server.service.request;
/**
* Filename: ListCategoriesRequest.java
* Revised: Date: 2017-04-07
* Revision: Revision: 1.0
* <p>
* Description: HTTP list categories request
* <p>
* Revision log:
* 2017-04-07: created by strawmanbobi
*/
public class ListCategoriesRequest extends BaseRequest {
private int from;
private int count;
public ListCategoriesRequest(int from, int count) {
this.from = from;
this.count = count;
}
public ListCategoriesRequest() {
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}

View File

@@ -0,0 +1,32 @@
package net.irext.server.service.request;
/**
* Filename: ListCitiesRequest.java
* Revised: Date: 2017-04-07
* Revision: Revision: 1.0
* <p>
* Description: HTTP list cities request
* <p>
* Revision log:
* 2017-04-07: created by strawmanbobi
*/
public class ListCitiesRequest extends BaseRequest {
private String provincePrefix;
public ListCitiesRequest(String provincePrefix) {
this.provincePrefix = provincePrefix;
}
public ListCitiesRequest() {
}
public String getProvincePrefix() {
return provincePrefix;
}
public void setProvincePrefix(String provincePrefix) {
this.provincePrefix = provincePrefix;
}
}

View File

@@ -0,0 +1,83 @@
package net.irext.server.service.request;
/**
* Filename: ListIndexesRequest.java
* Revised: Date: 2017-04-12
* Revision: Revision: 1.0
* <p>
* Description: HTTP list remote indexes request
* <p>
* Revision log:
* 2017-04-12: created by strawmanbobi
*/
public class ListIndexesRequest extends BaseRequest {
private int from;
private int count;
private int categoryId;
private int brandId;
private String cityCode;
private String operatorId;
public ListIndexesRequest(int from, int count, int categoryId, int brandId,
String cityCode, String operatorId) {
this.from = from;
this.count = count;
this.categoryId = categoryId;
this.brandId = brandId;
this.cityCode = cityCode;
this.operatorId = operatorId;
}
public ListIndexesRequest() {
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public int getBrandId() {
return brandId;
}
public void setBrandId(int brandId) {
this.brandId = brandId;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getOperatorId() {
return operatorId;
}
public void setOperatorId(String operatorId) {
this.operatorId = operatorId;
}
}

View File

@@ -0,0 +1,52 @@
package net.irext.server.service.request;
/**
* Filename: ListOperatorsRequest.java
* Revised: Date: 2017-04-10
* Revision: Revision: 1.0
* <p>
* Description: HTTP list STB operators request
* <p>
* Revision log:
* 2017-04-10: created by strawmanbobi
*/
public class ListOperatorsRequest extends BaseRequest {
private int from;
private int count;
private String cityCode;
public ListOperatorsRequest(int from, int count, String cityCode) {
this.from = from;
this.count = count;
this.cityCode = cityCode;
}
public ListOperatorsRequest() {
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
}

View File

@@ -0,0 +1,37 @@
package net.irext.server.service.response;
import net.irext.server.service.model.Brand;
import java.util.List;
/**
* Filename: BrandsResponse.java
* Revised: Date: 2017-04-07
* Revision: Revision: 1.0
* <p>
* Description: List brands response
* <p>
* Revision log:
* 2017-04-07: created by strawmanbobi
*/
public class BrandsResponse extends ServiceResponse {
private List<Brand> entity;
public BrandsResponse(Status status, List<Brand> brands) {
super(status);
this.entity = brands;
}
public BrandsResponse() {
}
public List<Brand> getEntity() {
return entity;
}
public void setEntity(List<Brand> entity) {
this.entity = entity;
}
}

View File

@@ -0,0 +1,37 @@
package net.irext.server.service.response;
import net.irext.server.service.model.Category;
import java.util.List;
/**
* Filename: CategoriesResponse.java
* Revised: Date: 2017-04-07
* Revision: Revision: 1.0
* <p>
* Description: List categories response
* <p>
* Revision log:
* 2017-04-07: created by strawmanbobi
*/
public class CategoriesResponse extends ServiceResponse {
private List<Category> entity;
public CategoriesResponse(Status status, List<Category> categories) {
super(status);
this.entity = categories;
}
public CategoriesResponse() {
}
public List<Category> getEntity() {
return entity;
}
public void setEntity(List<Category> entity) {
this.entity = entity;
}
}

View File

@@ -0,0 +1,37 @@
package net.irext.server.service.response;
import net.irext.server.service.model.City;
import java.util.List;
/**
* Filename: CitiesResponse.java
* Revised: Date: 2017-04-07
* Revision: Revision: 1.0
* <p>
* Description: List cities response
* <p>
* Revision log:
* 2017-04-07: created by strawmanbobi
*/
public class CitiesResponse extends ServiceResponse {
private List<City> entity;
public CitiesResponse(Status status, List<City> cities) {
super(status);
this.entity = cities;
}
public CitiesResponse() {
}
public List<City> getEntity() {
return entity;
}
public void setEntity(List<City> entity) {
this.entity = entity;
}
}

View File

@@ -0,0 +1,35 @@
package net.irext.server.service.response;
import java.util.List;
/**
* Filename: OperatorsResponse.java
* Revised: Date: 2017-04-10
* Revision: Revision: 1.0
* <p>
* Description: List STB operators response
* <p>
* Revision log:
* 2017-04-10: created by strawmanbobi
*/
public class OperatorsResponse extends ServiceResponse {
private List<net.irext.server.model.StbOperator> entity;
public OperatorsResponse(Status status, List<net.irext.server.model.StbOperator> cities) {
super(status);
this.entity = cities;
}
public OperatorsResponse() {
}
public List<net.irext.server.model.StbOperator> getEntity() {
return entity;
}
public void setEntity(List<net.irext.server.model.StbOperator> entity) {
this.entity = entity;
}
}

View File

@@ -0,0 +1,4 @@
package net.irext.server.service.restapi;
public class CategoryResponse {
}

View File

@@ -14,7 +14,7 @@ import net.irext.server.service.request.OpenRequest;
import net.irext.server.service.response.*;
import net.irext.server.service.utils.LoggerUtil;
import net.irext.server.service.utils.MD5Util;
import net.irext.server.service.businesslogic.IndexLogic;
import net.irext.server.service.businesslogic.IndexingLogic;
import net.irext.server.service.restapi.base.AbstractBaseService;
import net.irext.server.sdk.bean.ACStatus;
import net.irext.server.sdk.utils.Constants;
@@ -34,13 +34,13 @@ import java.text.SimpleDateFormat;
* Revised: Date: 2018-12-16
* Revision: Revision: 1.0
* <p>
* Description: IRext Decode WebService
* Description: IRext Decode Webservice
* <p>
* Revision log:
* 2018-12-16: created by strawmanbobi
*/
@RestController
@RequestMapping("/irext")
@RequestMapping("/irext-server/decode")
@Service("IRDecodeService")
public class IRDecodeService extends AbstractBaseService {
@@ -69,7 +69,7 @@ public class IRDecodeService extends AbstractBaseService {
LoggerUtil.getInstance().trace(TAG, "irOpen API called : " + remoteIndexId);
StringResponse response = new StringResponse();
RemoteIndex remoteIndex = IndexLogic.getInstance(remoteIndexMapper).getRemoteIndex(remoteIndexId);
RemoteIndex remoteIndex = IndexingLogic.getInstance(remoteIndexMapper).getRemoteIndex(remoteIndexId);
if (null == remoteIndex) {
response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR,
Constants.ERROR_CODE_NETWORK_ERROR_TEXT));

View File

@@ -0,0 +1,89 @@
package net.irext.server.service.restapi;
import net.irext.server.service.Constants;
import net.irext.server.service.businesslogic.IndexingLogic;
import net.irext.server.service.mapper.RemoteIndexMapper;
import net.irext.server.service.model.Category;
import net.irext.server.service.request.*;
import net.irext.server.service.response.*;
import net.irext.server.service.restapi.base.AbstractBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletContext;
import javax.ws.rs.HeaderParam;
import java.util.List;
/**
* Filename: IRIndexingService.java
* Revised: Date: 2019-06-08
* Revision: Revision: 1.0
* <p>
* Description: IRext Indexing Webservice
* <p>
* Revision log:
* 2019-06-08: created by strawmanbobi
*/
@RestController
@RequestMapping("/irext-server/indexing")
@Service("IRDecodeService")
public class IRIndexingService extends AbstractBaseService {
private static final String TAG = IRIndexingService.class.getSimpleName();
@Autowired
private ServletContext context;
@Autowired
private IndexingLogic indexingLogic;
public IRIndexingService(RemoteIndexMapper remoteIndexMapper) {
}
@PostMapping("/list_categories")
public CategoriesResponse listCategories(@HeaderParam("user-lang") String userLang,
ListCategoriesRequest listCategoriesRequest) {
try {
int id = listCategoriesRequest.getId();
String token = listCategoriesRequest.getToken();
int from = listCategoriesRequest.getFrom();
int count = listCategoriesRequest.getCount();
int lang = Constants.LANG_ZH_CN;
if (null != userLang) {
if (userLang.equals("en-US")) {
lang = Constants.LANG_EN;
} else if (userLang.equals("zh-TW")) {
lang = Constants.LANG_TW_CN;
} else {
lang = Constants.LANG_ZH_CN;
}
}
System.out.println("language = " + lang);
CategoriesResponse response = validateToken(Integer.toString(id), token, CategoriesResponse.class);
if (response.getStatus().getCode() == Constants.ERROR_CODE_AUTH_FAILURE) {
return response;
}
List<Category> categoryList = indexingLogic.listCategories(lang, from, count);
if (categoryList != null) {
response.getStatus().setCode(Constants.ERROR_CODE_SUCCESS);
response.setEntity(categoryList);
} else {
response.getStatus().setCode(Constants.ERROR_CODE_NETWORK_ERROR);
}
return response;
} catch (Exception e) {
e.printStackTrace();
return getExceptionResponse(CategoriesResponse.class);
}
}
}

View File

@@ -1,10 +1,15 @@
package net.irext.server.service.restapi.base;
import net.irext.server.redis.model.CachedAdmin;
import net.irext.server.redis.service.AdminService;
import net.irext.server.service.Constants;
import net.irext.server.service.aspect.TokenValidation;
import net.irext.server.service.response.ServiceResponse;
import net.irext.server.service.response.Status;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Filename: AbstractBaseService.java
@@ -16,10 +21,54 @@ import org.apache.commons.logging.LogFactory;
* Revision log:
* 2017-04-27: created by strawmanbobi
*/
public abstract class AbstractBaseService {
// note : not using ASPECT here but keep the ASPECT sketch
public abstract class AbstractBaseService implements TokenValidation {
protected static Log log = LogFactory.getLog(AbstractBaseService.class);
@Override
public ServiceResponse validateToken(String userId, String token) {
if (log.isDebugEnabled()) {
log.debug("Auth token id: " + userId + ", token: " + token);
}
ServiceResponse response = new ServiceResponse();
Status status = new Status();
if (null == userId || null == token) {
status.setCode(Constants.ERROR_CODE_AUTH_FAILURE);
} else {
status = validateUserToken(userId, token);
}
response.setStatus(status);
return response;
}
@Override
public <T extends ServiceResponse> T validateToken(String userId, String token,
Class<T> c) {
T r = null;
Status status = new Status();
if (null == userId || null == token) {
status.setCode(Constants.ERROR_CODE_AUTH_FAILURE);
} else {
status = validateUserToken(userId, token);
}
try {
r = c.newInstance();
} catch (InstantiationException e) {
log.error("Error when new instance of class: " + c.getName(), e);
} catch (IllegalAccessException e) {
log.error("Error when new instance of class: " + c.getName(), e);
}
if (null != r) {
r.setStatus(status);
}
return r;
}
protected ServiceResponse getExceptionResponse() {
ServiceResponse r = new ServiceResponse();
Status status = new Status();
@@ -33,7 +82,9 @@ public abstract class AbstractBaseService {
Status status = new Status();
try {
r = c.newInstance();
} catch (Exception e) {
} catch (InstantiationException e) {
log.error("Error when new instance of class: " + c.getName(), e);
} catch (IllegalAccessException e) {
log.error("Error when new instance of class: " + c.getName(), e);
}
status.setCode(Constants.ERROR_CODE_AUTH_FAILURE);
@@ -42,4 +93,21 @@ public abstract class AbstractBaseService {
}
return r;
}
private Status validateUserToken(String userId, String token) {
Status status = new Status();
ApplicationContext applicationContext;
AdminService adminService;
applicationContext = new ClassPathXmlApplicationContext("redisBeans.xml");
adminService = applicationContext.getBean(AdminService.class);
CachedAdmin cachedAdmin = adminService.get(userId);
if (null != cachedAdmin && cachedAdmin.getToken().equals(token)) {
status.setCode(Constants.ERROR_CODE_SUCCESS);
} else {
status.setCode(Constants.ERROR_CODE_AUTH_FAILURE);
}
return status;
}
}