From 3b55d06b89bf64873e685c3d78fce5affbba3d17 Mon Sep 17 00:00:00 2001 From: zheng_wenlong Date: Tue, 16 Apr 2019 11:20:38 +0900 Subject: Add warehouse server source code. [Patch Set 2] Add ReadMe.md Change-Id: I6ade52d2490f5ca4ba107c1a27ed6d5b39048725 Signed-off-by: zheng_wenlong --- webservice/src/main/java/app/market/LogUtil.java | 25 ++ .../src/main/java/app/market/PropertyUtil.java | 82 ++++ .../main/java/app/market/WebServiceConstants.java | 33 ++ .../src/main/java/app/market/common/comm.java | 51 +++ .../app/market/token/dao/RedisGeneratorDao.java | 48 ++ .../AuthenticationTokenInterceptor.java | 65 +++ .../market/token/service/RedisTokenManager.java | 268 ++++++++++++ .../main/java/app/market/webservice/Filter.java | 40 ++ .../java/app/market/webservice/InitServer.java | 52 +++ .../java/app/market/webservice/Log4jlistener.java | 53 +++ .../app/market/webservice/WebServiceClient.java | 24 + .../webservice/WebserviceRestBaseController.java | 35 ++ .../market/webservice/app/AppRestController.java | 485 +++++++++++++++++++++ .../authority/AuthorityRestController.java | 66 +++ .../webservice/dataManager/AppFileController.java | 255 +++++++++++ .../market/webservice/dataManager/FileUtil.java | 248 +++++++++++ .../market/webservice/dataManager/XmlFactory.java | 43 ++ .../resource/DictionaryRestController.java | 124 ++++++ .../resource/ResourceRestController.java | 71 +++ .../webservice/token/TokenRestController.java | 37 ++ .../market/webservice/user/UserRestController.java | 482 ++++++++++++++++++++ .../src/main/resources/fileManager.properties | 6 + webservice/src/main/resources/log4j.xml | 77 ++++ webservice/src/main/resources/mybatis-config.xml | 38 ++ .../src/main/resources/properties.properties | 7 + webservice/src/main/resources/redis-config.xml | 50 +++ webservice/src/main/resources/redis.properties | 13 + webservice/src/main/resources/servlet-context.xml | 56 +++ webservice/src/main/resources/spring-mybatis.xml | 68 +++ .../app/market/webservice/user/AppRestTest.java | 175 ++++++++ .../app/market/webservice/user/UploadTest.java | 77 ++++ .../app/market/webservice/user/UserRestTest.java | 75 ++++ webservice/src/main/webapp/WEB-INF/web.xml | 94 ++++ 33 files changed, 3323 insertions(+) create mode 100644 webservice/src/main/java/app/market/LogUtil.java create mode 100644 webservice/src/main/java/app/market/PropertyUtil.java create mode 100644 webservice/src/main/java/app/market/WebServiceConstants.java create mode 100644 webservice/src/main/java/app/market/common/comm.java create mode 100644 webservice/src/main/java/app/market/token/dao/RedisGeneratorDao.java create mode 100644 webservice/src/main/java/app/market/token/interceptor/AuthenticationTokenInterceptor.java create mode 100644 webservice/src/main/java/app/market/token/service/RedisTokenManager.java create mode 100644 webservice/src/main/java/app/market/webservice/Filter.java create mode 100644 webservice/src/main/java/app/market/webservice/InitServer.java create mode 100644 webservice/src/main/java/app/market/webservice/Log4jlistener.java create mode 100644 webservice/src/main/java/app/market/webservice/WebServiceClient.java create mode 100644 webservice/src/main/java/app/market/webservice/WebserviceRestBaseController.java create mode 100644 webservice/src/main/java/app/market/webservice/app/AppRestController.java create mode 100644 webservice/src/main/java/app/market/webservice/authority/AuthorityRestController.java create mode 100644 webservice/src/main/java/app/market/webservice/dataManager/AppFileController.java create mode 100644 webservice/src/main/java/app/market/webservice/dataManager/FileUtil.java create mode 100644 webservice/src/main/java/app/market/webservice/dataManager/XmlFactory.java create mode 100644 webservice/src/main/java/app/market/webservice/resource/DictionaryRestController.java create mode 100644 webservice/src/main/java/app/market/webservice/resource/ResourceRestController.java create mode 100644 webservice/src/main/java/app/market/webservice/token/TokenRestController.java create mode 100644 webservice/src/main/java/app/market/webservice/user/UserRestController.java create mode 100644 webservice/src/main/resources/fileManager.properties create mode 100644 webservice/src/main/resources/log4j.xml create mode 100644 webservice/src/main/resources/mybatis-config.xml create mode 100644 webservice/src/main/resources/properties.properties create mode 100644 webservice/src/main/resources/redis-config.xml create mode 100644 webservice/src/main/resources/redis.properties create mode 100644 webservice/src/main/resources/servlet-context.xml create mode 100644 webservice/src/main/resources/spring-mybatis.xml create mode 100644 webservice/src/main/test/app/market/webservice/user/AppRestTest.java create mode 100644 webservice/src/main/test/app/market/webservice/user/UploadTest.java create mode 100644 webservice/src/main/test/app/market/webservice/user/UserRestTest.java create mode 100644 webservice/src/main/webapp/WEB-INF/web.xml (limited to 'webservice/src/main') diff --git a/webservice/src/main/java/app/market/LogUtil.java b/webservice/src/main/java/app/market/LogUtil.java new file mode 100644 index 0000000..3c626b8 --- /dev/null +++ b/webservice/src/main/java/app/market/LogUtil.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market; + +import org.slf4j.Logger; + +public class LogUtil { + + public static void printCatchLog(final Logger logger, Exception e){ + //logger.error("Thread.currentThread().getStackTrace()[1].getMethodName()" + e.getMessage()); + } +} diff --git a/webservice/src/main/java/app/market/PropertyUtil.java b/webservice/src/main/java/app/market/PropertyUtil.java new file mode 100644 index 0000000..4d83c0c --- /dev/null +++ b/webservice/src/main/java/app/market/PropertyUtil.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Properties; + +import app.market.utils.constants.Constants; + +public class PropertyUtil { + + public static Properties defaultProp; + public static Properties fileProp; + public static Properties redisProp; + + private static Properties getPropertyInstance(String fileName) { + Properties prop = new Properties(); + try { + prop.load(new InputStreamReader(PropertyUtil.class.getClassLoader().getResourceAsStream(fileName), + Constants.CHARACTER_UTF8)); + } catch (FileNotFoundException e) { + throw new RuntimeException(); + } catch (IOException e) { + throw new RuntimeException(); + } + return prop; + } + + /** + * get Redis property + * + * @param key + * @return String + */ + public static String getRedisPropertites(String key) { + if (redisProp == null) { + redisProp = getPropertyInstance(Constants.PROPERTIES_FILE_NAME_REDIS); + } + return redisProp.getProperty(key); + } + + /** + * get file property + * + * @param key + * @return String + */ + public static String getFileManagerPropertites(String key) { + if (fileProp == null) { + fileProp = getPropertyInstance(Constants.PROPERTIES_FILE_NAME_FILE); + } + return fileProp.getProperty(key); + } + + /** + * get propertites + * + * @param key + * @return String + */ + public static String getPropertites(String key) { + if (defaultProp == null) { + defaultProp = getPropertyInstance(Constants.PROPERTIES_FILE_NAME_DEFAULT); + } + return defaultProp.getProperty(key); + } +} diff --git a/webservice/src/main/java/app/market/WebServiceConstants.java b/webservice/src/main/java/app/market/WebServiceConstants.java new file mode 100644 index 0000000..0416441 --- /dev/null +++ b/webservice/src/main/java/app/market/WebServiceConstants.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market; + +public class WebServiceConstants { + + /** Time-out */ + public static final long EXPIRE_TIME_MINUTES = Long.valueOf( PropertyUtil.getRedisPropertites( "redis.timeout" ) ); + public static final long EXPIRE_TIME_HOURS = Long.valueOf( PropertyUtil.getRedisPropertites( "redis.refreshtimeout" ) ); + + public static final String PATTERN_SKIP_LOGIN_AUTHRIZATION = "[A-Za-z0-9_/-]+/login"; + public static final String PATTERN_SKIP_REFRESHTOKEN_AUTHRIZATION = "[A-Za-z0-9_/-]+/token/[A-Za-z0-9_/:-]+"; + public static final String PATTERN_RESOURSE_API = "./api/v1/(.*?)[/{*|]\\w"; + public static final String PATTERN_RESOURSE_S = "/api/v1/"; + public static final String PATTERN_RESOURSE_PARAM_S = "/{"; + public static final String SEPARATOR = "/"; + + public static final String RESOURSE_TYPE_RES = "resource"; + public static final String RESOURSE_TYPE_API = "api"; +} diff --git a/webservice/src/main/java/app/market/common/comm.java b/webservice/src/main/java/app/market/common/comm.java new file mode 100644 index 0000000..4b200ba --- /dev/null +++ b/webservice/src/main/java/app/market/common/comm.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.common; + +import javax.servlet.http.HttpServletResponse; + +import org.springframework.http.HttpStatus; + +import app.market.model.errors.ResponseErrors; +import app.market.utils.constants.Constants; +import app.market.utils.json.JsonMapperUtils; +import app.market.utils.webservice.ErrorCode; + +public class comm { + + public static String getResponseError(HttpServletResponse response, int httpCode, ErrorCode errorcode) + { + //set Response code + ResponseErrors errors = new ResponseErrors(); + response.setStatus(httpCode); + + //set errors message + errors.setcode(errorcode.getCodeStr().toString()); + errors.setMessage(errorcode.getMessage()); + return JsonMapperUtils.writeValueAsString(errors); + } + + public static String getResponseException(HttpServletResponse response, Exception e) { + ResponseErrors errors = new ResponseErrors(); + response.setStatus(Constants.STATUS_ERROR); + errors.setcode(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); + errors.setMessage(e.getMessage()); + return JsonMapperUtils.writeValueAsString(errors); + + } + + +} diff --git a/webservice/src/main/java/app/market/token/dao/RedisGeneratorDao.java b/webservice/src/main/java/app/market/token/dao/RedisGeneratorDao.java new file mode 100644 index 0000000..9b18594 --- /dev/null +++ b/webservice/src/main/java/app/market/token/dao/RedisGeneratorDao.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.token.dao; + +import java.io.Serializable; + +import javax.annotation.Resource; + +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.RedisSerializer; + +public abstract class RedisGeneratorDao { + + @Resource(name = "redisTemplate") + protected RedisTemplate redis; + + /** + * set redisTemplate + * + * @param redisTemplate + * the redisTemplate to set + */ + public void setRedisTemplate(RedisTemplate redisTemplate) { + this.redis = redisTemplate; + } + + /** + * get RedisSerializer
+ * ------------------------------
+ */ + protected RedisSerializer getRedisSerializer() { + return redis.getStringSerializer(); + } + +} \ No newline at end of file diff --git a/webservice/src/main/java/app/market/token/interceptor/AuthenticationTokenInterceptor.java b/webservice/src/main/java/app/market/token/interceptor/AuthenticationTokenInterceptor.java new file mode 100644 index 0000000..f6993a0 --- /dev/null +++ b/webservice/src/main/java/app/market/token/interceptor/AuthenticationTokenInterceptor.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.token.interceptor; + +import java.util.regex.Pattern; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import app.market.WebServiceConstants; +import app.market.token.service.RedisTokenManager; +import app.market.utils.constants.Constants; + +@Aspect +@Component +public class AuthenticationTokenInterceptor extends HandlerInterceptorAdapter { + + @Autowired + private RedisTokenManager tokenManager; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) + throws Exception { + + if (Pattern.matches(WebServiceConstants.PATTERN_SKIP_LOGIN_AUTHRIZATION, request.getRequestURI()) + || (Pattern.matches(WebServiceConstants.PATTERN_SKIP_REFRESHTOKEN_AUTHRIZATION, request.getRequestURI()) + && HttpMethod.GET.name().equals(request.getMethod()))) + return true; + + // get token from header + String authentication = request.getHeader(Constants.TOKEN_AUTHORIZATION); + String servletPath = request.getHeader(Constants.TOKEN_AUTHORIZATION_RESOURCE); + String url = request.getRequestURI(); + String httpMethod = request.getMethod(); + + // check authentication + int status = tokenManager.checkAuthentication(authentication, servletPath, url, httpMethod); + if (status != Constants.STATUS_SUCCESS) { + response.setStatus(status); + return false; + } + return true; + } + +} diff --git a/webservice/src/main/java/app/market/token/service/RedisTokenManager.java b/webservice/src/main/java/app/market/token/service/RedisTokenManager.java new file mode 100644 index 0000000..aea419e --- /dev/null +++ b/webservice/src/main/java/app/market/token/service/RedisTokenManager.java @@ -0,0 +1,268 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.token.service; + +import java.util.List; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import app.market.WebServiceConstants; +import app.market.model.resource.Resource; +import app.market.model.user.User; +import app.market.token.dao.RedisGeneratorDao; +import app.market.utils.constants.Constants; +import app.market.utils.token.TokenModel; +import app.market.utils.webservice.WebServiceURI; + +@Component +public class RedisTokenManager extends RedisGeneratorDao { + + private static Logger logger = LoggerFactory.getLogger( RedisTokenManager.class ); + + /** + * generate token + * @param loginId + * @param user + * @param list + * @param isPermanent guestUser not expire + * @return + */ + public TokenModel generateToken(String loginId, User user, List list, boolean isPermanent ) { + // user UUID as token + String token = UUID.randomUUID().toString().replace( "-", "" ); + TokenModel model = new TokenModel( loginId, user, token ); + for (Resource res : list) { + if ( res.getResType().equals( WebServiceConstants.RESOURSE_TYPE_RES ) ) { + model.getResourceList().add( res ); + } else if ( res.getResType().equals( WebServiceConstants.RESOURSE_TYPE_API ) ) { + model.getApiList().add( res ); + } + } + // save token, set expire time + if(isPermanent){ + redis.boundValueOps( loginId ).set( model ); + }else{ + redis.boundValueOps( loginId ).set( model, WebServiceConstants.EXPIRE_TIME_MINUTES, TimeUnit.MINUTES ); + } + return model; + } + + + public String generateRefreshToken(String loginId) { + String token = UUID.randomUUID().toString().replace( "-", "" ); + redis.boundValueOps( token ).set( loginId, WebServiceConstants.EXPIRE_TIME_HOURS, TimeUnit.HOURS ); + return token; + } + + public int checkAuthentication(String authentication, String servletPath, String url, String httpMethod) { + int ret = Constants.STATUS_SUCCESS; + if(StringUtils.isEmpty(authentication)){ //guest + ret = validateAuthentication(authentication, servletPath, url, httpMethod); + }else{ + ret = validateToken(authentication, servletPath, url, httpMethod); + } + return ret; + } + + /** + * check Authentication when guest + * @param authentication + * @param servletPath + * @param httpMethod + * @param url + * @return + */ + private int validateAuthentication(String authentication, String servletPath, String url, String httpMethod) { + int ret = Constants.STATUS_FORBIDDEN; + String guestId = Constants.GUEST_ID; + TokenModel model = null; + + if (redis.opsForValue().get(guestId) instanceof TokenModel) { + model = (TokenModel) redis.opsForValue().get(guestId); + } + + if (model == null) { + //ResourceRestController.initGuestResoureList(Constants.GUEST_ID); + logger.debug( "validateAuthentication" + this.getClass().getName() + "--method: " + + Thread.currentThread().getStackTrace()[1].getMethodName() + "model is null" ); + ret = Constants.STATUS_ERROR; + } + + if(checkResoureAuth(model, servletPath, url, httpMethod)) { + ret = Constants.STATUS_SUCCESS; + }else { + ret = Constants.STATUS_FORBIDDEN; + } + + return ret; + } + + /** + * check Authentication when not guest + * @param authentication + * @param servletPath + * @param httpMethod + * @param url + * @return + */ + private int validateToken(String authentication, String servletPath, String url, String httpMethod) { + TokenModel model = getToken( authentication ); + + if ( model == null ) + return Constants.STATUS_UNAUTHORIZED; + // Only one account is online as one time + if ( !model.getToken().equals( authentication.split( Constants.TOKEN_SEPARATOR )[1] ) ) { + return Constants.STATUS_TOO_MANY_CONNECTIONS; + } + // get cache token + TokenModel cacheModel = getCacheModel( model.getLoginId() ); + // check cache token + if ( cacheModel == null ) + return Constants.STATUS_UNAUTHORIZED; + + String token = cacheModel.getToken(); + if ( !token.equals( model.getToken() ) ) + return Constants.STATUS_UNAUTHORIZED; + + if (!checkResoureAuth(cacheModel, servletPath, url, httpMethod)) { + return Constants.STATUS_FORBIDDEN; + } + + // check ok, reset expire time + redis.boundValueOps( model.getLoginId() ).expire( WebServiceConstants.EXPIRE_TIME_MINUTES, TimeUnit.SECONDS ); + return Constants.STATUS_SUCCESS; + } + + /** + * + * @param list + * @param resPath + * @return + */ + private boolean checkResoureAuth(TokenModel model, String resPath, String url, String httpMethod) { + boolean ret = false; + + if (StringUtils.isEmpty(resPath) && !url.contains(WebServiceURI.REST_TOKEN_VALIDATETOKENAUTHORTICATION_LF)) { //URL check + + int s = url.indexOf(WebServiceConstants.PATTERN_RESOURSE_S); + if (s < 0) { + return ret; + } + resPath = url.substring(s + WebServiceConstants.PATTERN_RESOURSE_S.length()-1); + if (resPath.endsWith(WebServiceConstants.SEPARATOR)) { + resPath = resPath.substring(0, resPath.length() - 1); + } + + int count = (resPath.split(WebServiceConstants.SEPARATOR)).length; + + List list = model.getApiList(); + for (Object obj : list) { + Resource r = (Resource) obj; + + //httpmethod check + if (!r.getHttpMethod().equalsIgnoreCase(httpMethod)){ + continue; + } + + //url length check + if(count != r.getAccessPath().split(WebServiceConstants.SEPARATOR).length) { + continue; + } + + String rUrl = r.getAccessPath(); + //delete param depart of url + int paramS = r.getAccessPath().indexOf(WebServiceConstants.PATTERN_RESOURSE_PARAM_S); + if (paramS > 0) { + rUrl = r.getAccessPath().substring(0, paramS); + } + + //url check + if (resPath.indexOf(rUrl) == 0) { + ret = true; + break; + } + } + + } else if(url.contains(WebServiceURI.REST_TOKEN_VALIDATETOKENAUTHORTICATION_LF)){ + // temp code + ret = true; + } else {//For web selertPath check; + List list = model.getResourceList(); + for (Object obj : list) { + Resource res = (Resource) obj; + if (res.getAccessPath().contains(resPath)) { + ret = true; + break; + } + } + } + return ret; + } + + public TokenModel getToken(String authentication) { + String loginId =""; + if (StringUtils.isEmpty(authentication)) { + loginId = Constants.GUEST_ID; + } else { + String[] param = authentication.split(Constants.TOKEN_SEPARATOR); + if (param.length != 2){ + return null; + } + loginId = param[0]; + } + TokenModel model = null; + if (redis.opsForValue().get(loginId) instanceof TokenModel) { + model = (TokenModel) redis.opsForValue().get(loginId); + } + return model; + } + + public TokenModel getCacheModel(String loginId) { + try { + return (TokenModel) redis.boundValueOps( loginId ).get(); + } catch ( Exception e ) { + return null; + } + } + + public String getRefreshtokenValue(String authentication) { + if (StringUtils.isEmpty(authentication)) { + return null; + } + String[] param = authentication.split(Constants.TOKEN_SEPARATOR); + if (param.length != 2){ + return null; + } + String loginId = (String) redis.opsForValue().get(param[1]); + + return loginId; + } + + public void updateTokenInfo(TokenModel model) { + redis.boundValueOps( model.getLoginId() ).set( model, WebServiceConstants.EXPIRE_TIME_MINUTES, + TimeUnit.MINUTES ); + } + + public void deleteToken(String loginId) { + redis.delete( loginId ); + } + +} diff --git a/webservice/src/main/java/app/market/webservice/Filter.java b/webservice/src/main/java/app/market/webservice/Filter.java new file mode 100644 index 0000000..de92006 --- /dev/null +++ b/webservice/src/main/java/app/market/webservice/Filter.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.webservice; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.web.filter.OncePerRequestFilter; + +import app.market.utils.constants.Constants; + +public class Filter extends OncePerRequestFilter { + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws ServletException, IOException { + request.setCharacterEncoding( Constants.CHARACTER_UTF8 ); + response.setCharacterEncoding( Constants.CHARACTER_UTF8 ); + filterChain.doFilter( request, response ); + + } + +} diff --git a/webservice/src/main/java/app/market/webservice/InitServer.java b/webservice/src/main/java/app/market/webservice/InitServer.java new file mode 100644 index 0000000..92f7f6b --- /dev/null +++ b/webservice/src/main/java/app/market/webservice/InitServer.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.webservice; + +import java.util.List; + +import javax.servlet.ServletContext; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.context.ServletContextAware; + +import app.market.core.resource.ResourceCore; +import app.market.token.service.RedisTokenManager; +import app.market.utils.constants.Constants; + +@Service +public class InitServer implements InitializingBean, ServletContextAware { + + @Autowired + private RedisTokenManager tokenManager; + + @Autowired + private ResourceCore resourceCore; + + @Override + public void setServletContext(ServletContext arg0) { + List list = resourceCore.selectResourcesByLoginId( Constants.GUEST_ID ); + tokenManager.generateToken( Constants.GUEST_ID, null, list, true ); + } + + @Override + public void afterPropertiesSet() throws Exception { + // TODO Auto-generated method stub + + } + +} diff --git a/webservice/src/main/java/app/market/webservice/Log4jlistener.java b/webservice/src/main/java/app/market/webservice/Log4jlistener.java new file mode 100644 index 0000000..9dee500 --- /dev/null +++ b/webservice/src/main/java/app/market/webservice/Log4jlistener.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.webservice; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Enumeration; +import java.util.Properties; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +public class Log4jlistener implements ServletContextListener { + + public void contextDestroyed(ServletContextEvent servletcontextevent) { + // System.getProperties().clear(); + } + + public void contextInitialized(ServletContextEvent servletcontextevent) { + Properties prop = new Properties(); + try { + prop.load( new InputStreamReader( + Log4jlistener.class.getClassLoader().getResourceAsStream( "properties.properties" ) ) ); + } catch ( IOException e ) { + e.printStackTrace(); + } + Enumeration e = prop.keys(); + while ( e.hasMoreElements() ) { + String key = e.nextElement().toString(); + String path = prop.getProperty( key ); + System.setProperty( key, path ); + File f = new File( path ); + if ( !f.getParentFile().exists() ) { + f.getParentFile().mkdirs(); + } + } + } + +} diff --git a/webservice/src/main/java/app/market/webservice/WebServiceClient.java b/webservice/src/main/java/app/market/webservice/WebServiceClient.java new file mode 100644 index 0000000..39b63f2 --- /dev/null +++ b/webservice/src/main/java/app/market/webservice/WebServiceClient.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.webservice; + +import app.market.utils.property.PropertyUtil; + +public class WebServiceClient { + + public static final String REST_SERVICE_URI = PropertyUtil.getPropertites( "webservice_base_uri" ); + +} diff --git a/webservice/src/main/java/app/market/webservice/WebserviceRestBaseController.java b/webservice/src/main/java/app/market/webservice/WebserviceRestBaseController.java new file mode 100644 index 0000000..1f9351c --- /dev/null +++ b/webservice/src/main/java/app/market/webservice/WebserviceRestBaseController.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.webservice; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.web.servlet.HandlerExceptionResolver; +import org.springframework.web.servlet.ModelAndView; + +public class WebserviceRestBaseController implements HandlerExceptionResolver { + + protected static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8"; + + @Override + public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, + Exception ex) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/webservice/src/main/java/app/market/webservice/app/AppRestController.java b/webservice/src/main/java/app/market/webservice/app/AppRestController.java new file mode 100644 index 0000000..25b5d68 --- /dev/null +++ b/webservice/src/main/java/app/market/webservice/app/AppRestController.java @@ -0,0 +1,485 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.webservice.app; + +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.alibaba.fastjson.JSONObject; + +import app.market.common.comm; +import app.market.core.app.AppCore; +import app.market.core.resource.DictionaryCore; +import app.market.core.user.UserCore; +import app.market.model.app.App; +import app.market.model.app.AppExample; +import app.market.model.app.AppExample.Criteria; +import app.market.model.app.AppVersion; +import app.market.utils.constants.Constants; +import app.market.utils.json.JsonMapperUtils; +import app.market.utils.property.KeysConstants; +import app.market.utils.webservice.ApiParam; +import app.market.utils.webservice.ErrorCode; +import app.market.utils.webservice.ErrorCodeEnum; +import app.market.utils.webservice.WebServiceURI; +import app.market.webservice.WebserviceRestBaseController; + +@RestController +public class AppRestController extends WebserviceRestBaseController { + + private static Logger logger = LoggerFactory.getLogger(AppRestController.class); + + @Autowired + private AppCore appCore; + + @Autowired + private UserCore userCore; + + @Autowired + private DictionaryCore dicCore; + + /** + * get application list + * + * @param appName + * @param appTypeId + * @param appDeveloper + * @param sort + * @param order + * @param offset + * @param limit + * @return + */ + @RequestMapping(value = WebServiceURI.REST_APP_COLLECTION_APP_LF, method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE) + public String selectDataByExample( + @RequestParam(value = ApiParam.API_APP_PARAM_APPTYPEID, required = false, defaultValue = "") String appTypeId, + @RequestParam(value = ApiParam.API_APP_PARAM_APPDEVICETYPEID, required = false, defaultValue = "") String deviceTypeId, + @RequestParam(value = ApiParam.API_APP_PARAM_APPISPUBLIC, required = false, defaultValue = "") String isPublic, + @RequestParam(value = ApiParam.API_PARAM_ORDER, required = false, defaultValue = ApiParam.API_PARAM_VALUE_ORDER_ASC) String order, + @RequestParam(value = ApiParam.API_PARAM_SORT, required = false, defaultValue = ApiParam.API_PARAM_DEFAULT_SORT_NAME) String sort, + @RequestParam(value = ApiParam.API_PARAM_OFFSET, required = false, defaultValue = ApiParam.API_PARAM_DEFAULT_OFFSET) Integer offset, + @RequestParam(value = ApiParam.API_PARAM_LIMIT, required = false, defaultValue = ApiParam.API_PARAM_DEFAULT_LIMIT ) Integer limit, + @RequestParam(value = ApiParam.API_APP_PARAM_KEYWORD, required = false, defaultValue = "") String keyWord, + HttpServletRequest request, + HttpServletResponse response) { + logger.debug("get application list --S--"); + + ErrorCode paramCheckResult = checkParamSearch(appTypeId,deviceTypeId,isPublic,order,sort,offset,limit,request); + if (paramCheckResult != null) { + return comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, paramCheckResult); + } + //guest user do not authority + String authentication = request.getHeader(Constants.TOKEN_AUTHORIZATION); + if(StringUtils.isEmpty(authentication)) { + if(StringUtils.isEmpty(isPublic)) { + isPublic = ApiParam.API_PARAM_DEFAULT_IS_PUBLIC; + } + else if (ApiParam.API_PARAM_DEFAULT_NOT_PUBLIC.equals(isPublic)) { + return comm.getResponseError(response, Constants.STATUS_FORBIDDEN, + new ErrorCode(ErrorCodeEnum.FORBIDDEN_RESOURCE,KeysConstants.STATUS_FORBIDDEN)); + } + } + + AppExample example = getAppSearchExample(deviceTypeId, isPublic, appTypeId, keyWord, sort, order); + String responseStr = null; + Map map = new HashMap(); + + try { + int counter = appCore.countByExample(example); + List list = appCore.selectByExample(offset, limit, example); + + map.put(Constants.PAGINATION_COUNTER, counter); + map.put(Constants.PAGINATION_DATA, list); + + responseStr = JsonMapperUtils.writeValueAsString(map); + } catch (Exception e) { + responseStr = comm.getResponseException(response, e); + } + + logger.debug("get application list --E--"); + return responseStr; + } + + /** + * + * @param deviceTypeId + * @param isPublic + * @param appTypeId + * @param keyWord + * @param sort + * @param order + * @return + */ + private AppExample getAppSearchExample(String deviceTypeId, String isPublic, String appTypeId, String keyWord, Object sort, String order) { + AppExample example = new AppExample(); + + //set or condition + if (StringUtils.isNotEmpty(keyWord)) { + String likeKeyWord = "%" + keyWord + "%"; + Criteria ucAppName = example.createCriteria(); + setAndCondition(ucAppName, deviceTypeId, isPublic, appTypeId); + ucAppName.andAppNameLike(likeKeyWord); + + Criteria ucDeveloper = example.createCriteria(); + setAndCondition(ucDeveloper, deviceTypeId, isPublic, appTypeId); + ucDeveloper.andDeveloperNameLike(likeKeyWord); + example.or(ucDeveloper); + + Criteria ucAbstract = example.createCriteria(); + setAndCondition(ucAbstract, deviceTypeId, isPublic, appTypeId); + ucAbstract.andAbstractLike(likeKeyWord); + example.or(ucAbstract); + }else{ + Criteria uc = example.createCriteria(); + setAndCondition(uc, deviceTypeId, isPublic, appTypeId); + } + + // set sort and order + String sortName = ApiParam.AppQueryParam.get(sort); + if (StringUtils.isNotEmpty(sortName)) { + example.setOrderByClause(sortName + " " + order); + } + return example; + } + + /** + * + * @param uc + * @param deviceTypeId + * @param isPublic + * @param appTypeId + */ + private void setAndCondition(Criteria uc, String deviceTypeId, String isPublic, String appTypeId) + { + uc.andIsDelEqualTo(Constants.DATA_VALID); + uc.andAppNameIsNotNull(); + if (StringUtils.isNotEmpty(deviceTypeId)) { + uc.andAppDeviceTypeIdEqualTo(deviceTypeId); + } + if (StringUtils.isNotEmpty(isPublic)) { + uc.andAppIsPublicEqualTo(isPublic); + } + if (StringUtils.isNotEmpty(appTypeId)) { + uc.andTypeIdEqualTo(appTypeId); + } + } + + /** + * update application information + * + * @param appJsonStr + * @return appId + */ + @RequestMapping(value = WebServiceURI.REST_APP_INFO_LF, method = RequestMethod.POST, consumes = APPLICATION_JSON_UTF8_VALUE, produces = APPLICATION_JSON_UTF8_VALUE) + public String saveApp(@RequestBody String appJsonStr, HttpServletResponse response) { + logger.debug("update application information --S-- appStr=" + appJsonStr); + + if(StringUtils.isEmpty(appJsonStr)){ + return comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, + new ErrorCode(ErrorCodeEnum.INVALID_QUERYPARAM,KeysConstants.INVALID_QUERYPARAM)); + } + + App app = JsonMapperUtils.readValue(appJsonStr, App.class); + + //createDate and updateDate + Date date = new Date(); + if(StringUtils.isEmpty(app.getAppId())){ + app.setCreateDate(date); + app.setUpdateDate(date); + }else{ + app.setUpdateDate(date); + } + + //パラメータのチェック + ErrorCode paramCheckResult = checkParamSave(app); + if (paramCheckResult != null) { + return comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, paramCheckResult); + } + app.setIsDel(Constants.DATA_VALID); + + JSONObject responseData = new JSONObject(); + String responseStr = null; + + try { + int r = appCore.save(app); + if (r == 0) { + responseStr = comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, + new ErrorCode(ErrorCodeEnum.INVALID_QUERYPARAM,KeysConstants.MISSING_NECESSARY_QUERYPARAM)); + } else { + responseData.put(ApiParam.API_RESPONSE_APPID, app.getAppId()); + responseStr = responseData.toJSONString(); + } + } catch (DuplicateKeyException ed) { + responseStr = comm.getResponseError(response, Constants.STATUS_ALREADY_EXISTS, new ErrorCode(ErrorCodeEnum.RESOURCE_ALREADY_EXISTS,KeysConstants.RESOURCE_APP_ALREADY_EXISTS)); + } catch (Exception e) { + responseStr = comm.getResponseException(response, e); + } + logger.debug("update application information --E-- appStr=" + appJsonStr); + return responseStr; + } + + /** + * + * @param appVerInfo + * @return + */ + @RequestMapping(value = WebServiceURI.REST_APP_VERSION_INFO_LF, method = RequestMethod.POST, consumes = APPLICATION_JSON_UTF8_VALUE, produces = APPLICATION_JSON_UTF8_VALUE) + public String saveAppVersionInfo(@RequestBody String appVerInfo, HttpServletResponse response) { + logger.debug("saveAppVersionInfo,input=" + appVerInfo); + boolean insertFlag = false; + + if(StringUtils.isEmpty(appVerInfo)){ + return comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, + new ErrorCode(ErrorCodeEnum.INVALID_QUERYPARAM,KeysConstants.APP_VERSIONNAME_IS_NOT_EMPTY)); + } + + AppVersion appVer = JsonMapperUtils.readValue(appVerInfo, AppVersion.class); + + //createDate and updateDate + Date date = new Date(); + if(StringUtils.isEmpty(appVer.getVersionId())){ + appVer.setVersionCreateDate(date); + } + + //check parameter + ErrorCode paramCheckResult = checkParamVersionSave(appVer); + if (paramCheckResult != null) { + return comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, paramCheckResult); + } + + appVer.setVersionIsDel(Constants.DATA_VALID); + if (appVer.getVersionId().isEmpty()) { + insertFlag = true; + } + + JSONObject responseData = new JSONObject(); + String responseStr = null; + + try { + int r = appCore.saveVision(appVer); + if (r == 0) { + responseStr = comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, + new ErrorCode(ErrorCodeEnum.INVALID_QUERYPARAM,KeysConstants.MISSING_NECESSARY_QUERYPARAM)); + } else { + responseData.put(ApiParam.API_RESPONSE_APPVERSIONID, appVer.getVersionId()); + responseStr = responseData.toJSONString(); + + //Save verion + if (insertFlag) { + App app = new App(); + app.setAppId(appVer.getvAppId()); + app.setAppVersionId(appVer.getVersionId()); + int i = appCore.save(app); + if (i == 0) { + responseStr = comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, new ErrorCode( + ErrorCodeEnum.INVALID_QUERYPARAM, KeysConstants.MISSING_NECESSARY_QUERYPARAM)); + } + } + } + } catch (Exception e) { + responseStr = comm.getResponseException(response, e); + } + logger.debug("アプリを保存終了 --E-- appStr=" + appVerInfo); + return responseStr; + } + + /** + * Delete application information + * + * @param user + * @return + */ + @RequestMapping(value = WebServiceURI.REST_APP_INFO_PARM_ID_LF, method = RequestMethod.DELETE, produces = APPLICATION_JSON_UTF8_VALUE) + public String deleteApp(@PathVariable(ApiParam.API_APP_PARAM_APPID) String appId, HttpServletResponse response) { + logger.debug("Delete application --S--" + appId); + + String responseStr = ""; + try { + int r = appCore.deleteByAppId(appId); + if (r == 0) { + responseStr = comm.getResponseError(response, Constants.STATUS_BAD_REQUEST, + new ErrorCode(ErrorCodeEnum.INVALID_QUERYPARAM,KeysConstants.INVALID_OPERATION)); + } + } catch (Exception e) { + responseStr = comm.getResponseException(response, e); + } + + logger.debug("Delete application --E-- appId=" + appId); + return responseStr; + } + + /** + * Get application information from id + * + * @param appId + * @return + */ + @RequestMapping(value = WebServiceURI.REST_APP_INFO_PARM_ID_LF, method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE) + public String getAppInfo(@PathVariable(ApiParam.API_APP_PARAM_APPID) String appId, HttpServletResponse response) { + logger.debug(" Get application information from id --S-- appId=" + appId); + String responseStr = ""; + + try { + App app = appCore.selectAppByAppId(appId); + if(app == null){ + return responseStr; + } + responseStr = JsonMapperUtils.writeValueAsString(app); + } catch (Exception e) { + responseStr = comm.getResponseException(response, e); + } + + logger.debug(" Get application information from id --E-- appId=" + appId); + return responseStr; + } + + /** + * Get application information from CustomId + * + * @param appCustomId + * @return + */ + @RequestMapping(value = WebServiceURI.REST_APP_INFO_PARM_CUSTOMID_LF, method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE) + public String getAppInfoByCustomId(@PathVariable(ApiParam.API_APP_PARAM_APPID_CUSTOM) String appIdCustom, HttpServletResponse response) { + logger.debug(" Get application information from CustomId --S-- appIdCustom=" + appIdCustom); + String responseStr = ""; + + try { + App app = appCore.selectAppByAppCustomId(appIdCustom); + if(app == null){ + return responseStr; + } + responseStr = JsonMapperUtils.writeValueAsString(app); + } catch (Exception e) { + responseStr = comm.getResponseException(response, e); + } + + logger.debug(" Get application information from CustomId --E-- appIdCustom=" + appIdCustom); + return responseStr; + } + + /** + * Check application + * @param app + * @return null:valid parameter + * ErrorCode: invalid parameter + */ + + private ErrorCode checkParamSave(App app) { + // create new application + if(StringUtils.isEmpty(app.getAppId())){ + if(StringUtils.isEmpty(app.getAppDeviceTypeId().replace(" ", ""))){ + return new ErrorCode(ErrorCodeEnum.MISSING_NECESSARY_QUERYPARAM, KeysConstants.MISSING_NECESSARY_QUERYPARAM); + } + }else{ + // update application + if (!StringUtils.isEmpty(app.getAppId())&& app.getUpdateDate() == null) { + return new ErrorCode(ErrorCodeEnum.MISSING_NECESSARY_QUERYPARAM, KeysConstants.MISSING_NECESSARY_QUERYPARAM); + } + + //check developer + if (userCore.countById(app.getDeveloper()) <= 0) { + return new ErrorCode(ErrorCodeEnum.INVALID_BODY, KeysConstants.DEVELOPER_IS_NOT_EXIST); + } + + //check typeId + if (dicCore.countDicforItem(ApiParam.API_PARAM_VALUE_DICTIONARY_CATEGORY, app.getTypeId()) <= 0) { + return new ErrorCode(ErrorCodeEnum.INVALID_QUERYPARAM, KeysConstants.INVALID_QUERYPARAM); + } + + //check deviceTypeId + /*if (0 > Integer.valueOf(app.getAppDeviceTypeId()) || Integer.valueOf(app.getAppDeviceTypeId()) > 9 ) { + return new ErrorCode(ErrorCodeEnum.INVALID_QUERYPARAM, KeysConstants.INVALID_QUERYPARAM); + }*/ + } + + return null; + } + + /** + * Check applciation version + * @param ver + * @return null:valid parameter + * ErrorCode: invalid parameter + */ + private ErrorCode checkParamVersionSave(AppVersion ver) { + if (ver.getVersionName() == null + || ver.getvAppId() == null + || (StringUtils.isEmpty(ver.getvAppId().replace(" ", ""))) + || (StringUtils.isEmpty(ver.getVersionName().replace(" ", "")))) { + return new ErrorCode(ErrorCodeEnum.MISSING_NECESSARY_QUERYPARAM, KeysConstants.MISSING_NECESSARY_QUERYPARAM); + } + // create new applciation + if (StringUtils.isEmpty(ver.getVersionId())){ + if ((StringUtils.isEmpty(ver.getFilePath()) || ver.getSize() == 0 || ver.getVersionCreateDate() == null )) { + return new ErrorCode(ErrorCodeEnum.MISSING_NECESSARY_QUERYPARAM, KeysConstants.MISSING_NECESSARY_QUERYPARAM); + } else { + // check appId + AppExample appExample = new AppExample(); + Criteria uc = appExample.createCriteria(); + uc.andAppIdEqualTo(ver.getvAppId()); + if (appCore.countByExample(appExample) <= 0) { + return new ErrorCode(ErrorCodeEnum.INVALID_QUERYPARAM, KeysConstants.INVALID_QUERYPARAM); + } + } + } + return null; + } + + /*** + * get application list + * @param appTypeId + * @param deviceTypeId + * @param isPublic + * @param order + * @param sort + * @param offset + * @param limit + * @param request + * @return ErrorCode + */ + private ErrorCode checkParamSearch(String appTypeId, String deviceTypeId, String isPublic, String order, + String sort, int offset, int limit, HttpServletRequest request) { + ErrorCode ret = null; + + if (offset < ApiParam.API_PARAM_CRITICAL_OFFSET || limit < ApiParam.API_PARAM_CRITICAL_LIMIT ||(!ApiParam.API_PARAM_VALUE_ORDER_ASC.equalsIgnoreCase(order) + && !ApiParam.API_PARAM_VALUE_ORDER_DESC.equalsIgnoreCase(order)) + || (ApiParam.AppQueryParam.get(sort) == null) + || (StringUtils.isNotEmpty(isPublic) && (!isPublic.equals(ApiParam.API_PARAM_DEFAULT_NOT_PUBLIC) + && !isPublic.equals(ApiParam.API_PARAM_DEFAULT_IS_PUBLIC)))) { + ret = new ErrorCode(ErrorCodeEnum.MISSING_NECESSARY_QUERYPARAM, KeysConstants.MISSING_NECESSARY_QUERYPARAM); + } + + return ret; + } +} diff --git a/webservice/src/main/java/app/market/webservice/authority/AuthorityRestController.java b/webservice/src/main/java/app/market/webservice/authority/AuthorityRestController.java new file mode 100644 index 0000000..868fa63 --- /dev/null +++ b/webservice/src/main/java/app/market/webservice/authority/AuthorityRestController.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.webservice.authority; + +import java.util.List; + +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import app.market.common.comm; +import app.market.core.authority.AuthorityCore; +import app.market.utils.json.JsonMapperUtils; +import app.market.utils.property.Option; +import app.market.utils.webservice.WebServiceURI; +import app.market.webservice.WebserviceRestBaseController; + +@RestController +public class AuthorityRestController extends WebserviceRestBaseController { + + private static Logger logger = LoggerFactory.getLogger( AuthorityRestController.class ); + + @Autowired + private AuthorityCore authorityCore; + + /** + * get authority list + * + * @param userExample + * @return + */ + @RequestMapping(value = WebServiceURI.REST_AUTHORITY_GET_LIST_OPTION_LF, method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE) + public String selectAuthorityListOption(HttpServletResponse response) { + logger.debug(" selectAuthorityListOption --S--"); + String responseStr = null; + + try { + List