Search in sources :

Example 11 with RepositoryException

use of javax.jcr.RepositoryException in project jackrabbit by apache.

the class SearchManager method createQueryInstance.

/**
     * Creates a new instance of an {@link AbstractQueryImpl} which is not
     * initialized.
     *
     * @return an new query instance.
     * @throws RepositoryException if an error occurs while creating a new query
     *                             instance.
     */
protected AbstractQueryImpl createQueryInstance() throws RepositoryException {
    try {
        String queryImplClassName = handler.getQueryClass();
        Object obj = Class.forName(queryImplClassName).newInstance();
        if (obj instanceof AbstractQueryImpl) {
            return (AbstractQueryImpl) obj;
        } else {
            throw new IllegalArgumentException(queryImplClassName + " is not of type " + AbstractQueryImpl.class.getName());
        }
    } catch (Throwable t) {
        throw new RepositoryException("Unable to create query: " + t.toString(), t);
    }
}
Also used : AbstractQueryImpl(org.apache.jackrabbit.core.query.AbstractQueryImpl) RepositoryException(javax.jcr.RepositoryException)

Example 12 with RepositoryException

use of javax.jcr.RepositoryException in project jackrabbit by apache.

the class SessionImpl method impersonate.

/**
     * {@inheritDoc}
     */
@Override
public Session impersonate(Credentials otherCredentials) throws LoginException, RepositoryException {
    // check sanity of this session
    sanityCheck();
    if (!(otherCredentials instanceof SimpleCredentials)) {
        String msg = "impersonate failed: incompatible credentials, SimpleCredentials expected";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    // set IMPERSONATOR_ATTRIBUTE attribute of given credentials
    // with subject of current session
    SimpleCredentials creds = (SimpleCredentials) otherCredentials;
    creds.setAttribute(SecurityConstants.IMPERSONATOR_ATTRIBUTE, subject);
    try {
        return getRepository().login(otherCredentials, getWorkspace().getName());
    } catch (NoSuchWorkspaceException nswe) {
        // should never get here...
        String msg = "impersonate failed";
        log.error(msg, nswe);
        throw new RepositoryException(msg, nswe);
    } finally {
        // make sure IMPERSONATOR_ATTRIBUTE is removed
        creds.removeAttribute(SecurityConstants.IMPERSONATOR_ATTRIBUTE);
    }
}
Also used : NoSuchWorkspaceException(javax.jcr.NoSuchWorkspaceException) SimpleCredentials(javax.jcr.SimpleCredentials) RepositoryException(javax.jcr.RepositoryException)

Example 13 with RepositoryException

use of javax.jcr.RepositoryException in project jackrabbit by apache.

the class QueryObjectModelImpl method init.

/**
     * Initializes a query instance from a query object model.
     *
     * @param sessionContext component context of the current session
     * @param handler  the query handler of the search index.
     * @param qomTree  the query object model tree.
     * @param language the original query syntax from where the JQOM was
     *                 created.
     * @param node     a nt:query node where the query was read from or
     *                 <code>null</code> if it is not a stored query.
     * @throws InvalidQueryException if the qom tree cannot be serialized
     *                               according to the given language.
     * @throws RepositoryException   if another error occurs
     */
public void init(SessionContext sessionContext, QueryHandler handler, QueryObjectModelTree qomTree, String language, Node node) throws InvalidQueryException, RepositoryException {
    checkNotInitialized();
    this.sessionContext = sessionContext;
    this.language = language;
    this.handler = handler;
    this.qomTree = qomTree;
    this.node = node;
    this.statement = QueryObjectModelBuilderRegistry.getQueryObjectModelBuilder(language).toString(this);
    try {
        qomTree.accept(new DefaultTraversingQOMTreeVisitor() {

            @Override
            public Object visit(BindVariableValueImpl node, Object data) {
                variables.put(node.getBindVariableName(), null);
                return data;
            }
        }, null);
    } catch (Exception ignore) {
    }
    this.lqf = new LuceneQueryFactory(sessionContext.getSessionImpl(), (SearchIndex) handler, variables);
    setInitialized();
}
Also used : DefaultTraversingQOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor) BindVariableValueImpl(org.apache.jackrabbit.spi.commons.query.qom.BindVariableValueImpl) SearchIndex(org.apache.jackrabbit.core.query.lucene.SearchIndex) LuceneQueryFactory(org.apache.jackrabbit.core.query.lucene.LuceneQueryFactory) RepositoryException(javax.jcr.RepositoryException) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 14 with RepositoryException

use of javax.jcr.RepositoryException in project jackrabbit by apache.

the class BundleWriter method writeState.

/**
     * Serializes a property entry. The serialization begins with the
     * property name followed by a single byte that encodes the type and
     * multi-valuedness of the property:
     * <pre>
     * +-------------------------------+
     * |   mv count    |     type      |
     * +-------------------------------+
     * </pre>
     * <p>
     * The lower four bits encode the property type (0-12 in JCR 2.0) and
     * higher bits indicate whether this is a multi-valued property and how
     * many property values there are. A value of 0 is reserved for
     * single-valued properties (that are guaranteed to always have just a
     * single value), and all non-zero values indicate a multi-valued property.
     * <p>
     * In multi-valued properties the exact value of the "mv count" field is
     * the number of property values plus one and truncated at 15 (the highest
     * four-bit value). If there are 14 or more (14 + 1 == 15) property values,
     * then the number of additional values is serialized as a variable-length
     * integer (see {@link #writeVarInt(int)}) right after this byte.
     * <p>
     * The modification count of the property state is written next as a
     * variable-length integer, followed by the serializations of all the
     * values of this property.
     *
     * @param state the property entry to store
     * @throws IOException if an I/O error occurs.
     */
private void writeState(NodePropBundle.PropertyEntry state) throws IOException {
    writeName(state.getName());
    InternalValue[] values = state.getValues();
    int type = state.getType();
    if (type < 0 || type > 0xf) {
        throw new IOException("Illegal property type " + type);
    }
    if (state.isMultiValued()) {
        int len = values.length + 1;
        if (len < 0x0f) {
            out.writeByte(len << 4 | type);
        } else {
            out.writeByte(0xf0 | type);
            writeVarInt(len - 0x0f);
        }
    } else {
        if (values.length != 1) {
            throw new IOException("Single values property with " + values.length + " values: " + state.getName());
        }
        out.writeByte(type);
    }
    writeVarInt(state.getModCount());
    // values
    for (int i = 0; i < values.length; i++) {
        InternalValue val = values[i];
        switch(type) {
            case PropertyType.BINARY:
                try {
                    long size = val.getLength();
                    if (val.isInDataStore()) {
                        out.writeInt(BundleBinding.BINARY_IN_DATA_STORE);
                        writeString(val.toString());
                    } else if (binding.dataStore != null) {
                        writeSmallBinary(val, state, i);
                    } else if (size < 0) {
                        log.warn("Blob has negative size. Potential loss of data. " + "id={} idx={}", state.getId(), String.valueOf(i));
                        out.writeInt(0);
                        values[i] = InternalValue.create(new byte[0]);
                        val.discard();
                    } else if (size > binding.getMinBlobSize()) {
                        // special handling required for binary value:
                        // spool binary value to file in blob store
                        out.writeInt(BundleBinding.BINARY_IN_BLOB_STORE);
                        String blobId = state.getBlobId(i);
                        if (blobId == null) {
                            BLOBStore blobStore = binding.getBlobStore();
                            try {
                                InputStream in = val.getStream();
                                try {
                                    blobId = blobStore.createId(state.getId(), i);
                                    blobStore.put(blobId, in, size);
                                    state.setBlobId(blobId, i);
                                } finally {
                                    IOUtils.closeQuietly(in);
                                }
                            } catch (Exception e) {
                                String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + size;
                                log.error(msg, e);
                                throw new IOExceptionWithCause(msg, e);
                            }
                            try {
                                // backed by resource in blob store and delete temp file
                                if (blobStore instanceof ResourceBasedBLOBStore) {
                                    values[i] = InternalValue.create(((ResourceBasedBLOBStore) blobStore).getResource(blobId));
                                } else {
                                    values[i] = InternalValue.create(blobStore.get(blobId));
                                }
                            } catch (Exception e) {
                                log.error("Error while reloading blob. truncating. id=" + state.getId() + " idx=" + i + " size=" + size, e);
                                values[i] = InternalValue.create(new byte[0]);
                            }
                            val.discard();
                        }
                        // store id of blob as property value
                        // value
                        writeString(blobId);
                    } else {
                        // delete evt. blob
                        byte[] data = writeSmallBinary(val, state, i);
                        // replace value instance with value
                        // backed by resource in blob store and delete temp file
                        values[i] = InternalValue.create(data);
                        val.discard();
                    }
                } catch (RepositoryException e) {
                    String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " value=" + val;
                    log.error(msg, e);
                    throw new IOExceptionWithCause(msg, e);
                }
                break;
            case PropertyType.DOUBLE:
                try {
                    out.writeDouble(val.getDouble());
                } catch (RepositoryException e) {
                    throw convertToIOException(type, e);
                }
                break;
            case PropertyType.DECIMAL:
                try {
                    writeDecimal(val.getDecimal());
                } catch (RepositoryException e) {
                    throw convertToIOException(type, e);
                }
                break;
            case PropertyType.LONG:
                try {
                    writeVarLong(val.getLong());
                } catch (RepositoryException e) {
                    throw convertToIOException(type, e);
                }
                break;
            case PropertyType.BOOLEAN:
                try {
                    out.writeBoolean(val.getBoolean());
                } catch (RepositoryException e) {
                    throw convertToIOException(type, e);
                }
                break;
            case PropertyType.NAME:
                try {
                    writeName(val.getName());
                } catch (RepositoryException e) {
                    throw convertToIOException(type, e);
                }
                break;
            case PropertyType.WEAKREFERENCE:
            case PropertyType.REFERENCE:
                writeNodeId(val.getNodeId());
                break;
            case PropertyType.DATE:
                try {
                    writeDate(val.getCalendar());
                } catch (RepositoryException e) {
                    throw convertToIOException(type, e);
                }
                break;
            case PropertyType.STRING:
            case PropertyType.PATH:
            case PropertyType.URI:
                writeString(val.toString());
                break;
            default:
                throw new IOException("Inknown property type: " + type);
        }
    }
}
Also used : IOExceptionWithCause(org.apache.commons.io.IOExceptionWithCause) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) IOException(java.io.IOException) RepositoryException(javax.jcr.RepositoryException)

Example 15 with RepositoryException

use of javax.jcr.RepositoryException in project jackrabbit by apache.

the class QueryImpl method storeAsNode.

/**
     * {@inheritDoc}
     */
public Node storeAsNode(String absPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, UnsupportedRepositoryOperationException, RepositoryException {
    checkInitialized();
    try {
        Path p = sessionContext.getQPath(absPath).getNormalizedPath();
        if (!p.isAbsolute()) {
            throw new RepositoryException(absPath + " is not an absolute path");
        }
        String relPath = sessionContext.getJCRPath(p).substring(1);
        Node queryNode = sessionContext.getSessionImpl().getRootNode().addNode(relPath, sessionContext.getJCRName(NT_QUERY));
        // set properties
        queryNode.setProperty(sessionContext.getJCRName(JCR_LANGUAGE), language);
        queryNode.setProperty(sessionContext.getJCRName(JCR_STATEMENT), statement);
        node = queryNode;
        return node;
    } catch (NameException e) {
        throw new RepositoryException(e.getMessage(), e);
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException)

Aggregations

RepositoryException (javax.jcr.RepositoryException)1236 Node (javax.jcr.Node)289 Session (javax.jcr.Session)182 IOException (java.io.IOException)156 ArrayList (java.util.ArrayList)106 Name (org.apache.jackrabbit.spi.Name)94 DavException (org.apache.jackrabbit.webdav.DavException)90 Test (org.junit.Test)87 Value (javax.jcr.Value)80 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)76 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)72 Path (org.apache.jackrabbit.spi.Path)67 ItemNotFoundException (javax.jcr.ItemNotFoundException)65 PathNotFoundException (javax.jcr.PathNotFoundException)65 NodeId (org.apache.jackrabbit.core.id.NodeId)64 Property (javax.jcr.Property)61 HashMap (java.util.HashMap)53 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)53 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)52 InvalidItemStateException (javax.jcr.InvalidItemStateException)50