| |
---|
| | import java.util.List; |
---|
| | import javax.swing.*; |
---|
| | import javax.ws.rs.*; |
---|
| | import javax.ws.rs.core.MediaType; |
---|
| | import javax.ws.rs.core.Response; |
---|
| | |
---|
| | @Component |
---|
| | @Path("/accounts") |
---|
| | public class AccountsRest { |
---|
| | private Accounts accounts = Accounts.getInstance(); |
---|
| | |
---|
| | AccountsRest() { |
---|
| | |
---|
| | /** |
---|
| | * テスト用 |
---|
| | * */ |
---|
| | { |
---|
| |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @GET |
---|
| | public String getAccounts() { |
---|
| | ObjectMapper mapper = new ObjectMapper(); |
---|
| | @Produces(MediaType.APPLICATION_JSON) |
---|
| | public ArrayList<Account> getAccounts() { |
---|
| | |
---|
| | String json = ""; |
---|
| | try { |
---|
| | |
---|
| | @JsonInclude(JsonInclude.Include.NON_NULL) |
---|
| | @JsonPropertyOrder({ |
---|
| | "accounts" |
---|
| | }) |
---|
| | class Message { |
---|
| | |
---|
| | @JsonProperty("accounts") |
---|
| | private List<String> accounts = null; |
---|
| | @JsonIgnore |
---|
| | private Map<String, Object> additionalProperties = new HashMap<String, Object>(); |
---|
| | |
---|
| | @JsonProperty("accounts") |
---|
| | public List<String> getAccounts() { |
---|
| | return accounts; |
---|
| | } |
---|
| | |
---|
| | @JsonProperty("accounts") |
---|
| | public void setAccounts(List<String> accounts) { |
---|
| | this.accounts = accounts; |
---|
| | } |
---|
| | |
---|
| | @JsonAnyGetter |
---|
| | public Map<String, Object> getAdditionalProperties() { |
---|
| | return this.additionalProperties; |
---|
| | } |
---|
| | |
---|
| | @JsonAnySetter |
---|
| | public void setAdditionalProperty(String name, Object value) { |
---|
| | this.additionalProperties.put(name, value); |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | Message message = new Message(); |
---|
| | List<String> list = new ArrayList<String>(); |
---|
| | for (Account a : accounts.getList()) list.add(a.getUid()); |
---|
| | message.setAccounts(list); |
---|
| | json = mapper.writeValueAsString(message); |
---|
| | } catch (IOException e) { |
---|
| | e.printStackTrace(); |
---|
| | System.out.println("failed"); |
---|
| | } |
---|
| | System.out.println(json); |
---|
| | return json; |
---|
| | return accounts.getList(); |
---|
| | } |
---|
| | |
---|
| | @POST |
---|
| | @Produces(MediaType.APPLICATION_JSON) |
---|
| | public AbstractJson createAccount(@FormParam("name") String name, @FormParam("password") String password) { |
---|
| | public HashMap<String,String> createAccount(@FormParam("name") String name, @FormParam("password") String password) { |
---|
| | if (accounts.isRegisteredName(name)) { |
---|
| | return new AbstractJson() { |
---|
| | String message = "conflict"; |
---|
| | throw new WebApplicationException(400); |
---|
| | |
---|
| | public String getMessage() { |
---|
| | return message; |
---|
| | } |
---|
| | }; |
---|
| | } else { |
---|
| | |
---|
| | Account newAccount = accounts.createAccount(name, password); |
---|
| | return new AbstractJson() { |
---|
| | String message = "success"; |
---|
| | String uid = newAccount.getUid(); |
---|
| | |
---|
| | public String getMessage() { |
---|
| | return message; |
---|
| | } |
---|
| | |
---|
| | public String getUid() { |
---|
| | return uid; |
---|
| | } |
---|
| | }; |
---|
| | HashMap<String,String> response=new HashMap<>(); |
---|
| | response.put("message","success"); |
---|
| | response.put("uid",newAccount.getUid()); |
---|
| | return response; |
---|
| | // |
---|
| | // return new AbstractJson() { |
---|
| | // String message = "success"; |
---|
| | // String uid = newAccount.getUid(); |
---|
| | // |
---|
| | // public String getMessage() { |
---|
| | // return message; |
---|
| | // } |
---|
| | // |
---|
| | // public String getUid() { |
---|
| | // return uid; |
---|
| | // } |
---|
| | // }; |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |
---|
| | @Path("/{uid}/login") |
---|
| | @PUT |
---|
| | @Produces(MediaType.APPLICATION_JSON) |
---|
| | public AbstractJson login(@PathParam("uid") String uid, @FormParam("password") String password) { |
---|
| | public LoginJson login(@PathParam("uid") String uid, @FormParam("password") String password) { |
---|
| | Account account = accounts.getAccount(uid); |
---|
| | |
---|
| | if (password.equals(account.getPassword())) { |
---|
| | |
---|
| | account.setToken(UUID.randomUUID().toString()); |
---|
| | return new AbstractJson() { |
---|
| | String message = "success"; |
---|
| | String token = account.getToken(); |
---|
| | |
---|
| | public String getMessage() { |
---|
| | return message; |
---|
| | } |
---|
| | |
---|
| | public String getToken() { |
---|
| | return token; |
---|
| | } |
---|
| | }; |
---|
| | LoginJson loginJson=new LoginJson("success"); |
---|
| | return loginJson; |
---|
| | |
---|
| | } else { |
---|
| | return new MessageJson("failed"); |
---|
| | throw new WebApplicationException(400); |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |
---|
| | @Path("/{uid}") |
---|
| | @GET |
---|
| | @Produces(MediaType.APPLICATION_JSON) |
---|
| | public AbstractJson getAccount(@PathParam("uid") String uid) { |
---|
| | public Account getAccount(@PathParam("uid") String uid) { |
---|
| | class ResponseJson extends AbstractJson { |
---|
| | Account account = accounts.getAccount(uid); |
---|
| | |
---|
| | public Account getAccount() { |
---|
| | return account; |
---|
| | } |
---|
| | } |
---|
| | AbstractJson response = new ResponseJson(); |
---|
| | throw new WebApplicationException(400); |
---|
| | |
---|
| | |
---|
| | return response; |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |