Added: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/core/src/main/scala/org/apache/clerezza/platform/users/WebIdGraphsService.scala URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/core/src/main/scala/org/apache/clerezza/platform/users/WebIdGraphsService.scala?rev=959675&view=auto ============================================================================== --- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/core/src/main/scala/org/apache/clerezza/platform/users/WebIdGraphsService.scala (added) +++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/core/src/main/scala/org/apache/clerezza/platform/users/WebIdGraphsService.scala Thu Jul 1 14:11:51 2010 @@ -0,0 +1,225 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.clerezza.platform.users + +import java.net.HttpURLConnection +import java.net.URL +import java.security.AccessController +import java.security.PrivilegedAction +import org.apache.clerezza.platform.config.PlatformConfig +import org.apache.clerezza.platform.config.SystemConfig +import org.apache.clerezza.rdf.core.MGraph +import org.apache.clerezza.rdf.core.UriRef +import org.apache.clerezza.rdf.core.access.NoSuchEntityException +import org.apache.clerezza.rdf.core.access.SecuredMGraph +import org.apache.clerezza.rdf.core.access.TcManager +import org.apache.clerezza.rdf.core.impl.SimpleMGraph +import org.apache.clerezza.rdf.core.serializedform.Parser +import org.apache.clerezza.rdf.core.serializedform.SupportedFormat +import org.apache.clerezza.rdf.ontologies.PLATFORM +import org.apache.clerezza.rdf.utils.UnionMGraph + + +/** + * For agents with a Web-Id various graphs are available, this graphs are + * grouped by <code>WebIdGraphs</code> which this service provides. + */ +class WebIdGraphsService() { + + private var parser: Parser = null + + protected def bindParser(p: Parser) = { + parser = p + } + + protected def unbindParser(p: Parser) = { + parser = null + } + + private var tcManager: TcManager = null; + + protected def bindTcManager(tcManager: TcManager) = { + this.tcManager = tcManager + } + + protected def unbindTcManager(tcManager: TcManager) = { + this.tcManager = null + } + + private var platformConfig: PlatformConfig = null; + + protected def bindPlatformConfig(c: PlatformConfig) = { + this.platformConfig = c + } + + protected def unbindPlatformConfig(c: PlatformConfig) = { + this.platformConfig = null + } + + private lazy val acceptHeader = { + import scala.collection.JavaConversions._ + (for (f <- parser.getSupportedFormats) yield { + val qualityOfFormat = { + f match { + //the default format + case SupportedFormat.RDF_XML => "1.0"; + //n3 is a bit less well defined and/or many parsers supports only subsets + case SupportedFormat.N3 => "0.5"; + case _ => "0.8"; + } + } + f+"; q="+qualityOfFormat+"," + }).mkString +" *; q=.1" + } + + def getWebIdGraphs(webId: UriRef): WebIdGraphs = { + new WebIdGraphs(webId) + } + + class WebIdGraphs(webId: UriRef) { + val uriString = webId.getUnicodeString + + lazy val isLocal: Boolean = { + import scala.collection.JavaConversions._ + platformConfig.getBaseUris.exists(baseUri => uriString.startsWith(baseUri.getUnicodeString)) + } + + /** + * remote graphs are cache locally with this name + */ + lazy val localCacheUri = { + new UriRef(representationGraphUriString+".cache") + } + + lazy val localCache = try { + val g = tcManager.getMGraph(localCacheUri) + g + } catch { + case e: NoSuchEntityException => tcManager.createMGraph(localCacheUri) + } + + lazy val representationGraphUriString = { + val hashPos = uriString.indexOf('#') + if (hashPos != -1) { + uriString.substring(0, hashPos) + } else { + finalRedirectLocation + } + } + + /** + * for web-ids with a # same as representationGraphUriString + */ + //FIXME multiple remote users could have same + lazy val localGraphUri = { + new UriRef(localGraphUriString) + } + + lazy val localGraphUriString = { + val hashPos = uriString.indexOf('#') + if (hashPos != -1) { + uriString.substring(0, hashPos) + } else { + uriString + } + } + + lazy val localGraph = try { + val g = tcManager.getMGraph(localGraphUri) + g + } catch { + case e: NoSuchEntityException => tcManager.createMGraph(localGraphUri) + } + + lazy val representationGraphUri = { + new UriRef(representationGraphUriString) + } + private lazy val finalRedirectLocation = { + finalRedirectLocationFor(webId.getUnicodeString) + } + def finalRedirectLocationFor(us: String): String = { + val url = new URL(us) + val connection = url.openConnection() + connection match { + case hc : HttpURLConnection => { + hc.setRequestMethod("HEAD"); + hc.setInstanceFollowRedirects(false) + hc.addRequestProperty("Accept:", acceptHeader) + hc.getResponseCode match { + case HttpURLConnection.HTTP_SEE_OTHER => { + val location = hc.getHeaderField("Location") + if (location == null) { + throw new RuntimeException("No Location Headers in 303 response") + } + finalRedirectLocationFor(location) + } + case _ => us + } + } + case _ => us + } + } + + def updateLocalCache() = { + val url = new URL(representationGraphUriString) + val connection = url.openConnection() + connection match { + case hc: HttpURLConnection => hc.addRequestProperty("Accept:", acceptHeader); + } + val mediaType = connection.getContentType() + connection.connect() + val in = connection.getInputStream() + val remoteTriples = parser.parse(in, mediaType, representationGraphUri) + localCache.clear() + localCache.addAll(remoteTriples) + } + + /** + * Returns the graph with triples to which public read access can be granted. + * + * This will return a union of the following graphs. + * - minimum graph constructed for system graph + * - cached version of profile document from web, if available + * - as read/write graph: the public personal profile graph + * + * @return a GraphNode describing webId + */ + def publicUserGraph: MGraph = { + def systemTriples = { + val systemGraph = tcManager.getMGraph(SystemConfig.SYSTEM_GRAPH_URI) + val triples = systemGraph.filter(webId, PLATFORM.userName, null) + val result = new SimpleMGraph + while (triples.hasNext) { + result.add(triples.next()) + } + result + } + AccessController.doPrivileged(new PrivilegedAction[MGraph]() { + def run() = { + val unionGraph = if (isLocal) { + new UnionMGraph(localGraph, systemTriples) + } else { + new UnionMGraph(localGraph, localCache, systemTriples) + } + new SecuredMGraph(unionGraph, localGraphUri) + } + }) + } + } +} \ No newline at end of file
Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/ontologies/pom.xml URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/ontologies/pom.xml?rev=959675&r1=956251&r2=959675&view=diff ============================================================================== --- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/ontologies/pom.xml (original) +++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/ontologies/pom.xml Thu Jul 1 14:11:51 2010 @@ -2,16 +2,16 @@ <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.apache.clerezza</groupId> - <artifactId>org.apache.clerezza.platform.security.foafssl</artifactId> + <artifactId>org.apache.clerezza.platform.users</artifactId> <version>0.1-incubating-SNAPSHOT</version> </parent> <groupId>org.apache.clerezza</groupId> - <artifactId>org.apache.clerezza.platform.security.foafssl.ontologies</artifactId> + <artifactId>org.apache.clerezza.platform.users.ontologies</artifactId> <version>0.1-incubating-SNAPSHOT</version> <packaging>bundle</packaging> - <name>CClerezza - Platform Security foaf+ssl Ontologies</name> + <name>Clerezza - Platform Users Ontologies</name> <description> - Fafo + Ssl Ontologies + User Graphs Ontologies </description> <dependencies> <dependency> Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/pom.xml URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/pom.xml?rev=959675&r1=956251&r2=959675&view=diff ============================================================================== --- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/pom.xml (original) +++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.users/pom.xml Thu Jul 1 14:11:51 2010 @@ -6,12 +6,14 @@ <version>0.2-incubating-SNAPSHOT</version> </parent> <groupId>org.apache.clerezza</groupId> - <artifactId>org.apache.clerezza.platform.security.foafssl</artifactId> + <artifactId>org.apache.clerezza.platform.users</artifactId> <packaging>pom</packaging> <version>0.1-incubating-SNAPSHOT</version> - <name>Clerezza - Platform Security foaf+ssl</name> + <name>Clerezza - Platform Users</name> + <description>Utilities to access graphs with information about and for + specific agents/users</description> <modules> - <module>ontologies</module> + <!-- <module>ontologies</module> --> <module>core</module> </modules> </project> Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/SecuredMGraph.java URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/SecuredMGraph.java?rev=959675&r1=959674&r2=959675&view=diff ============================================================================== --- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/SecuredMGraph.java (original) +++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/SecuredMGraph.java Thu Jul 1 14:11:51 2010 @@ -24,6 +24,9 @@ import org.apache.clerezza.rdf.core.UriR import org.apache.clerezza.rdf.core.impl.SimpleGraph; /** + * A SecuredMGraph is a LockableMGraph that wraps a LockableMGraph checking each + * access for the rights on a the graph for which the uri is passed to the + * constructor. * * @author mir */ @@ -31,7 +34,7 @@ public class SecuredMGraph extends Secur private LockableMGraph wrapped; - SecuredMGraph(LockableMGraph wrapped, UriRef name) { + public SecuredMGraph(LockableMGraph wrapped, UriRef name) { super(wrapped, name); this.wrapped = wrapped; } Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/SecuredTripleCollection.java URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/SecuredTripleCollection.java?rev=959675&r1=959674&r2=959675&view=diff ============================================================================== --- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/SecuredTripleCollection.java (original) +++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/SecuredTripleCollection.java Thu Jul 1 14:11:51 2010 @@ -30,6 +30,9 @@ import org.apache.clerezza.rdf.core.even import org.apache.clerezza.rdf.core.event.GraphListener; /** + * A Secured triple collection wraps a triple collection checking each access + * for the rights on a the graph for which the uri is passed to the + * constructor. * * @author mir */ @@ -38,7 +41,7 @@ public class SecuredTripleCollection imp private TripleCollection wrapped; private String name; - SecuredTripleCollection(TripleCollection wrapped, UriRef name) { + public SecuredTripleCollection(TripleCollection wrapped, UriRef name) { this.wrapped = wrapped; this.name = name.getUnicodeString(); } Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java?rev=959675&r1=959674&r2=959675&view=diff ============================================================================== --- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java (original) +++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleLiteralFactory.java Thu Jul 1 14:11:51 2010 @@ -60,6 +60,8 @@ public class SimpleLiteralFactory extend final private static UriRef xsdDouble = new UriRef("http://www.w3.org/2001/XMLSchema#double"); + final private static UriRef xsdAnyURI = + new UriRef("http://www.w3.org/2001/XMLSchema#anyURI"); final static Class<? extends byte[]> byteArrayType; static { @@ -208,6 +210,24 @@ public class SimpleLiteralFactory extend return new Double(literal.getLexicalForm()); } } + + private static class UriRefConverter implements TypeConverter<UriRef> { + + + + @Override + public TypedLiteral createTypedLiteral(UriRef value) { + return new TypedLiteralImpl(value.getUnicodeString(), xsdAnyURI); + } + + @Override + public UriRef createObject(TypedLiteral literal) { + if (!literal.getDataType().equals(xsdAnyURI)) { + throw new InvalidLiteralTypeException(UriRef.class, literal.getDataType()); + } + return new UriRef(literal.getLexicalForm()); + } + } private static Map<Class<?>, TypeConverter<?>> typeConverterMap = new HashMap<Class<?>, TypeConverter<?>>(); @@ -219,6 +239,7 @@ public class SimpleLiteralFactory extend typeConverterMap.put(Integer.class, new IntegerConverter()); typeConverterMap.put(Long.class, new LongConverter()); typeConverterMap.put(Double.class, new DoubleConverter()); + typeConverterMap.put(UriRef.class, new UriRefConverter()); } Propchange: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.ontologies/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Thu Jul 1 14:11:51 2010 @@ -0,0 +1 @@ +target Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/pom.xml URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/pom.xml?rev=959675&r1=959674&r2=959675&view=diff ============================================================================== --- incubator/clerezza/trunk/org.apache.clerezza.parent/pom.xml (original) +++ incubator/clerezza/trunk/org.apache.clerezza.parent/pom.xml Thu Jul 1 14:11:51 2010 @@ -71,6 +71,7 @@ <module>org.apache.clerezza.platform.typerendering.seedsnipe</module> <module>org.apache.clerezza.platform.usermanager</module> <module>org.apache.clerezza.platform.usermanager.webinterface</module> + <module>org.apache.clerezza.platform.users</module> <module>org.apache.clerezza.platform.xhtml2html</module> <module>org.apache.clerezza.rdf.core</module> <module>org.apache.clerezza.rdf.core.test</module> @@ -785,6 +786,11 @@ </dependency> <dependency> <groupId>org.apache.clerezza</groupId> + <artifactId>org.apache.clerezza.platform.users.core</artifactId> + <version>0.1-incubating-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.clerezza</groupId> <artifactId>org.apache.clerezza.platform.security.auth.cookie</artifactId> <version>0.4-incubating-SNAPSHOT</version> </dependency>
