refactor
This commit is contained in:
parent
f7276b4777
commit
05d26a3810
|
@ -1,85 +1,88 @@
|
|||
package org.bench4q.web.controller;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
import org.bench4q.master.api.model.AuthorizeResponseModel;
|
||||
import org.bench4q.master.communication.HttpRequester;
|
||||
import org.bench4q.master.communication.HttpRequester.HttpResponse;
|
||||
import org.bench4q.web.model.UserModel;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
|
||||
@Controller
|
||||
@SessionAttributes({"rtmsg","accessToken","username"})
|
||||
public class AuthorizeActionController extends BaseActionController {
|
||||
|
||||
private AuthorizeResponseModel extractAuthorizeResponseModel(String content)
|
||||
throws JAXBException {
|
||||
AuthorizeResponseModel resultModel = new AuthorizeResponseModel();
|
||||
Unmarshaller ummarshaller = JAXBContext.newInstance(
|
||||
resultModel.getClass()).createUnmarshaller();
|
||||
resultModel = (AuthorizeResponseModel) ummarshaller
|
||||
.unmarshal(new ByteArrayInputStream(content.getBytes()));
|
||||
return resultModel;
|
||||
|
||||
}
|
||||
@RequestMapping("login.do")
|
||||
public String login(UserModel user, ModelMap model) throws IOException, JAXBException {
|
||||
System.out.println("enter authorize");
|
||||
String urlString = masterIP + "user/authorize";
|
||||
String username = user.getUsername();
|
||||
String password = user.getPassword();
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("userName", username);
|
||||
params.put("password", password);
|
||||
HttpRequester httpRequester = new HttpRequester();
|
||||
HttpResponse httpResponse = httpRequester.sendGet(urlString,
|
||||
params, null);
|
||||
AuthorizeResponseModel authorizeResponseModel = extractAuthorizeResponseModel(httpResponse
|
||||
.getContent());
|
||||
if (authorizeResponseModel.isSuccess()) {
|
||||
System.out.println("success");
|
||||
model.addAttribute("accessToken", authorizeResponseModel.getAccessToken());
|
||||
model.addAttribute("username", username);
|
||||
return "homepage";
|
||||
} else {
|
||||
model.addAttribute("rtmsg", "somthing is wrong with the password, please try again.");
|
||||
|
||||
return "userlogin";
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("adminlogin.do")
|
||||
public String adminlogin (UserModel user, ModelMap model) throws IOException, JAXBException{
|
||||
System.out.println("enter admin authorize");
|
||||
String urlString = masterIP + "user/authorize";
|
||||
String username = user.getUsername();
|
||||
String password = user.getPassword();
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("userName", username);
|
||||
params.put("password", password);
|
||||
HttpRequester httpRequester = new HttpRequester();
|
||||
HttpResponse httpResponse = httpRequester.sendGet(urlString,
|
||||
params, null);
|
||||
AuthorizeResponseModel authorizeResponseModel = extractAuthorizeResponseModel(httpResponse
|
||||
.getContent());
|
||||
if (authorizeResponseModel.isSuccess()) {
|
||||
System.out.println("success");
|
||||
model.addAttribute("accessToken", authorizeResponseModel.getAccessToken());
|
||||
return "admin";
|
||||
} else {
|
||||
model.addAttribute("rtmsg", "somthing is wrong with the password, please try again.");
|
||||
return "adminlogin";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package org.bench4q.web.controller;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
import org.bench4q.master.api.model.AuthorizeResponseModel;
|
||||
import org.bench4q.master.communication.HttpRequester;
|
||||
import org.bench4q.master.communication.HttpRequester.HttpResponse;
|
||||
import org.bench4q.web.model.UserModel;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
|
||||
@Controller
|
||||
@SessionAttributes({ "rtmsg", "accessToken", "username" })
|
||||
public class AuthorizeActionController extends BaseActionController {
|
||||
|
||||
private AuthorizeResponseModel extractAuthorizeResponseModel(String content)
|
||||
throws JAXBException {
|
||||
AuthorizeResponseModel resultModel = new AuthorizeResponseModel();
|
||||
Unmarshaller ummarshaller = JAXBContext.newInstance(
|
||||
resultModel.getClass()).createUnmarshaller();
|
||||
resultModel = (AuthorizeResponseModel) ummarshaller
|
||||
.unmarshal(new ByteArrayInputStream(content.getBytes()));
|
||||
return resultModel;
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("login.do")
|
||||
public String login(UserModel user, ModelMap model) throws IOException,
|
||||
JAXBException {
|
||||
System.out.println("enter authorize");
|
||||
|
||||
AuthorizeResponseModel authorizeResponseModel = authorize(user);
|
||||
if (authorizeResponseModel.isSuccess()) {
|
||||
System.out.println("success");
|
||||
model.addAttribute("accessToken",
|
||||
authorizeResponseModel.getAccessToken());
|
||||
model.addAttribute("username", user.getUsername());
|
||||
return "homepage";
|
||||
} else {
|
||||
model.addAttribute("rtmsg",
|
||||
"somthing is wrong with the password, please try again.");
|
||||
|
||||
return "userlogin";
|
||||
}
|
||||
}
|
||||
|
||||
private AuthorizeResponseModel authorize(UserModel user)
|
||||
throws IOException, JAXBException {
|
||||
String urlString = masterIP + "user/authorize";
|
||||
String username = user.getUsername();
|
||||
String password = user.getPassword();
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("userName", username);
|
||||
params.put("password", password);
|
||||
HttpRequester httpRequester = new HttpRequester();
|
||||
HttpResponse httpResponse = httpRequester.sendGet(urlString, params,
|
||||
null);
|
||||
return extractAuthorizeResponseModel(httpResponse.getContent());
|
||||
}
|
||||
|
||||
@RequestMapping("adminlogin.do")
|
||||
public String adminlogin(UserModel user, ModelMap model)
|
||||
throws IOException, JAXBException {
|
||||
System.out.println("enter admin authorize");
|
||||
|
||||
AuthorizeResponseModel authorizeResponseModel = authorize(user);
|
||||
if (authorizeResponseModel.isSuccess()) {
|
||||
System.out.println("success");
|
||||
model.addAttribute("accessToken",
|
||||
authorizeResponseModel.getAccessToken());
|
||||
return "admin";
|
||||
} else {
|
||||
model.addAttribute("rtmsg",
|
||||
"somthing is wrong with the password, please try again.");
|
||||
return "adminlogin";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue