This commit is contained in:
Tienan Chen 2013-09-06 11:37:31 +08:00
parent f7276b4777
commit 05d26a3810
1 changed files with 88 additions and 85 deletions

View File

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