// Path: src/main/java/org/swarmcom/jsynapse/controller/JsynapseApi.java
// public class JsynapseApi {
// 
//     public static final String CLIENT_V1_API = "/_matrix/client/api/v1";
//     public static final String CONTENT_V1_API = "/_matrix/media/v1";
// 
//     @ExceptionHandler
//     @ResponseStatus(NOT_FOUND)
//     public String handleEntityNotFound(EntityNotFoundException ex) {
//         return ex.getMessage();
//     }
// 
//     @ExceptionHandler
//     @ResponseStatus(CONFLICT)
//     public String handleEntityAlreadyExists(EntityAlreadyExistsException ex) {
//         return ex.getMessage();
//     }
// 
//     @ExceptionHandler
//     @ResponseStatus(BAD_REQUEST)
//     public String handleInvalidRequest(InvalidRequestException ex) {
//         return ex.getMessage();
//     }
// 
//     @ExceptionHandler
//     @ResponseStatus(FORBIDDEN)
//     public String handleTokenNotFound(TokenNotFoundException ex) {
//         return ex.getMessage();
//     }
// 
//     @ExceptionHandler
//     @ResponseStatus(BAD_REQUEST)
//     public String handleValidationException(ValidationException ex) {
//         return ex.getMessage();
//     }
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/domain/User.java
// public class User {
//     @Id
//     String id;
// 
//     String userId;
// 
//     @JsonProperty("password")
//     String hashedPassword;
// 
//     @JsonView(AvatarUrlSummary.class)
//     @JsonProperty("avatar_url")
//     String avatarUrl;
// 
//     @JsonView(DisplayNameSummary.class)
//     @JsonProperty("displayname")
//     String displayName;
// 
//     public User() {
//     }
// 
//     public User(String userId, String hashedPassword) {
//         this.userId = userId;
//         this.hashedPassword = hashedPassword;
//     }
// 
//     public String getUserId() {
//         return userId;
//     }
// 
//     public void setUserId(String userId) {
//         this.userId = userId;
//     }
// 
//     public String getHashedPassword() {
//         return hashedPassword;
//     }
// 
//     public void setHashedPassword(String password) {
//         this.hashedPassword = password;
//     }
// 
//     public String getAvatarUrl() {
//         return avatarUrl;
//     }
// 
//     public void setAvatarUrl(String avatarUrl) {
//         this.avatarUrl = avatarUrl;
//     }
// 
//     public String getDisplayName() {
//         return displayName;
//     }
// 
//     public void setDisplayName(String displayName) {
//         this.displayName = displayName;
//     }
// 
//     public interface DisplayNameSummary {}
// 
//     public interface AvatarUrlSummary {}
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/service/user/UserService.java
// public interface UserService {
//     public User createUser(User user);
// 
//     public User findUserById(String userId);
// 
//     public void saveDisplayName(String userId, String displayName, String accessToken);
// 
//     public void saveAvatarUrl(String userId, String avatarUrl, String accessToken);
// 
//     public User findLoggedUserById(String userId, String accessToken);
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/controller/JsynapseApi.java
// public static final String CLIENT_V1_API = "/_matrix/client/api/v1";


import static org.swarmcom.jsynapse.controller.JsynapseApi.CLIENT_V1_API;
import com.fasterxml.jackson.annotation.JsonView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.swarmcom.jsynapse.controller.JsynapseApi;
import org.swarmcom.jsynapse.domain.User;
import org.swarmcom.jsynapse.service.user.UserService;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import static java.lang.String.format;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
/*
 * (C) Copyright 2015 eZuce Inc.
 *
 * 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 org.swarmcom.jsynapse.controller.client.api.v1;




@RestController
@RequestMapping(value = CLIENT_V1_API)
public class UserRestApi extends JsynapseApi {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserRestApi.class);
    private static final String ACCESS_TOKEN = "access_token";
    private final UserService userService;

    @Inject
    public UserRestApi(final UserService userService) {
        this.userService = userService;
    }

    @JsonView(User.DisplayNameSummary.class)
    @RequestMapping(value = "/profile/{userId}/displayname", method = GET)
    public @ResponseBody User getDisplayName(@PathVariable String userId, HttpServletRequest request) {
        String accessToken = request.getParameter(ACCESS_TOKEN);
        return userService.findLoggedUserById(userId, accessToken);
    }

    @RequestMapping(value = "/profile/{userId}/displayname", method = PUT)
    public void setDisplayName(@PathVariable String userId, @RequestBody final User user, HttpServletRequest request) {
        LOGGER.debug(format("Save display name %s for user id %s", user.getDisplayName(), userId));
        String accessToken = request.getParameter(ACCESS_TOKEN);
        userService.saveDisplayName(userId, user.getDisplayName(), accessToken);
    }

    @JsonView(User.AvatarUrlSummary.class)
    @RequestMapping(value = "/profile/{userId}/avatar_url", method = GET)
// Path: src/main/java/org/swarmcom/jsynapse/controller/JsynapseApi.java
// public class JsynapseApi {
// 
//     public static final String CLIENT_V1_API = "/_matrix/client/api/v1";
//     public static final String CONTENT_V1_API = "/_matrix/media/v1";
// 
//     @ExceptionHandler
//     @ResponseStatus(NOT_FOUND)
//     public String handleEntityNotFound(EntityNotFoundException ex) {
//         return ex.getMessage();
//     }
// 
//     @ExceptionHandler
//     @ResponseStatus(CONFLICT)
//     public String handleEntityAlreadyExists(EntityAlreadyExistsException ex) {
//         return ex.getMessage();
//     }
// 
//     @ExceptionHandler
//     @ResponseStatus(BAD_REQUEST)
//     public String handleInvalidRequest(InvalidRequestException ex) {
//         return ex.getMessage();
//     }
// 
//     @ExceptionHandler
//     @ResponseStatus(FORBIDDEN)
//     public String handleTokenNotFound(TokenNotFoundException ex) {
//         return ex.getMessage();
//     }
// 
//     @ExceptionHandler
//     @ResponseStatus(BAD_REQUEST)
//     public String handleValidationException(ValidationException ex) {
//         return ex.getMessage();
//     }
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/domain/User.java
// public class User {
//     @Id
//     String id;
// 
//     String userId;
// 
//     @JsonProperty("password")
//     String hashedPassword;
// 
//     @JsonView(AvatarUrlSummary.class)
//     @JsonProperty("avatar_url")
//     String avatarUrl;
// 
//     @JsonView(DisplayNameSummary.class)
//     @JsonProperty("displayname")
//     String displayName;
// 
//     public User() {
//     }
// 
//     public User(String userId, String hashedPassword) {
//         this.userId = userId;
//         this.hashedPassword = hashedPassword;
//     }
// 
//     public String getUserId() {
//         return userId;
//     }
// 
//     public void setUserId(String userId) {
//         this.userId = userId;
//     }
// 
//     public String getHashedPassword() {
//         return hashedPassword;
//     }
// 
//     public void setHashedPassword(String password) {
//         this.hashedPassword = password;
//     }
// 
//     public String getAvatarUrl() {
//         return avatarUrl;
//     }
// 
//     public void setAvatarUrl(String avatarUrl) {
//         this.avatarUrl = avatarUrl;
//     }
// 
//     public String getDisplayName() {
//         return displayName;
//     }
// 
//     public void setDisplayName(String displayName) {
//         this.displayName = displayName;
//     }
// 
//     public interface DisplayNameSummary {}
// 
//     public interface AvatarUrlSummary {}
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/service/user/UserService.java
// public interface UserService {
//     public User createUser(User user);
// 
//     public User findUserById(String userId);
// 
//     public void saveDisplayName(String userId, String displayName, String accessToken);
// 
//     public void saveAvatarUrl(String userId, String avatarUrl, String accessToken);
// 
//     public User findLoggedUserById(String userId, String accessToken);
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/controller/JsynapseApi.java
// public static final String CLIENT_V1_API = "/_matrix/client/api/v1";


// Path: src/main/java/org/swarmcom/jsynapse/controller/client/api/v1/UserRestApi.java
import static org.swarmcom.jsynapse.controller.JsynapseApi.CLIENT_V1_API;
import com.fasterxml.jackson.annotation.JsonView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.swarmcom.jsynapse.controller.JsynapseApi;
import org.swarmcom.jsynapse.domain.User;
import org.swarmcom.jsynapse.service.user.UserService;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import static java.lang.String.format;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;

/*
 * (C) Copyright 2015 eZuce Inc.
 *
 * 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 org.swarmcom.jsynapse.controller.client.api.v1;




@RestController
@RequestMapping(value = CLIENT_V1_API)
public class UserRestApi extends JsynapseApi {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserRestApi.class);
    private static final String ACCESS_TOKEN = "access_token";
    private final UserService userService;

    @Inject
    public UserRestApi(final UserService userService) {
        this.userService = userService;
    }

    @JsonView(User.DisplayNameSummary.class)
    @RequestMapping(value = "/profile/{userId}/displayname", method = GET)
    public @ResponseBody User getDisplayName(@PathVariable String userId, HttpServletRequest request) {
        String accessToken = request.getParameter(ACCESS_TOKEN);
        return userService.findLoggedUserById(userId, accessToken);
    }

    @RequestMapping(value = "/profile/{userId}/displayname", method = PUT)
    public void setDisplayName(@PathVariable String userId, @RequestBody final User user, HttpServletRequest request) {
        LOGGER.debug(format("Save display name %s for user id %s", user.getDisplayName(), userId));
        String accessToken = request.getParameter(ACCESS_TOKEN);
        userService.saveDisplayName(userId, user.getDisplayName(), accessToken);
    }

    @JsonView(User.AvatarUrlSummary.class)
    @RequestMapping(value = "/profile/{userId}/avatar_url", method = GET)
    public @ResponseBody User getAvatarUrl(@PathVariable String userId, HttpServletRequest request) {