001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.client.impl;
017
018import static org.slf4j.LoggerFactory.getLogger;
019
020import java.io.InputStream;
021
022import org.fcrepo.client.NotFoundException;
023
024import org.apache.http.client.HttpClient;
025
026import org.fcrepo.client.FedoraContent;
027import org.fcrepo.client.FedoraDatastream;
028import org.fcrepo.client.FedoraException;
029import org.fcrepo.client.FedoraObject;
030import org.fcrepo.client.FedoraRepository;
031import org.fcrepo.client.ReadOnlyException;
032import org.fcrepo.client.utils.HttpHelper;
033import org.slf4j.Logger;
034
035/**
036 * Read-only FedoraRepository implementation that throws a ReadOnlyException any time a write operation is attempted.
037 *
038 * @author escowles
039 * @since 2014-08-25
040 */
041public class ReadOnlyFedoraRepositoryImpl extends FedoraRepositoryImpl implements FedoraRepository {
042    private static final Logger LOGGER = getLogger(ReadOnlyFedoraRepositoryImpl.class);
043    private static final String msg = "Write operation attempted using read-only repository";
044
045    /**
046     * Constructor that takes the repository url
047     *
048     * @param repositoryURL Fedora base URL.
049     */
050    public ReadOnlyFedoraRepositoryImpl(final String repositoryURL) {
051        this.repositoryURL = repositoryURL;
052        this.httpHelper = new HttpHelper(repositoryURL, null, null, true);
053    }
054
055    /**
056     * Constructor that takes the repoistory url and username/password for connecting
057     *
058     * @param repositoryURL Repository base URL
059     * @param username Repository username
060     * @param password Repository password
061     */
062    public ReadOnlyFedoraRepositoryImpl(final String repositoryURL, final String username, final String password) {
063        this.repositoryURL = repositoryURL;
064        this.httpHelper = new HttpHelper(repositoryURL, username, password, true);
065    }
066
067    /**
068     * Constructor that takes the pre-configured HttpClient
069     *
070     * @param repositoryURL Repository baseURL
071     * @param httpClient Pre-configured httpClient
072     */
073    public ReadOnlyFedoraRepositoryImpl(final String repositoryURL, final HttpClient httpClient) {
074        this.repositoryURL = repositoryURL;
075        this.httpHelper = new HttpHelper(repositoryURL, httpClient, true);
076    }
077
078    @Override
079    public FedoraDatastream createDatastream(final String path, final FedoraContent content) throws ReadOnlyException {
080        LOGGER.warn(msg);
081        throw new ReadOnlyException();
082    }
083
084    @Override
085    public FedoraObject createObject(final String path) throws FedoraException {
086        LOGGER.warn(msg);
087        throw new ReadOnlyException();
088    }
089
090    @Override
091    public FedoraDatastream findOrCreateDatastream(final String path) throws FedoraException {
092        try {
093            return getDatastream(path);
094        } catch ( NotFoundException ex ) {
095            LOGGER.warn(msg);
096            throw new ReadOnlyException();
097        }
098    }
099
100    @Override
101    public FedoraObject findOrCreateObject(final String path) throws FedoraException {
102        try {
103            return getObject(path);
104        } catch ( NotFoundException ex ) {
105            LOGGER.warn(msg);
106            throw new ReadOnlyException();
107        }
108    }
109
110    @Override
111    public void registerNodeTypes(final InputStream cndStream) throws ReadOnlyException {
112        LOGGER.warn(msg);
113        throw new ReadOnlyException();
114    }
115
116    @Override
117    public void addNamespace(final String prefix, final String uri) throws ReadOnlyException {
118        LOGGER.warn(msg);
119        throw new ReadOnlyException();
120    }
121
122    @Override
123    public void removeNamespace(final String prefix) throws ReadOnlyException {
124        LOGGER.warn(msg);
125        throw new ReadOnlyException();
126    }
127
128    @Override
129    public boolean isWritable() {
130        return false;
131    }
132}