From 3076da1ed808e74bb277dd537b82fdc6f68a54be Mon Sep 17 00:00:00 2001 From: Justin Richer Date: Tue, 24 Feb 2015 12:10:54 -0500 Subject: [PATCH] functioning resource set repository layer --- .../openid/connect/model/ResourceSet.java | 2 +- .../connect/service/ResourceSetService.java | 4 ++ .../db/tables/hsql_database_tables.sql | 2 +- .../webapp/WEB-INF/application-context.xml | 3 +- .../repository/ResourceSetRepository.java | 30 +++++++++++++ .../impl/JpaResourceSetRepository.java | 45 +++++++++++++++++++ .../impl/DefaultResourceSetService.java | 21 +++++++++ .../web/ResourceSetRegistrationEndpoint.java | 7 +-- 8 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 openid-connect-server/src/main/java/org/mitre/openid/connect/repository/ResourceSetRepository.java create mode 100644 openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/JpaResourceSetRepository.java diff --git a/openid-connect-common/src/main/java/org/mitre/openid/connect/model/ResourceSet.java b/openid-connect-common/src/main/java/org/mitre/openid/connect/model/ResourceSet.java index 12a0cc169..88900d10b 100644 --- a/openid-connect-common/src/main/java/org/mitre/openid/connect/model/ResourceSet.java +++ b/openid-connect-common/src/main/java/org/mitre/openid/connect/model/ResourceSet.java @@ -97,7 +97,7 @@ public class ResourceSet { * @return the type */ @Basic - @Column(name = "type") + @Column(name = "rs_type") public String getType() { return type; } diff --git a/openid-connect-common/src/main/java/org/mitre/openid/connect/service/ResourceSetService.java b/openid-connect-common/src/main/java/org/mitre/openid/connect/service/ResourceSetService.java index 1c00b7893..74ee0799b 100644 --- a/openid-connect-common/src/main/java/org/mitre/openid/connect/service/ResourceSetService.java +++ b/openid-connect-common/src/main/java/org/mitre/openid/connect/service/ResourceSetService.java @@ -16,6 +16,8 @@ *******************************************************************************/ package org.mitre.openid.connect.service; +import org.mitre.openid.connect.model.ResourceSet; + /** * * Manage registered resource sets at this authorization server. @@ -25,4 +27,6 @@ package org.mitre.openid.connect.service; */ public interface ResourceSetService { + public ResourceSet saveNew(ResourceSet rs); + } diff --git a/openid-connect-server-webapp/src/main/resources/db/tables/hsql_database_tables.sql b/openid-connect-server-webapp/src/main/resources/db/tables/hsql_database_tables.sql index 8f27c839e..84d25d25c 100644 --- a/openid-connect-server-webapp/src/main/resources/db/tables/hsql_database_tables.sql +++ b/openid-connect-server-webapp/src/main/resources/db/tables/hsql_database_tables.sql @@ -224,7 +224,7 @@ CREATE TABLE IF NOT EXISTS resource_set ( name VARCHAR(1024), uri VARCHAR(1024), icon_uri VARCHAR(1024), - policy_uri VARCHAR(1024), + rs_type VARCHAR(256), owner VARCHAR(256) ); diff --git a/openid-connect-server-webapp/src/main/webapp/WEB-INF/application-context.xml b/openid-connect-server-webapp/src/main/webapp/WEB-INF/application-context.xml index add7a9640..ee31aaaec 100644 --- a/openid-connect-server-webapp/src/main/webapp/WEB-INF/application-context.xml +++ b/openid-connect-server-webapp/src/main/webapp/WEB-INF/application-context.xml @@ -117,11 +117,10 @@ - + - diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/ResourceSetRepository.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/ResourceSetRepository.java new file mode 100644 index 000000000..08932d7e3 --- /dev/null +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/ResourceSetRepository.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright 2015 The MITRE Corporation + * and the MIT Kerberos and Internet Trust Consortium + * + * 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.mitre.openid.connect.repository; + +import org.mitre.openid.connect.model.ResourceSet; + +/** + * @author jricher + * + */ +public interface ResourceSetRepository { + + public ResourceSet save(ResourceSet rs); + +} diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/JpaResourceSetRepository.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/JpaResourceSetRepository.java new file mode 100644 index 000000000..5d9b01fdb --- /dev/null +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/JpaResourceSetRepository.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright 2015 The MITRE Corporation + * and the MIT Kerberos and Internet Trust Consortium + * + * 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.mitre.openid.connect.repository.impl; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.mitre.openid.connect.model.ResourceSet; +import org.mitre.openid.connect.repository.ResourceSetRepository; +import org.mitre.util.jpa.JpaUtil; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author jricher + * + */ +@Repository +public class JpaResourceSetRepository implements ResourceSetRepository { + + @PersistenceContext + private EntityManager em; + + @Override + @Transactional + public ResourceSet save(ResourceSet rs) { + return JpaUtil.saveOrUpdate(rs.getId(), em, rs); + } + +} diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/DefaultResourceSetService.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/DefaultResourceSetService.java index 61a6433f2..b79f233a7 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/DefaultResourceSetService.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/DefaultResourceSetService.java @@ -17,7 +17,10 @@ package org.mitre.openid.connect.service.impl; +import org.mitre.openid.connect.model.ResourceSet; +import org.mitre.openid.connect.repository.ResourceSetRepository; import org.mitre.openid.connect.service.ResourceSetService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** @@ -27,4 +30,22 @@ import org.springframework.stereotype.Service; @Service public class DefaultResourceSetService implements ResourceSetService { + @Autowired + private ResourceSetRepository repository; + + @Override + public ResourceSet saveNew(ResourceSet rs) { + + if (rs.getId() != null) { + throw new IllegalArgumentException("Can't save a new resource set with an ID already set to it."); + } + + ResourceSet saved = repository.save(rs); + + return saved; + + } + + + } diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ResourceSetRegistrationEndpoint.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ResourceSetRegistrationEndpoint.java index 2736f8c4f..b823d44fa 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ResourceSetRegistrationEndpoint.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ResourceSetRegistrationEndpoint.java @@ -40,10 +40,10 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; @Controller @RequestMapping(ResourceSetRegistrationEndpoint.URL) +@PreAuthorize("hasRole('ROLE_USER')") public class ResourceSetRegistrationEndpoint { public static final String URL = "/resource_set/resource_set"; @@ -54,7 +54,6 @@ public class ResourceSetRegistrationEndpoint { private JsonParser parser = new JsonParser(); @RequestMapping(method = RequestMethod.POST, produces = MimeTypeUtils.APPLICATION_JSON_VALUE, consumes = MimeTypeUtils.APPLICATION_JSON_VALUE) - @PreAuthorize("hasRole('ROLE_USER')") public String createResourceSet(@RequestBody String jsonString, Model m, Authentication auth) { // if auth is OAuth, make sure we've got the right scope @@ -80,8 +79,10 @@ public class ResourceSetRegistrationEndpoint { rs.setOwner(auth.getName()); + ResourceSet saved = resourceSetService.saveNew(rs); + m.addAttribute("code", HttpStatus.CREATED); - m.addAttribute("entity", rs); + m.addAttribute("entity", saved); return ResourceSetEntityView.VIEWNAME; }