Skip to content

Commit

Permalink
Issue #80 - Initial REST web services updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdrummond committed Mar 23, 2013
1 parent 0c89ff1 commit a0a5edc
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
@@ -0,0 +1,103 @@
/*
Copyright 2013 Josh Drummond
This file is part of WebPasswordSafe.
WebPasswordSafe is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
WebPasswordSafe is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WebPasswordSafe; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.webpasswordsafe.server.webservice.rest;

import javax.servlet.http.HttpServletRequest;
import net.webpasswordsafe.client.remote.LoginService;
import net.webpasswordsafe.client.remote.PasswordService;
import net.webpasswordsafe.common.model.Password;
import net.webpasswordsafe.server.ServerSessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.servlet.ModelAndView;
import org.springframework.web.servlet.View;

/**
* Set of password related REST webservices
*
* @author Josh Drummond
*
*/
@Controller
public class PasswordController {

@Autowired
private PasswordService passwordService;
@Autowired
protected LoginService loginService;
@Autowired
private View jsonView;

private static Logger LOG = Logger.getLogger(PasswordController.class);
private static final String DATA_FIELD = "data";
private static final String ERROR_FIELD = "error";

@RequestMapping(value = "/password/{passwordId}", method = RequestMethod.GET)
public ModelAndView getCurrentPassword(@PathVariable("passwordId") String passwordId, HttpServletRequest request,
@RequestHeader("X-WPS-Username") String authnUsername,
@RequestHeader("X-WPS-Password") String authnPassword)
{
boolean isSuccess = false;
String message = "";
String currentPassword = "";
try
{
ServerSessionUtil.setIP(request.getRemoteAddr());
boolean isAuthnValid = loginService.login(authnUsername, authnPassword);
if (isAuthnValid)
{
Password password = passwordService.getPassword(Long.valueOf(passwordId));
if (password != null)
{
currentPassword = passwordService.getCurrentPassword(password.getId());
isSuccess = true;
}
else
{
message = "Password not found";
}
}
else
{
message = "Invalid authentication";
}
loginService.logout();
}
catch (Exception e)
{
LOG.error(e.getMessage(), e);
isSuccess = false;
message = e.getMessage();
}
if (isSuccess)
{
return new ModelAndView(jsonView, DATA_FIELD, currentPassword);
}
else
{
return new ModelAndView(jsonView, ERROR_FIELD, message);
}
}
}
19 changes: 18 additions & 1 deletion src/main/webapp/WEB-INF/web.xml
Expand Up @@ -55,6 +55,18 @@
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>report</servlet-name>
<servlet-class>
Expand Down Expand Up @@ -83,6 +95,11 @@
<url-pattern>*.wsdl</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>report</servlet-name>
<url-pattern>/report</url-pattern>
Expand All @@ -107,7 +124,7 @@
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/webpasswordsafe-service.xml,/WEB-INF/webpasswordsafe-data.xml,/WEB-INF/webpasswordsafe-reports.xml
/WEB-INF/webpasswordsafe-service.xml,/WEB-INF/webpasswordsafe-data.xml,/WEB-INF/webpasswordsafe-reports.xml,/WEB-INF/webpasswordsafe-rest.xml
</param-value>
</context-param>

Expand Down
48 changes: 48 additions & 0 deletions war/WEB-INF/webpasswordsafe-rest.xml
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013 Josh Drummond
This file is part of WebPasswordSafe.
WebPasswordSafe is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
WebPasswordSafe is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WebPasswordSafe; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<context:component-scan base-package="net.webpasswordsafe.server.webservice.rest"/>
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="contentType" value="text/plain"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonMessageConverter"/>
</util:list>
</property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</beans>

0 comments on commit a0a5edc

Please sign in to comment.