feat: complete reverse engineering, standard Maven structure, detailed code annotations and IoT-M platform architecture

This commit is contained in:
rick committed 2026-11-16 14:35:53 +08:00
1 parent e8dc09fb3c
commit cd6a4c70e7
3045 files changed
+566733 -1

No files matched your search

@@ -0,0 +1,82 @@
package com.midm.controller;
import com.google.common.base.Strings;
import com.midm.model.org.User;
import com.midm.security.UserPrincipal;
import com.midm.service.org.SiteService;
import com.midm.service.org.UserService;
import com.midm.service.system.LocaleService;
import com.mitac.cell.utils.McStringUtils;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.servlet.LocaleResolver;
public class BaseController {
@Autowired
protected LocaleResolver localeResolver;
@Autowired
protected LocaleService localeService;
@Autowired
protected UserService userService;
@Autowired
protected SiteService siteService;
protected User user = null;
protected String theme = "default";
protected void init(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
this.init(request, response, authentication, (String)null);
}
protected void init(HttpServletRequest request, HttpServletResponse response, Authentication authentication, String localeTag) {
this.determineUser(request, authentication);
String userLocale = !Strings.isNullOrEmpty(localeTag) ? localeTag : (this.user != null ? this.user.getSettingAsString("language") : request.getLocale().toLanguageTag());
if (McStringUtils.isNullOrEmpty(userLocale)) {
userLocale = request.getLocale().toLanguageTag();
}
Locale altLocale = this.localeService.findAltLocale(userLocale);
if (this.user != null) {
String altLocaleTag = altLocale.toLanguageTag();
if (userLocale == null || !this.user.isSameLocale(altLocaleTag)) {
this.userService.changeLocale(this.user, altLocaleTag);
}
}
this.localeResolver.setLocale(request, response, altLocale);
}
protected String getView(Authentication authentication, String viewName) {
return this.getResource(authentication, viewName + ".html");
}
protected String getResource(Authentication authentication, String fileName) {
User user = UserPrincipal.getUser(authentication);
String theme = user == null ? "default" : user.getTheme();
return theme + fileName;
}
protected String getTemplateResource(Authentication authentication, String fileName) {
if (this.user == null) {
this.user = UserPrincipal.getUser(authentication);
}
String theme = this.user == null ? "default" : this.user.getTheme();
return "templates/" + theme + fileName;
}
private void determineUser(HttpServletRequest request, Authentication authentication) {
if (authentication != null) {
synchronized(authentication) {
this.user = UserPrincipal.getUser(authentication);
this.theme = this.user != null ? this.user.getTheme() : "default";
}
} else {
this.user = null;
this.theme = "default";
}
}
}
@@ -0,0 +1,236 @@
package com.midm.controller;
import com.midm.model.device.Device;
import com.midm.model.org.Site;
import com.midm.model.org.User;
import com.midm.model.org.UserRole;
import com.midm.model.org.UserToken;
import com.midm.service.device.DeviceService;
import com.midm.service.message.WebSocketSessionManager;
import com.mitac.cell.internet.http.McRequest;
import com.mitac.cell.utils.McUuidUtils;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
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.servlet.view.RedirectView;
/**
* ======================================================================================
* 移动终端设备远程接入与控制路由控制器 (DeviceController)
* ======================================================================================
*
* <p>【架构设计与安全亮点】
* 1. 业务场景:
* - 管理员在监控大屏或告警邮件中点击设备远程查看链接,一键无感知跳转至设备的远程桌面或管理详情;
* - 设备维护人员使用移动端或外链进行快速应急远程排障(Remote Assist)。
* 2. 【安全机制一:一次性动态安全令牌 (OTT - One-Time Token)】:
* - 在 `GET /device/{accessToken}` 中,Token 经 Base64 编码,且类型标记为 'D'(Device Token);
* - 验证通过后**立即从数据库中物理删除**({@link #remoteView} 第 56 行),从根源杜绝重放攻击(Replay Attack);
* - 严格校验 Token 是否过期(isExpired)以及租户数据隔离边界。
* 3. 【安全机制二:多租户 RBAC 权限与版本授权控制】:
* - 严格校验操作人是否归属于设备所在租户(Site);
* - 校验用户角色是否拥有 `DEVICE_MANAGE_ROLES` 权限;
* - 针对租户订阅版本(如基础版 Lite vs 企业版 Pro),对高级功能标签页(如 WebRTC 远程控制 remoteControl、GPS实时轨迹 location)进行动态降级限制。
*
* @author MiDM Core Platform Team
*/
@Controller
public class DeviceController extends BaseController {
/** 设备详情页支持的功能标签页集合 */
private static List tabs = Arrays.asList("general", "remoteControl", "apps", "files", "location", "logs");
@Autowired
private DeviceService deviceService;
@Autowired
private WebSocketSessionManager sessionManager;
/**
* 【安全快捷通道】基于一次性动态 Token 的设备远程控制接入接口
*
* @param request HTTP 请求
* @param response HTTP 响应
* @param authentication 当前 Spring Security 认证上下文
* @param model 视图模型
* @param accessToken Base64 编码的一次性设备访问令牌
* @param language 国际化语言(默认 "en-US")
* @param tab 目标定位标签页(如 "remoteControl" 远程桌面、"location" GPS轨迹,默认 "general")
* @return 重定向到单页面应用前端详情路由的 RedirectView
* @throws IOException 网络 IO 异常
*/
@RequestMapping(
value = {"/device/{accessToken}"},
method = {RequestMethod.GET}
)
public RedirectView remoteView(HttpServletRequest request, HttpServletResponse response,
Authentication authentication, Model model,
@PathVariable String accessToken,
@RequestParam(required = false, defaultValue = "en-US") String language,
@RequestParam(required = false, defaultValue = "general") String tab) throws IOException {
// 1. 获取当前会话登录的用户身份
User loggedUser = this.userService.authorizedUser(authentication, (String)null);
if (authentication != null) {
UserToken prevToken = (UserToken)authentication.getCredentials();
if (!this.userService.existsById(UserToken.class, prevToken)) {
this.logout(loggedUser);
}
}
// 2. 解码并反序列化 Token UUID
accessToken = URLDecoder.decode(accessToken, Charset.defaultCharset());
UUID tokenUuid = McUuidUtils.toUuidFromBase64(accessToken);
UserToken deviceToken = this.userService.findToken(tokenUuid);
// 3. 安全性严格校验:必须存在且类型为 'D' (Device Token),且未过期
if (deviceToken != null && deviceToken.getTokenType() == 'D' && !deviceToken.isExpired()) {
// 【核心安全点:一次性销毁防重放】
this.userService.delete(deviceToken);
UUID requesterUuid = deviceToken.getUserUuid();
// 若当前登录人与发券人不一致,先清除旧会话
if (loggedUser != null && !loggedUser.getUuid().equals(requesterUuid)) {
this.logout(loggedUser);
loggedUser = null;
}
// 自动为请求发起者建立登录认证态
if (loggedUser == null) {
loggedUser = this.userService.findUserByUserUuid(requesterUuid);
authentication = this.userService.authorizeLogin(loggedUser);
}
loggedUser.setProfile("forRemote", true);
this.init(request, response, authentication, language);
// 4. 定位目标设备(优先在线实例)并校验租户归属隔离
UUID deviceUuid = deviceToken.getDeviceUuid();
Device device = this.deviceService.findOnlineDeviceFirst(deviceUuid);
if (device != null && this.user.isBelongedToSite(device.getSiteUuid())) {
if (!tabs.contains(tab)) {
tab = "general";
}
// 5. 商业版本功能门限鉴权:若租户为 Lite 简版且未购买该功能,强制降级到概要标签
Site site = this.siteService.findByUuid(device.getSiteUuid());
if (!tab.equals("general") && site.isLite() && !site.isFeatureAllowed("device", tab)) {
tab = "general";
}
// 跳转前端对应功能视图
return this.remoteView(request, device, tab);
} else {
this.logout(loggedUser);
response.sendError(401);
return null;
}
} else {
response.sendError(400, "The remote control URL is unavailable.");
if (authentication != null) {
this.logout(loggedUser);
}
return null;
}
}
/**
* 【凭证直连通道】基于企业邮箱、密码/凭证和设备序列号直接接入远程管理
*/
@RequestMapping(
value = {"/device/{email}/{credential}/{serialNo}"},
method = {RequestMethod.GET}
)
public RedirectView remoteView(HttpServletRequest request, HttpServletResponse response,
Authentication authentication, Model model,
@PathVariable String email,
@PathVariable String credential,
@PathVariable String serialNo,
@RequestParam(required = false, defaultValue = "en-US") String language,
@RequestParam(required = false, defaultValue = "general") String tab) throws IOException {
// 1. 查找目标硬件设备
Device device = this.deviceService.findBySerialNo(serialNo);
if (device == null) {
this.logout(this.user);
response.sendError(401, "The device may not exist.");
return null;
} else {
Site site = this.siteService.findByUuid(device.getSiteUuid());
User user = this.userService.authorizedUser(authentication, (String)null);
if (user != null && !user.getEmail().equals(email)) {
this.logout(user);
user = null;
}
// 2. 校验用户身份合法性
if (user == null) {
user = this.userService.findUserByEmail(site.getUuid(), email);
if (user == null || !user.getEnabled() || !user.isBelongedToSite(site)) {
response.sendError(401, "The account may not exist or disabled or not belonged to the site.");
return null;
}
}
authentication = this.userService.updateAuthorizedUser(user);
// 3. 校验设备管理角色权限
if (!user.canHandle(device.getSiteUuid(), UserRole.DEVICE_MANAGE_ROLES)) {
this.logout(user);
response.sendError(401, "The user doesn't have the role to manage the device.");
return null;
} else if (site != null && (site.isCredentialMatched(credential) || this.userService.matchPassword(user, credential))) {
// 4. 凭证比对成功
if (!tabs.contains(tab)) {
tab = "general";
}
if (!tab.equals("general") && site.isLite() && !site.isFeatureAllowed("device", tab)) {
tab = "general";
}
user.setProfile("forRemote", true);
this.init(request, response, authentication, language);
return this.remoteView(request, device, tab);
} else {
response.sendError(401, "The site credential or password is mismatched.");
return null;
}
}
}
/**
* 安全退出并彻底销毁会话与 SecurityContext
*/
private void logout(User loggedUser) {
if (loggedUser != null) {
this.sessionManager.logout(loggedUser);
SecurityContextHolder.getContext().setAuthentication((Authentication)null);
this.userService.deleteLoginToken(loggedUser);
}
}
/**
* 构建单页面应用前端重定向路由地址
*/
private RedirectView remoteView(HttpServletRequest request, Device device, String tab) {
String url = McRequest.getBaseUrl(request, String.format("#/device/details/%s/%s/%s", device.getUuid(), device.getSerialNo(), tab));
url = URLDecoder.decode(url, Charset.defaultCharset());
RedirectView view = new RedirectView(url);
return view;
}
}
@@ -0,0 +1,123 @@
package com.midm.controller;
import com.google.common.base.Strings;
import com.mitac.cell.utils.McStringUtils;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class ExceptionController extends BaseController implements ErrorController {
@RequestMapping(
value = {"error"},
method = {RequestMethod.GET}
)
public String exceptionHandler(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "JSESSIONID",defaultValue = "") String sessionId, Model model, @RequestParam(required = false) String language) {
int errorCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
if (!Strings.isNullOrEmpty(sessionId)) {
this.init(request, (HttpServletResponse)null, authentication, language);
String contextPath = (String)request.getAttribute("javax.servlet.forward.context_path");
String path = (String)request.getAttribute("javax.servlet.error.request_uri");
if (errorCode == 404) {
if (authentication == null) {
return "redirect:/";
} else {
String route = path.substring(contextPath.length());
return "forward:/#" + route;
}
} else {
Error error = new Error(errorCode);
String message = (String)request.getAttribute("javax.servlet.error.message");
if (!Strings.isNullOrEmpty(message)) {
error.setMessage(message);
}
Exception ex = (Exception)request.getAttribute("javax.servlet.error.exception");
if (ex != null) {
error.setException(ex);
}
model.addAttribute("path", path);
model.addAttribute("error", error);
String stackTrace = McStringUtils.stackTrace(ex);
model.addAttribute("stackTrace", stackTrace);
return this.getView(authentication, "/error");
}
} else {
try {
response.sendError(errorCode);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
@RequestMapping({"generate_204", "gen_204"})
public void gen204(HttpServletRequest request, HttpServletResponse response) {
try {
response.sendError(204);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(
value = {"blocked"},
method = {RequestMethod.GET}
)
public String blocked(HttpServletRequest request, Authentication authentication, Model model, @RequestParam(required = false) String url, @RequestParam(required = false) String serialNo, @RequestParam(required = false) String language) {
this.init(request, (HttpServletResponse)null, authentication, language);
model.addAttribute("url", URLDecoder.decode(url, Charset.defaultCharset()));
model.addAttribute("serialNo", serialNo);
Error error = new Error(408);
model.addAttribute("error", error);
return this.getView(authentication, "/error");
}
private class Error {
private int code;
private Exception exception;
private String message;
public Error(int code) {
this.setCode(code);
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public Exception getException() {
return this.exception;
}
public void setException(Exception exception) {
this.exception = exception;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
}
}
@@ -0,0 +1,79 @@
package com.midm.controller;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class FragmentController extends BaseController {
@RequestMapping(
value = {"/view/**"},
method = {RequestMethod.GET}
)
public String view(HttpServletRequest request, HttpServletResponse response, Authentication authentication, Model model) {
this.init(request, response, authentication);
model.addAttribute("theme", this.theme);
if (authentication == null) {
model.addAttribute("hasUser", this.userService.hasUser());
}
return this.getView(authentication, request.getServletPath());
}
@RequestMapping(
value = {"/view/system/resource"},
method = {RequestMethod.GET}
)
public String resourceView(HttpServletRequest request, HttpServletResponse response, Authentication authentication, Locale locale, Model model, @RequestParam String category) {
super.init(request, response, authentication);
model.addAttribute("category", category);
return "template/system/resource.html";
}
@RequestMapping(
value = {"/fragment/{fragmentName}"},
method = {RequestMethod.GET}
)
public String fragment(HttpServletRequest request, HttpServletResponse response, Authentication authentication, Locale locale, Model model, @PathVariable String fragmentName) {
super.init(request, response, authentication);
model.addAttribute("theme", this.theme);
if (authentication == null) {
model.addAttribute("hasUser", this.userService.hasUser());
}
String var10000 = this.getView(authentication, request.getServletPath());
return var10000 + " :: fragment";
}
@RequestMapping(
value = {"/fragment/{fragmentName}/{blockName}"},
method = {RequestMethod.GET}
)
public String fragment(HttpServletRequest request, HttpServletResponse response, Authentication authentication, Locale locale, Model model, @PathVariable String fragmentName, @PathVariable String blockName) {
super.init(request, response, authentication);
String var10000 = this.getView(authentication, "/fragment/" + fragmentName);
return var10000 + " :: " + blockName;
}
@RequestMapping(
value = {"/dialog/{dialogName}"},
method = {RequestMethod.GET}
)
public String dialog(HttpServletRequest request, HttpServletResponse response, Authentication authentication, Locale locale, Model model, @PathVariable String dialogName, @RequestParam(required = false) String category) {
this.init(request, response, authentication);
model.addAttribute("theme", this.theme);
if (authentication == null) {
model.addAttribute("hasUser", this.userService.hasUser());
}
String var10000 = this.getView(authentication, request.getServletPath());
return var10000 + " :: dialog";
}
}
@@ -0,0 +1,273 @@
package com.midm.controller;
import com.google.common.base.Strings;
import com.midm.model.org.Site;
import com.midm.model.org.UserToken;
import com.midm.security.UserPrincipal;
import com.mitac.cell.internet.http.McResponse;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
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.servlet.view.RedirectView;
@Controller
public class HomeController extends BaseController {
@RequestMapping(
value = {"/", "/index.html"},
method = {RequestMethod.GET}
)
public String home(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, @CookieValue(value = "site.uuid",defaultValue = "") UUID siteUuid, @CookieValue(value = "locale",defaultValue = "en-US") String locale, Model model, @RequestHeader("host") String hostName, @RequestParam(required = false) String language, @RequestParam(required = false) String siteId) {
if (Strings.isNullOrEmpty(language)) {
language = locale;
}
if (authentication != null) {
UserToken token = (UserToken)authentication.getCredentials();
UserToken existingToken = token == null ? null : this.userService.findToken(token.getTokenUuid());
if (existingToken == null) {
SecurityContextHolder.getContext().setAuthentication((Authentication)null);
authentication = null;
if (token != null && token.getTokenUuid().equals(tokenUuid)) {
tokenUuid = null;
McResponse.setCookie(response, "user.token", (String)null);
}
}
}
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
if (authentication == null && tokenUuid != null) {
UserToken token = this.userService.findToken(tokenUuid);
if (token != null) {
authentication = this.userService.authorizeLogin(tokenUuid);
} else {
McResponse.setCookie(response, "user.token", (String)null);
}
}
if (authentication == null) {
if (!Strings.isNullOrEmpty(siteId)) {
Site site = siteId.equals("default") ? null : this.siteService.findByIdName(siteId);
if (site != null) {
siteUuid = site.getUuid();
model.addAttribute("siteUuid", siteUuid);
McResponse.setCookie(response, "site.uuid", siteUuid.toString());
} else {
siteUuid = null;
}
}
if (siteUuid != null) {
model.addAttribute("siteUuid", siteUuid);
}
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "login");
model.addAttribute("locale", language);
if (locale != null && language != null && !locale.equals(language)) {
McResponse.setCookie(response, "locale", language);
}
return this.getView(authentication, "/login");
} else {
this.user = UserPrincipal.getUser(authentication);
if (this.user == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "login");
model.addAttribute("locale", language);
if (locale != null && language != null && !locale.equals(language)) {
McResponse.setCookie(response, "locale", language);
}
return this.getView(authentication, "/login");
} else {
return this.getView(authentication, "/index");
}
}
}
@RequestMapping(
value = {"/remoteLogout"},
method = {RequestMethod.GET}
)
public String home(HttpServletRequest request, HttpServletResponse response, Authentication authentication, Model model, @RequestParam(required = false) String language) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
return this.getView(authentication, "/remoteLogout");
}
@RequestMapping(
value = {"/"},
method = {RequestMethod.POST}
)
public void homePost(HttpServletRequest request, HttpServletResponse response) {
try {
response.sendError(400);
} catch (IOException var4) {
}
}
@RequestMapping(
value = {"register"},
method = {RequestMethod.GET}
)
public String registerToModelSite(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, @RequestParam String modelSite, ModelMap model, @RequestParam(required = false) String language) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UserToken token = null;
if (authentication == null && tokenUuid != null) {
token = this.userService.findToken(tokenUuid);
authentication = this.userService.authorizeLogin(tokenUuid);
}
if (authentication == null && token == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "register");
return this.getView(authentication, "/login");
} else {
return "forward:/";
}
}
@RequestMapping(
value = {"{modelSite}/register"},
method = {RequestMethod.GET}
)
public RedirectView redirectToRegister(ModelMap model, @PathVariable String modelSite, @RequestParam(required = false) String language) {
model.clear();
return new RedirectView("../register?modelSite=" + modelSite);
}
@RequestMapping(
value = {"/bypass", "/dpc"},
method = {RequestMethod.GET}
)
public String bypassMode(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, ModelMap model, @RequestParam(required = false) String language, @RequestHeader("host") String hostName) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UserToken token = null;
if (authentication == null && tokenUuid != null) {
token = this.userService.findToken(tokenUuid);
authentication = this.userService.authorizeLogin(tokenUuid);
}
if (authentication == null && token == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "dpc");
return this.getView(authentication, "/login");
} else {
this.user = UserPrincipal.getUser(authentication);
if (this.user.getSettingAsBoolean("keepSignIn")) {
if (token == null && authentication != null) {
token = (UserToken)authentication.getCredentials();
}
if (token != null) {
Cookie cookie = new Cookie("user.token", token.getTokenUuid().toString());
response.addCookie(cookie);
}
} else {
Cookie cookie = new Cookie("user.token", (String)null);
response.addCookie(cookie);
}
return this.getView(authentication, "/index");
}
}
@RequestMapping(
value = {"{modelSite}/bypass", "{modelSite}/dpc"},
method = {RequestMethod.GET}
)
public String bypassModeWithModel(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, ModelMap model, @RequestParam(required = false) String language, @RequestHeader("host") String hostName) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UserToken token = null;
if (authentication == null && tokenUuid != null) {
token = this.userService.findToken(tokenUuid);
authentication = this.userService.authorizeLogin(tokenUuid);
}
if (authentication == null && token == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "dpc");
return this.getView(authentication, "/login");
} else {
this.user = UserPrincipal.getUser(authentication);
if (this.user.getSettingAsBoolean("keepSignIn")) {
if (token == null && authentication != null) {
token = (UserToken)authentication.getCredentials();
}
if (token != null) {
Cookie cookie = new Cookie("user.token", token.getTokenUuid().toString());
response.addCookie(cookie);
}
} else {
Cookie cookie = new Cookie("user.token", (String)null);
response.addCookie(cookie);
}
return this.getView(authentication, "/index");
}
}
@RequestMapping(
value = {"/requestTrial"},
method = {RequestMethod.GET}
)
public String requestTrial(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, ModelMap model, @RequestParam(required = false) String language, @RequestHeader("host") String hostName) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UserToken token = null;
if (authentication == null && tokenUuid != null) {
token = this.userService.findToken(tokenUuid);
authentication = this.userService.authorizeLogin(tokenUuid);
}
if (authentication == null && token == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "requestTrial");
return this.getView(authentication, "/login");
} else {
this.user = UserPrincipal.getUser(authentication);
if (this.user.getSettingAsBoolean("keepSignIn")) {
if (token == null && authentication != null) {
token = (UserToken)authentication.getCredentials();
}
if (token != null) {
Cookie cookie = new Cookie("user.token", token.getTokenUuid().toString());
response.addCookie(cookie);
}
} else {
Cookie cookie = new Cookie("user.token", (String)null);
response.addCookie(cookie);
}
return this.getView(authentication, "/index");
}
}
@RequestMapping(
method = {RequestMethod.OPTIONS, RequestMethod.GET},
path = {"/login/{siteId}"}
)
public String forwardToLogin(@PathVariable String siteId) {
return "redirect:/?siteId=" + siteId;
}
}
@@ -0,0 +1,60 @@
package com.midm.controller;
import com.midm.model.org.User;
import com.midm.model.org.UserToken;
import com.mitac.cell.utils.McUuidUtils;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestHeader;
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.ResponseBody;
import org.springframework.web.server.NotAcceptableStatusException;
@Controller
public class UserController extends BaseController {
@RequestMapping(
value = {"/resetPassword"},
method = {RequestMethod.GET}
)
public String home(HttpServletRequest request, HttpServletResponse response, Authentication authentication, Model model, @RequestParam(name = "token") String passwordTokenUid, @RequestParam(required = false) String language) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UUID passwordTokenUuid = McUuidUtils.toUuidFromBase64(passwordTokenUid);
UserToken token = this.userService.findToken(passwordTokenUuid);
if (token != null && token.isExpired()) {
this.userService.deleteTokenByType(token);
}
if (token != null && token.getTokenType() == 'P') {
model.addAttribute("error", false);
User user = this.userService.findUserByUserUuid(token.getUserUuid());
model.addAttribute("token", token);
model.addAttribute("user", user);
return this.getView(authentication, "/resetPassword");
} else {
model.addAttribute("error", true);
return this.getView(authentication, "/resetPassword");
}
}
@RequestMapping(
value = {"/keepSession"},
method = {RequestMethod.GET},
produces = {"text/plain"}
)
@ResponseBody
public String getSettings(HttpServletRequest request, Authentication authentication, @RequestHeader(value = "Authorization",required = false) String accessToken) {
User loggedUser = this.userService.authorizedUser(authentication, accessToken);
if (loggedUser == null) {
throw new NotAcceptableStatusException("");
} else {
return "ok";
}
}
}
@@ -0,0 +1,21 @@
package com.midm.controller.lib;
import com.mitac.cell.barcode.QrCode;
import com.mitac.cell.io.McResource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class BarcodeController {
@RequestMapping(
value = {"/barcode/qr/{code}"},
method = {RequestMethod.GET}
)
public void qrCode(HttpServletResponse response, @PathVariable String code, @RequestParam(required = false) Integer size, @RequestParam(required = false) Integer margin, @RequestParam(required = false) Boolean encrypt) {
McResource.copyToResponse(response, QrCode.toImage(code, size, margin));
}
}
@@ -0,0 +1,598 @@
package com.midm.controller.lib;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.io.Files;
import com.midm.model.device.Device;
import com.midm.model.file.AppFile;
import com.midm.model.file.BaseFile;
import com.midm.model.org.User;
import com.midm.security.UserPrincipal;
import com.midm.service.cloudStorage.CloudStorageService;
import com.midm.service.device.DeviceService;
import com.midm.service.file.FileService;
import com.midm.service.file.ReportService;
import com.midm.service.store.StoreAppService;
import com.midm.service.store.StoreOtaService;
import com.midm.service.store.StoreOtherService;
import com.mitac.cell.io.McFile;
import com.mitac.cell.io.McResource;
import com.mitac.cell.utils.McUrlUtils;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
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.ResponseBody;
import org.springframework.web.servlet.HandlerMapping;
@Controller
public class FilesController {
@Autowired
private ServletContext servletContext;
@Autowired
private FileService fileService;
@Autowired
private StoreAppService storeAppService;
@Autowired
private StoreOtaService storeOtaService;
@Autowired
private DeviceService deviceService;
@Autowired
private ReportService reportService;
@Autowired
private StoreOtherService storeOtherService;
@Autowired
private CloudStorageService cloudStorageService;
@RequestMapping(
value = {"userfile/{userUuid}/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getUserFile(HttpServletResponse response, Authentication authentication, @PathVariable UUID userUuid, @PathVariable UUID fileUuid, @PathVariable String fileName, @RequestParam(required = false) Long position, @RequestParam(required = false) Long crc32) {
String filePath = this.fileService.getUserFilePath(userUuid, fileUuid);
McFile file = new McFile(filePath);
McResource.copyFileToResponse(this.servletContext, response, file, fileName, 0, -1);
file.delete();
}
@RequestMapping(
value = {"file/device/{deviceUuid}/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void geDevicetFile(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable UUID deviceUuid, @PathVariable UUID fileUuid, @PathVariable String fileName) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
String filePath = this.fileService.getDeviceFilePath(user.getSiteUuid(), deviceUuid, "file", fileUuid);
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, 0, -1);
}
}
@RequestMapping(
value = {"file/{fileType}/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getFile(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable String fileType, @PathVariable UUID fileUuid, @PathVariable String fileName) {
User user = UserPrincipal.getUser(authentication);
try {
if (user == null) {
response.sendError(406, "User not logged in.");
return;
}
BaseFile file = this.findFile(response, fileUuid, fileType, true, user.getSiteUuid());
if (file == null || file.hasFileUrl()) {
return;
}
String filePath = this.fileService.getFilePath(user.getSiteUuid(), fileType, fileUuid);
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, 0, -1);
} catch (IOException var10) {
}
}
@RequestMapping(
value = {"file/report/template/{fileName}"},
method = {RequestMethod.GET}
)
public void getReportTemplate(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable String fileName) {
String filePath = this.reportService.getTemplateFilePath(fileName);
McFile file = new McFile(filePath);
if (!file.exists()) {
file = this.fileService.getResourceFile("report/" + fileName, false);
}
McResource.copyFileToResponse(this.servletContext, response, file, fileName, 0, -1);
}
@RequestMapping(
value = {"must/reserved/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getMustReservedFile(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable UUID fileUuid, @PathVariable String fileName) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
String filePath = this.fileService.getMustDownloadFilePath(user.getSiteUuid(), fileUuid, "region");
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, 0, -1);
}
}
@RequestMapping(
value = {"must/download/{isDirectory}/{fileUuid}/{fileName}/**"},
method = {RequestMethod.GET}
)
public void getMustDownloadFile(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable UUID fileUuid, @PathVariable String fileName, @PathVariable String isDirectory) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
boolean isDir = Boolean.parseBoolean(isDirectory);
String path = (String)request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String[] tempPath = path.split("/", 7);
String filePath = this.fileService.getMustFolderFilePath(user.getSiteUuid(), fileUuid, tempPath[6]);
if (isDir) {
filePath = this.fileService.getMustDownloadProfilePath(user.getSiteUuid(), fileUuid, "tempDownlad", fileName);
fileName = fileName + ".zip";
}
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, 0, -1);
}
}
@RequestMapping(
value = {"store/{fileType}/{packageName}/{versionCode}/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getStoreAppFile(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable String fileType, @PathVariable String packageName, @PathVariable long versionCode, @PathVariable UUID fileUuid, @PathVariable String fileName) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
String filePath = this.storeAppService.getStoreAppPath(packageName, versionCode, fileUuid);
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, 0, -1);
}
}
@RequestMapping(
value = {"store/ota/{productId}/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getStoreOtaFile(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable String productId, @PathVariable UUID fileUuid, @PathVariable String fileName) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
String filePath = this.storeOtaService.getStoreOtaFilePath(productId, fileUuid);
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, 0, -1);
}
}
@RequestMapping(
value = {"store/other/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getStoreOtherFile(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable UUID fileUuid, @PathVariable String fileName) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
String filePath = this.storeOtherService.getStoreFilePath("other", fileUuid);
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, 0, -1);
}
}
@RequestMapping(
value = {"{siteUuid}/file/{fileType}/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getFile(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable String siteUuid, @PathVariable String fileType, @PathVariable String fileUuid, @PathVariable String fileName, @RequestHeader(name = "range",required = false) String range, @RequestHeader(name = "if-match",required = false) String etag, @RequestParam(name = "position",required = false,defaultValue = "0") Integer from) {
try {
BaseFile file = this.findFile(response, UUID.fromString(fileUuid), fileType, false, UUID.fromString(siteUuid));
if (file == null || file.hasFileUrl()) {
return;
}
String checksum = String.valueOf(file.getChecksum());
if (etag != null && !etag.equals(checksum)) {
range = "";
}
String filePath = this.fileService.getFilePath(siteUuid, fileType, fileUuid);
Integer to = -1;
if (!Strings.isNullOrEmpty(range)) {
range = range.substring(range.indexOf(61) + 1);
if (range.indexOf(44) >= 0) {
response.sendError(400, "Multiple ranges not supported.");
return;
}
boolean isRangeOk = true;
if (range.indexOf(45) >= 0) {
String[] tokens = range.split("-");
if (tokens.length > 0) {
if (Strings.isNullOrEmpty(tokens[0])) {
to = (int)file.getLength() - 1;
from = to - Integer.valueOf(tokens[1]) + 1;
} else {
from = Integer.valueOf(tokens[0]);
if (tokens.length > 1) {
if (Strings.isNullOrEmpty(tokens[1])) {
to = (int)file.getLength() - 1;
} else {
to = Integer.valueOf(tokens[1]);
}
} else {
to = (int)file.getLength() - 1;
}
}
} else {
isRangeOk = false;
}
}
if (!isRangeOk) {
response.sendError(400, "Range is invalid.");
return;
}
}
response.addHeader("ETag", checksum);
response.addHeader("If-Match", checksum);
response.setDateHeader("Last-Modified", file.getLastModified());
response.addHeader("Accept-Ranges", "bytes");
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, from, to);
} catch (Exception var17) {
}
}
@RequestMapping(
value = {"icon/app/{fileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getAppIcon(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable UUID fileUuid, @PathVariable String fileName) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
if (this.cloudStorageService.hasCloudStorage()) {
AppFile file = this.fileService.findApp(fileUuid, true);
if (file.hasIconUrl()) {
try {
String url = file.getIconUrl();
response.sendRedirect(url);
return;
} catch (IOException var11) {
}
}
String url = this.cloudStorageService.getUrl(user.getSiteUuid(), "app", fileUuid + ".png");
if (!Strings.isNullOrEmpty(url)) {
try {
file.setIconUrl(url);
response.sendRedirect(url);
return;
} catch (IOException var10) {
}
}
} else {
String apkPath = this.fileService.getFilePath(user.getSiteUuid(), "app", fileUuid);
McFile iconFile = new McFile(apkPath + ".png");
if (iconFile.exists()) {
byte[] iconData = iconFile.toBytes();
if (iconData != null) {
String var10000 = Files.getNameWithoutExtension(fileName);
fileName = var10000 + (iconData[0] == 60 ? ".svg" : ".png");
fileName = McUrlUtils.encode(fileName);
McResource.copyFileToResponse(this.servletContext, response, iconData, fileName);
return;
}
}
}
this.getMimeIcon(request, response, authentication, "apk");
}
}
@RequestMapping(
value = {"icon/must/app/{profileUuid}/{fileName}"},
method = {RequestMethod.GET}
)
public void getMustSiteAppIcon(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable UUID profileUuid, @PathVariable String fileName) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
String iconFilePath = this.fileService.getMustFolderFilePath(user.getSiteUuid(), profileUuid, "apps/" + fileName + ".png");
McFile iconFile = new McFile(iconFilePath);
if (!iconFile.exists()) {
}
byte[] iconData = iconFile.toBytes();
if (iconData != null) {
McResource.copyFileToResponse(this.servletContext, response, iconData, fileName + ".png");
} else {
this.getMimeIcon(request, response, authentication, "apk");
}
}
}
@RequestMapping(
value = {"icon/store/app/{packageName}"},
method = {RequestMethod.GET}
)
public void getStoreAppIcon(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable String packageName) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
String iconFilePath = this.storeAppService.getStoreIconPath(packageName);
McFile iconFile = new McFile(iconFilePath);
byte[] iconData = iconFile.toBytes();
if (iconData != null) {
McResource.copyFileToResponse(this.servletContext, response, iconData, packageName + ".png");
} else {
this.getMimeIcon(request, response, authentication, "apk");
}
}
}
@RequestMapping(
value = {"icon/mime/{extension}"},
method = {RequestMethod.GET}
)
public void getMimeIcon(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable String extension) {
String resourcePath = Joiner.on('/').join("/image/mime", extension.toLowerCase() + ".png", new Object[0]);
if (!this.fileService.resourceExists(resourcePath, true)) {
resourcePath = "/image/mime/_blank.png";
}
forwardRequest(request, response, resourcePath);
}
@RequestMapping(
value = {"icon/folder/{folderType}"},
method = {RequestMethod.GET}
)
public void getFolderIcon(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable String folderType) {
String resourcePath = Joiner.on('/').join("/image/folder", folderType.toLowerCase() + ".png", new Object[0]);
if (!this.fileService.resourceExists(resourcePath, true)) {
resourcePath = "/image/folder/normal.png";
}
forwardRequest(request, response, resourcePath);
}
@RequestMapping(
value = {"profile/user/{userUuid}"},
method = {RequestMethod.GET}
)
public void getUserProfileImage(HttpServletRequest request, HttpServletResponse response, Authentication aHttputhentication, @PathVariable UUID userUuid) {
response.setHeader("Cache-Control", "max-age=2592000");
String profilePath = this.fileService.getUserProfilePath(userUuid);
String fileName = "profile.jpg";
if ((new File(profilePath)).exists()) {
McResource.copyFileToResponse(this.servletContext, response, new McFile(profilePath), "profile.jpg", 0, -1);
} else {
forwardRequest(request, response, "/image/icon/profileUnknown.png");
}
}
@RequestMapping(
value = {"favicon"},
method = {RequestMethod.GET}
)
public void getFavicon(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @RequestParam(required = false) UUID siteUuid) {
McFile favicon = new McFile(this.fileService.getFaviconPath());
if (favicon.exists()) {
response.setHeader("Cache-Control", "max-age=2592000");
McResource.copyFileToResponse(this.servletContext, response, favicon, favicon.getName(), 0, -1);
} else {
forwardRequest(request, response, "/image/favicon.png");
}
}
@RequestMapping(
value = {"background"},
method = {RequestMethod.GET}
)
public void getBackgroundImage(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @RequestParam(required = false) UUID siteUuid) {
if (siteUuid != null) {
String profilePath = this.fileService.getSiteFilePath(siteUuid, "background");
String fileName = "background.jpg";
McFile file = new McFile(profilePath);
if (file.exists()) {
response.setHeader("Cache-Control", "max-age=2592000");
McResource.copyFileToResponse(this.servletContext, response, new McFile(profilePath), "background.jpg", 0, -1);
} else {
forwardRequest(request, response, "/image/bg/background.jpg");
}
} else {
McFile bgFile = new McFile(this.fileService.getBackgroundPath());
if (bgFile.exists()) {
response.setHeader("Cache-Control", "max-age=2592000");
McResource.copyFileToResponse(this.servletContext, response, bgFile, bgFile.getName(), 0, -1);
} else {
forwardRequest(request, response, "/image/bg/background.jpg");
}
}
}
@RequestMapping(
value = {"background/{siteUuid}"},
method = {RequestMethod.GET}
)
public void getSitebackgroundImage(HttpServletRequest request, HttpServletResponse response, @PathVariable UUID siteUuid) {
this.getSiteImage(request, response, siteUuid, "background");
}
@RequestMapping(
value = {"logo/{siteUuid}"},
method = {RequestMethod.GET}
)
public void getSiteLogoImage(HttpServletRequest request, HttpServletResponse response, @PathVariable UUID siteUuid) {
this.getSiteImage(request, response, siteUuid, "logo");
}
@RequestMapping(
value = {"profile/site/{siteUuid}"},
method = {RequestMethod.GET}
)
public void getSiteProfileImage(HttpServletRequest request, HttpServletResponse response, @PathVariable UUID siteUuid) {
this.getSiteImage(request, response, siteUuid, "profile");
}
public void getSiteImage(HttpServletRequest request, HttpServletResponse response, UUID siteUuid, String type) {
if (this.cloudStorageService.hasCloudStorage()) {
String url = this.cloudStorageService.getUrl(siteUuid, type);
if (!Strings.isNullOrEmpty(url)) {
try {
response.sendRedirect(url);
return;
} catch (Exception var10) {
}
}
}
McFile imgFile = new McFile(this.fileService.getSiteFilePath(siteUuid, type));
String fileName = "";
switch (type) {
case "background":
fileName = "backgroud.jpg";
break;
case "logo":
fileName = "logo.png";
break;
case "profile":
fileName = "profile.png";
}
if (imgFile.exists()) {
McResource.copyFileToResponse(this.servletContext, response, imgFile, fileName, 0, -1);
} else if (type.equals("background")) {
this.getBackgroundImage(request, response, (Authentication)null, (UUID)null);
} else if (type.equals("profile")) {
forwardRequest(request, response, "/image/icon/siteUnknown.png");
} else {
try {
response.sendError(404, "Not found.");
} catch (IOException var9) {
}
}
}
@RequestMapping(
value = {"profile/org/{userUuid}"},
method = {RequestMethod.GET}
)
public void getOrgProfileImage(HttpServletResponse response, Authentication authentication, @PathVariable UUID userUuid) {
String profilePath = this.fileService.getFilePath("user", userUuid);
String fileName = "profile.jpg";
McResource.copyFileToResponse(this.servletContext, response, new McFile(profilePath), "profile.jpg", 0, -1);
}
@RequestMapping(
value = {"device/screen/{deviceUuid}"},
method = {RequestMethod.GET}
)
public void getDeviceScreenImage(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @PathVariable UUID deviceUuid) {
User user = UserPrincipal.getUser(authentication);
if (user != null) {
String profilePath = this.fileService.getDeviceFilePath(user.getSiteUuid(), deviceUuid, "screen", "screen.png");
if (McFile.exists(profilePath)) {
McResource.copyFileToResponse(this.servletContext, response, new McFile(profilePath), "screen.png", 0, -1);
} else {
forwardRequest(request, response, "/image/empty.png");
}
}
}
@RequestMapping(
value = {"device/file/{deviceUuid}/{hostFile}/{fileName}"},
method = {RequestMethod.GET}
)
public void getDeviceFile(HttpServletResponse response, Authentication authentication, @PathVariable UUID deviceUuid, @PathVariable String hostFile, @PathVariable String fileName) {
try {
Device device = this.deviceService.findOnlineDeviceFirst(deviceUuid);
User user = UserPrincipal.getUser(authentication);
if (device != null && user != null && user.isBelongedToSite(device.getSiteUuid())) {
String filePath = this.fileService.getDeviceFilePath(device.getSiteUuid(), deviceUuid, "file", hostFile);
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), fileName, 0, -1);
} else {
response.sendError(401, "Not logged in or Unauthorized.");
}
} catch (IOException var9) {
}
}
@RequestMapping(
value = {"file/checksum/{fileUuid}"},
method = {RequestMethod.GET}
)
@ResponseBody
public String getCheckSum(Authentication authentication, @PathVariable UUID fileUuid, @RequestParam(required = false) Integer size) {
User user = UserPrincipal.getUser(authentication);
if (user == null) {
return null;
} else {
String path = this.fileService.getFilePath(user.getSiteUuid(), "other", fileUuid);
McFile file = new McFile(path);
Long checksum = file.getCrcChecksum(size);
return "" + checksum;
}
}
public static void forwardRequest(HttpServletRequest request, HttpServletResponse response, String path) {
try {
if (!response.isCommitted()) {
request.getRequestDispatcher(path).forward(request, response);
}
} catch (IOException | ServletException var6) {
try {
response.sendError(404);
} catch (IOException var5) {
}
}
}
private BaseFile findFile(HttpServletResponse response, UUID fileUuid, String fileType, boolean minify, UUID siteUuid) {
BaseFile file = null;
switch (fileType) {
case "app":
file = this.fileService.findApp(fileUuid, minify);
break;
case "ota":
file = this.fileService.findOta(fileUuid);
break;
case "other":
file = this.fileService.findFile(fileUuid);
break;
case "report":
file = this.reportService.findReportByUuid(fileUuid);
}
try {
if (file == null) {
response.sendError(404);
} else if (file.hasFileUrl()) {
response.sendRedirect(file.getFileUrl());
} else if (this.cloudStorageService.hasCloudStorage()) {
String url = this.cloudStorageService.getUrl(siteUuid, fileType, fileUuid);
if (!Strings.isNullOrEmpty(url)) {
file.setCloudUrl(url);
response.sendRedirect(url);
}
}
} catch (Exception var9) {
}
return file;
}
}
@@ -0,0 +1,81 @@
package com.midm.controller.lib;
import com.google.common.base.Strings;
import com.google.common.io.Files;
import com.midm.controller.BaseController;
import com.mitac.cell.internet.http.McResponse;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestHeader;
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.ResponseBody;
@Controller
public class ResourceController extends BaseController {
@RequestMapping(
value = {"/js/**", "/css/**"},
method = {RequestMethod.GET}
)
public String getScript(HttpServletRequest request, Authentication authentication) {
return this.getResource(authentication, request.getServletPath());
}
@RequestMapping(
value = {"js/controller/login.js"},
method = {RequestMethod.GET},
produces = {"application/javascript"}
)
public String getLoginScript(HttpServletRequest request, Model model, Authentication authentication, @RequestHeader("host") String hostHeader, @RequestParam(required = false) String hostParam, @RequestParam(required = false,defaultValue = "login") String pageFor) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", pageFor);
return this.getResource(authentication, request.getServletPath());
}
@RequestMapping(
value = {"/js/controller/resetPassword.js"},
method = {RequestMethod.GET},
produces = {"application/javascript"}
)
public String getResetPasswordScript(HttpServletRequest request, Model model, Authentication authentication, @RequestParam(required = false) UUID userUuid, @RequestParam(required = false) UUID tokenUuid) {
model.addAttribute("error", userUuid == null);
model.addAttribute("tokenUuid", tokenUuid);
model.addAttribute("userUuid", userUuid);
return this.getResource(authentication, request.getServletPath());
}
@RequestMapping(
value = {"img/**"},
method = {RequestMethod.GET}
)
@ResponseBody
public ResponseEntity getImage(HttpServletRequest request, Authentication authentication) {
String path = this.getTemplateResource(authentication, request.getServletPath());
return this.getServletResource(request, path);
}
@RequestMapping(
value = {"images/**"},
method = {RequestMethod.GET}
)
@ResponseBody
public ResponseEntity getImages(HttpServletRequest request, Authentication authentication) {
String path = this.getTemplateResource(authentication, "/img" + request.getServletPath());
return this.getServletResource(request, path);
}
private ResponseEntity getServletResource(HttpServletRequest request, String path) {
String subType = Files.getFileExtension(path);
if (Strings.isNullOrEmpty(subType)) {
subType = "jpg";
}
return McResponse.getServletResource(request, new MediaType("image", subType), path);
}
}
@@ -0,0 +1,320 @@
package com.midm.controller.lib;
import com.google.common.base.Strings;
import com.midm.model.store.AppReleaseNote;
import com.midm.model.store.KeyStore;
import com.midm.model.store.StoreApp;
import com.midm.model.store.StoreAppFile;
import com.midm.model.store.StoreOtaFile;
import com.midm.model.store.StoreOtherFile;
import com.midm.model.store.StoreWebApp;
import com.midm.service.cloudStorage.CloudStorageService;
import com.midm.service.file.FileService;
import com.midm.service.store.KeyStoreService;
import com.midm.service.store.StoreAppService;
import com.midm.service.store.StoreOtaService;
import com.midm.service.store.StoreOtherService;
import com.midm.service.store.StoreWebAppService;
import com.midm.service.system.HostLicenseService;
import com.mitac.cell.io.McFile;
import com.mitac.cell.io.McResource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
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.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
@Controller
@RequestMapping({"store"})
public class StoreController {
@Autowired
private ServletContext servletContext;
@Autowired
private StoreAppService storeAppService;
@Autowired
private StoreOtaService storeOtaService;
@Autowired
private StoreWebAppService storeWebAppService;
@Autowired
@Qualifier("stringTemplateEngine")
private TemplateEngine stringTemplateEngine;
@Autowired
private KeyStoreService keyStoreService;
@Autowired
private StoreOtherService storeOtherService;
@Autowired
private FileService fileService;
@Autowired
private CloudStorageService cloudStorageService;
@Autowired
@Lazy
private HostLicenseService hostLicenseService;
@RequestMapping(
value = {"apps/download/{packageName}"},
method = {RequestMethod.GET}
)
public void getLatestApp(HttpServletRequest request, HttpServletResponse response, @PathVariable String packageName, @RequestHeader(name = "range",required = false) String range, @RequestHeader(name = "if-match",required = false) String etag, @RequestParam(name = "checksum",required = false) String certMd5, @RequestParam(name = "keyStore",required = false) String keyStoreId, @RequestParam(required = false,defaultValue = "-1") int versionCode) {
if (Strings.isNullOrEmpty(certMd5) && !Strings.isNullOrEmpty(keyStoreId)) {
if (keyStoreId.equals("default")) {
keyStoreId = "android_default";
}
KeyStore keyStore = this.keyStoreService.find(keyStoreId);
certMd5 = keyStore.getCertMd5();
}
this.getApp(request, response, range, etag, packageName, String.valueOf(versionCode), keyStoreId, -1);
}
@RequestMapping(
value = {"apps/{packageName}/{version}/{keyStoreId}"},
method = {RequestMethod.GET}
)
public void getApp(HttpServletRequest request, HttpServletResponse response, @RequestHeader(name = "range",required = false) String range, @RequestHeader(name = "if-match",required = false) String etag, @PathVariable String packageName, @PathVariable String version, @PathVariable String keyStoreId, @RequestParam(required = false,defaultValue = "-1") int sdkVersion) {
if (Strings.isNullOrEmpty(keyStoreId) || keyStoreId.equals("default")) {
keyStoreId = "android_default";
}
KeyStore keyStore = this.keyStoreService.find(keyStoreId);
if (keyStore == null) {
try {
response.sendError(404, "No key store exists.");
} catch (IOException var14) {
}
} else {
String certMd5 = keyStore.getCertMd5();
StoreAppFile appFile = null;
if (version.equalsIgnoreCase("latest")) {
appFile = this.storeAppService.findLatestAppFile(packageName, -1, sdkVersion, certMd5);
} else {
try {
long versionCode = Long.parseLong(version);
appFile = this.storeAppService.findAppFile(packageName, versionCode, certMd5);
} catch (Exception var16) {
}
}
if (appFile != null) {
this.getApp(response, appFile, range);
} else {
try {
response.sendError(404, "No apps found.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@RequestMapping(
value = {"app/{fileUuid}"},
method = {RequestMethod.GET}
)
public void getApp(HttpServletRequest request, HttpServletResponse response, @PathVariable UUID fileUuid, @RequestHeader(name = "range",required = false) String range, @RequestHeader(name = "if-match",required = false) String etag, @RequestParam(required = false) Long position, @RequestParam(required = false) Long checksum) {
StoreAppFile appFile = this.storeAppService.findAppFile(fileUuid);
if (appFile == null) {
try {
response.sendError(404, "App file not exists.");
} catch (IOException e) {
e.printStackTrace();
}
} else {
this.getApp(response, appFile, range);
}
}
private void getApp(HttpServletResponse response, StoreAppFile appFile, String range) {
String url = this.cloudStorageService.getUrl("store", "app", appFile.getPackageName(), appFile.getVersionCode(), appFile.getFileUuid());
if (Strings.isNullOrEmpty(url)) {
String filePath = this.storeAppService.getStoreAppPath(appFile.getPackageName(), appFile.getVersionCode(), appFile.getFileUuid());
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), appFile.getFileName(), appFile.getLastModified(), range, appFile.getChecksum());
} else {
try {
response.sendRedirect(url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@RequestMapping(
value = {"apps/{packageName}/releaseNote"},
method = {RequestMethod.GET}
)
@ResponseBody
public String appReleaseNote(HttpServletRequest request, HttpServletResponse response, @PathVariable String packageName, @RequestParam(required = false,defaultValue = "-1") int sdkVersion) throws IOException {
return this.appReleaseNote(request, response, packageName, (String)null, (Integer)null, sdkVersion, true);
}
@RequestMapping(
value = {"apps/{packageName}/{keyStore}/releaseNote", "apps/{packageName}/{keyStore}/releaseNote/{pathSdkVersion}"},
method = {RequestMethod.GET}
)
@ResponseBody
public String appReleaseNote(HttpServletRequest request, HttpServletResponse response, @PathVariable String packageName, @PathVariable String keyStore, @PathVariable(required = false) Integer pathSdkVersion, @RequestParam(required = false,defaultValue = "-1") int sdkVersion, @RequestParam(required = false,defaultValue = "1") Boolean isFormal) throws IOException {
WebContext context = new WebContext(request, response, this.servletContext, request.getLocale());
StoreApp app = this.storeAppService.find(packageName);
if (pathSdkVersion != null) {
sdkVersion = pathSdkVersion;
}
List<AppReleaseNote> notes = this.storeAppService.findReleaseNotes(packageName, sdkVersion, isFormal == null ? null : true);
context.setVariable("app", app);
context.setVariable("fixedKeyStores", Arrays.asList("mio", "default"));
if (!Strings.isNullOrEmpty(keyStore)) {
context.setVariable("keyStore", keyStore);
}
context.setVariable("notes", notes);
List<Integer> sdkVersions = this.storeAppService.getSdkVersions(packageName);
List<String> sdkVersionsStr = (List)sdkVersions.stream().map(String::valueOf).collect(Collectors.toList());
sdkVersionsStr.add(0, "All");
context.setVariable("sdkVersions", sdkVersionsStr);
context.setVariable("selectedSdk", String.valueOf(sdkVersion));
String helpTemplate = this.fileService.getResourceToString("help/app.releaseNote.html", false);
String result = this.stringTemplateEngine.process(helpTemplate, context);
return result;
}
@RequestMapping(
value = {"apps/{packageName}/{keyStore}/releaseNote/showAllDownloadLink", "apps/{packageName}/{keyStore}/releaseNote/showAllDownloadLink/{pathSdkVersion}"},
method = {RequestMethod.GET}
)
@ResponseBody
public String appReleaseNoteShowAll(HttpServletRequest request, HttpServletResponse response, @PathVariable String packageName, @PathVariable String keyStore, @PathVariable(required = false) Integer pathSdkVersion, @RequestParam(required = false,defaultValue = "-1") int sdkVersion) throws IOException {
return this.appReleaseNote(request, response, packageName, keyStore, pathSdkVersion, sdkVersion, (Boolean)null);
}
@RequestMapping(
value = {"ota/{fileUuid}"},
method = {RequestMethod.GET}
)
public void getOta(HttpServletRequest request, HttpServletResponse response, @PathVariable UUID fileUuid, @RequestHeader(name = "range",required = false) String range, @RequestHeader(name = "if-match",required = false) String etag, @RequestParam(required = false) Long position, @RequestParam(required = false) Long checksum) {
StoreOtaFile otaFile = this.storeOtaService.findOtaFile(fileUuid);
try {
if (otaFile == null) {
response.sendError(404, "System update file not exists.");
} else {
String url = this.cloudStorageService.getUrl("store", "ota", otaFile.getProductId(), fileUuid);
if (Strings.isNullOrEmpty(url)) {
String filePath = this.storeOtaService.getStoreOtaFilePath(otaFile.getProductId(), fileUuid);
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), otaFile.getFileName(), otaFile.getLastModified(), range, otaFile.getChecksum());
} else {
response.sendRedirect(url);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(
value = {"webapp/{fileUuid}"},
method = {RequestMethod.GET}
)
public void getWebApp(HttpServletRequest request, HttpServletResponse response, @PathVariable String fileUuid, @RequestHeader(name = "range",required = false) String range, @RequestHeader(name = "if-match",required = false) String etag, @RequestParam(required = false) Long position, @RequestParam(required = false) Long checksum) throws IOException {
StoreWebApp app = null;
try {
app = this.storeWebAppService.find(UUID.fromString(fileUuid));
} catch (Exception var10) {
app = this.storeWebAppService.find((String)null, (String)null, fileUuid);
}
this.getWebApp(response, app, range);
}
@RequestMapping(
value = {"webapp/latest"},
method = {RequestMethod.GET}
)
public void getWebApp(HttpServletRequest request, HttpServletResponse response, @RequestHeader(name = "range",required = false) String range, @RequestHeader(name = "if-match",required = false) String etag, @RequestParam(required = false,defaultValue = "com.mitac") String groupId, @RequestParam(required = false,defaultValue = "midm") String artifactId, @RequestParam(required = false,defaultValue = "true") boolean isFormal) throws IOException {
StoreWebApp app = this.storeWebAppService.findLatest(groupId, artifactId, isFormal);
this.getWebApp(response, app, range);
}
private void getWebApp(HttpServletResponse response, StoreWebApp app, String range) {
try {
if (app != null) {
String url = this.cloudStorageService.getUrl("store", "webapp", app.getFileUuid());
if (Strings.isNullOrEmpty(url)) {
String filePath = this.storeWebAppService.getStoreFilePath(app.getFileUuid());
McFile file = new McFile(filePath);
if (file.exists()) {
McResource.copyFileToResponse(this.servletContext, response, file, app.getFileName(), app.getLastModified(), range, app.getChecksum());
} else {
response.sendError(404, "Webapp file not exists.");
}
} else {
response.sendRedirect(url);
}
} else {
response.sendError(404, "Webapp file not exists.");
}
} catch (IOException var7) {
}
}
@RequestMapping(
value = {"webapp/releaseNote"},
method = {RequestMethod.GET}
)
@ResponseBody
public String webAppReleaseNote(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false,defaultValue = "com.mitac") String groupId, @RequestParam(required = false,defaultValue = "midm") String artifactId, @RequestParam(required = false) String version, @RequestParam(required = false,defaultValue = "0") int limit, @RequestParam(required = false,defaultValue = "true") boolean isFormal) throws IOException {
List<StoreWebApp> webapp = this.storeWebAppService.findAll(groupId, artifactId, version, limit);
WebContext context = new WebContext(request, response, this.servletContext, request.getLocale());
context.setVariable("title", "MiDM Host Release Note");
context.setVariable("apps", webapp);
context.setVariable("isFormal", isFormal);
String helpTemplate = this.fileService.getResourceToString("help/webapp.releaseNote.html", false);
String result = this.stringTemplateEngine.process(helpTemplate, context);
return result;
}
@RequestMapping(
value = {"other/{fileName}"},
method = {RequestMethod.GET}
)
public void getOtherFile(HttpServletRequest request, HttpServletResponse response, @PathVariable String fileName, @RequestHeader(name = "range",required = false) String range, @RequestHeader(name = "if-match",required = false) String etag, @RequestParam(required = false) Long position, @RequestParam(required = false) Long checksum) {
StoreOtherFile file = this.storeOtherService.findByUuidOrNameOrAlias(fileName);
try {
if (file == null) {
response.sendError(404, "System update file not exists.");
} else {
String url = this.cloudStorageService.getUrl("store", "other", file.getFileUuid());
if (Strings.isNullOrEmpty(url)) {
String filePath = this.storeOtherService.getStoreFilePath("other", file.getFileUuid());
McResource.copyFileToResponse(this.servletContext, response, new McFile(filePath), file.getFileName(), file.getLastModified(), range, file.getChecksum());
} else {
response.sendRedirect(url);
}
}
} catch (IOException var11) {
}
}
}