Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.marmotta.platform.core.services.content;
 
 import java.util.Map;
 import java.util.Set;
 
 
Service that provides access to the content associated with a resource. It makes use of the ContentReader and ContentWriter implementations registered in the system. User: Thomas Kurz, Sebastian Schaffert Date: 07.02.11 Time: 12:37
 
 public class ContentServiceImpl implements ContentService {
 
     @Inject
     private Logger log;
 
     @Inject
 
     @Inject
     private Instance<ContentReaderreaders;
 
     @Inject
     private Instance<ContentWriterwriters;
 
     private Map<Pattern,ContentReaderreaderMap;
     private Map<Pattern,ContentWriterwriterMap;
 
     @Override
     public void initialise() {
         .info("Content Service starting up ...");
         initialiseReadersWriters();
     }
     
     protected void initialize(@Observes SesameStartupEvent event) {
     	initialise();
     }
 
     private void initialiseReadersWriters() {
          = new HashMap<PatternContentReader>();
          = new HashMap<PatternContentWriter>();
 
         // first read from the config file all content.* keys and store the name of the reader/writer in this set
         // will e.g. store "triplestore" and "filesystem" if it finds keys of the form "content.triplestore.reader"
         // and "content.filesystem.writer"
         Set<StringconfigNames = new HashSet<String>();
         for(String key : .listConfigurationKeys("content")) {
             String[] components = key.split("\\.");
             if(components.length > 1) {
                 configNames.add(components[1]);
             }
         }
 
         // then read the configuration for each reader/writer specification in the config file and look whether
         // the appropriate reader and/or writer is available
         for(String configName : configNames) {
             String readerClass = .getStringConfiguration("content."+configName+".reader");
             String writerClass = .getStringConfiguration("content."+configName+".writer");
            String patternStr  = .getStringConfiguration("content."+configName+".pattern");
            String enabledStr  = .getStringConfiguration("content."+configName+".enabled");
            if(Boolean.parseBoolean(enabledStr)) {
                ContentReader reader = null;
                ContentWriter writer = null;
                Pattern pattern = null;
                if(readerClass != null) {
                    for(ContentReader r : ) {
                        if(r.getClass().getCanonicalName().startsWith(readerClass)) {
                            reader = r;
                            break;
                        }
                    }
                }
                if(writerClass != null) {
                    for(ContentWriter w : ) {
                        if(w.getClass().getCanonicalName().startsWith(writerClass)) {
                            writer = w;
                            break;
                        }
                    }
                }
                try {
                    pattern = Pattern.compile(patternStr);
                } catch(Exception ex) {
                    .warn("pattern {} is not a valid regular expression; disabling reader/writer {} (message was {})"patternStr,configName,ex.getMessage());
                    continue;
                }
                if(pattern != null && reader != null) {
                    .put(pattern,reader);
                    .info("enabled content reader '{}' for pattern {}",reader.getName(), pattern);
                }
                if(pattern != null && writer != null) {
                    .put(pattern,writer);
                    .info("enabled content writer '{}' for pattern {}"writer.getName(), pattern);
                }
            } else {
                .info("content reader/writer {} disabled",configName);
            }
        }
    }
        for (String key : event.getKeys())
            if (key.startsWith("content.")) {
                .info("Content Service reinitialising ...");
                initialiseReadersWriters();
                break;
            }
    }
    @Override
    public void setContentData(Resource resourcebyte[] dataString mimetypethrows WritingNotSupportedException {
        // iterate over all possible writers; if the pattern matches, try to store the content and return
        for(Pattern p : .keySet()) {
            if(p.matcher(resource.toString()).matches()) {
                ContentWriter writer = .get(p);
                .debug("setting content for resource {} using writer {}",resource,writer.getName());
                try {
                    writer.setContentData(resourcedatamimetype);
                    return;
                } catch(IOException ex) {
                    .error("could not write content, writer threw an IO Exception",ex);
                    throw new WritingNotSupportedException(ex.getMessage(),ex);
                }
            }
        }
        throw new WritingNotSupportedException("no writer found for resource "+resource);
    }


    
Store the content of the specified mime type for the specified resource. Accepts an input stream containing the byte data of the content that is read and written to the destination configured for this writer.

This method is preferrable for resources with large amounts of data.

Parameters:
resource the resource for which to return the content
mimeType the mime type to retrieve of the content
in a InputStream containing the content of the resource
    @Override
    public void setContentStream(Resource resourceInputStream inString mimeTypethrows WritingNotSupportedException {
        // iterate over all possible writers; if the pattern matches, try to store the content and return
        for(Pattern p : .keySet()) {
            if(p.matcher(resource.toString()).matches()) {
                ContentWriter writer = .get(p);
                .debug("setting content for resource {} using writer {}",resource,writer.getName());
                try {
                    writer.setContentStream(resource,in,mimeType);
                    return;
                } catch(IOException ex) {
                    .error("could not write content, writer threw an IO Exception",ex);
                    throw new WritingNotSupportedException(ex.getMessage(),ex);
                }
            }
        }
        throw new WritingNotSupportedException("no writer found for resource "+resource);
    }

    
Retrieve the content of the specified mime type for the specified resource. Returns a byte array containing the byte data of the content, or null, indicating that a content of the specified mime type does not exist for the resource.

Specialised content readers could even transform the resource content from its original form to the new mimetype, e.g. converting an image from JPEG to PNG.

Parameters:
resource the resource for which to return the content
mimetype the mime type to retrieve of the content
Returns:
a byte array containing the content of the resource, or null if no content exists
    @Override
    public byte[] getContentData(Resource resourceString mimetype) {
        // iterate over all possible writers; if the pattern matches, try to store the content and return
        for(Pattern p : .keySet()) {
            if(p.matcher(resource.toString()).matches()) {
                ContentReader reader = .get(p);
                .debug("reading content for resource {} using reader {}",resource,reader.getName());
                try {
                    return reader.getContentData(resourcemimetype);
                } catch(IOException ex) {
                    .error("could not read content, reader threw an IO Exception",ex);
                    return null;
                }
            }
        }
        return null;
    }

    
Retrieve the content of the specified mime type for the specified resource. Returns a input stream containing the byte data of the content, or null, indicating that a content of the specified mime type does not exist for the resource.

Specialised content readers could even transform the resource content from its original form to the new mimetype, e.g. converting an image from JPEG to PNG.

This method is preferrable for resources with large amounts of data.

Parameters:
resource the resource for which to return the content
mimetype the mime type to retrieve of the content
Returns:
a InputStream containing the content of the resource, or null if no content exists
    @Override
    public InputStream getContentStream(Resource resourceString mimetypethrows IOException {
        // iterate over all possible writers; if the pattern matches, try to store the content and return
        for(Pattern p : .keySet()) {
            if(p.matcher(resource.toString()).matches()) {
                ContentReader reader = .get(p);
                .debug("reading content for resource {} using reader {}",resource,reader.getName());
                try {
                    return reader.getContentStream(resourcemimetype);
                } catch(IOException ex) {
                    .error("could not read content for resource {}, reader threw an IO Exception (message: {})",resource,ex.getMessage());
                    return null;
                }
            }
        }
        return null;
    }
    @Override
    public boolean hasContent(Resource resourceString mimetype) {
        // iterate over all possible writers; if the pattern matches, try to store the content and return
        for(Pattern p : .keySet()) {
            if(p.matcher(resource.toString()).matches()) {
                ContentReader reader = .get(p);
                return reader.hasContent(resourcemimetype);
            }
        }
        return false;
    }
    @Override
    public String getContentType(Resource resource) {
        // iterate over all possible writers; if the pattern matches, try to store the content and return
        for(Pattern p : .keySet()) {
            if(p.matcher(resource.stringValue()).matches()) {
                ContentReader reader = .get(p);
                return reader.getContentType(resource);
            }
        }
        return null;
    }


    
Return the number of bytes the content of this resource contains.

Parameters:
resource resource for which to return the content length
Returns:
byte count for the resource content
    @Override
    public long getContentLength(Resource resourceString mimetype) {
        // iterate over all possible writers; if the pattern matches, try to store the content and return
        for(Pattern p : .keySet()) {
            if(p.matcher(resource.toString()).matches()) {
                ContentReader reader = .get(p);
                return reader.getContentLength(resource,mimetype);
            }
        }
        return 0;
    }
    @Override
    public boolean deleteContent(Resource resourcethrows MarmottaException {
        // iterate over all possible writers; if the pattern matches, try to store the content and return
        for(Pattern p : .keySet()) {
            if(p.matcher(resource.toString()).matches()) {
                ContentWriter writer = .get(p);
                try {
                    writer.deleteContent(resource,"");
                    return true;
                } catch(IOException ex) {
                    .error("could not write content, writer threw an IO Exception",ex);
                    throw new WritingNotSupportedException(ex.getMessage(),ex);
                }
            }
        }
        return false;
    }
New to GrepCode? Check out our FAQ X