Search in sources :

Example 56 with ValueFormatException

use of javax.jcr.ValueFormatException in project jackrabbit-oak by apache.

the class PropertyTest method testBooleanRequiredTypeLong.

public void testBooleanRequiredTypeLong() throws Exception {
    try {
        Property p = node.setProperty(LONG_PROP_NAME, true);
        fail("Conversion from BOOLEAN to LONG must throw ValueFormatException");
    } catch (ValueFormatException e) {
    // success
    }
}
Also used : ValueFormatException(javax.jcr.ValueFormatException) Property(javax.jcr.Property)

Example 57 with ValueFormatException

use of javax.jcr.ValueFormatException in project sling by apache.

the class VotingView method vote.

/**
     * add a vote from the given slingId to this voting
     * @param slingId the slingId which is voting
     * @param vote true for a yes-vote, false for a no-vote
     */
public void vote(final String slingId, final Boolean vote, final String leaderElectionId) {
    if (logger.isDebugEnabled()) {
        logger.debug("vote: slingId=" + slingId + ", vote=" + vote);
    }
    Resource r = getResource();
    if (r == null) {
        logger.error("vote: no resource set. slingId = " + slingId + ", vote=" + vote);
        return;
    }
    Resource members = r.getChild("members");
    if (members == null) {
        logger.error("vote: no members resource available for " + r + ". slingId = " + slingId + ", vote=" + vote);
        return;
    }
    final Resource memberResource = members.getChild(slingId);
    if (memberResource == null) {
        if (vote == null || !vote) {
            // if I wanted to vote no or empty, then it's no big deal
            // that I can't find my entry ..
            logger.debug("vote: no memberResource found for slingId=" + slingId + ", vote=" + vote + ", resource=" + getResource());
        } else {
            // if I wanted to vote yes, then it is a big deal that I can't find myself
            logger.error("vote: no memberResource found for slingId=" + slingId + ", vote=" + vote + ", resource=" + getResource());
        }
        return;
    }
    final ModifiableValueMap memberMap = memberResource.adaptTo(ModifiableValueMap.class);
    if (vote == null) {
        if (memberMap.containsKey("vote")) {
            logger.info("vote: removing vote (vote==null) of slingId=" + slingId + " on: " + this);
        } else {
            logger.debug("vote: removing vote (vote==null) of slingId=" + slingId + " on: " + this);
        }
        memberMap.remove("vote");
    } else {
        boolean shouldVote = true;
        try {
            if (memberMap.containsKey("vote")) {
                Object v = memberMap.get("vote");
                if (v instanceof Property) {
                    Property p = (Property) v;
                    if (p.getBoolean() == vote) {
                        logger.debug("vote: already voted, with same vote (" + vote + "), not voting again");
                        shouldVote = false;
                    }
                } else if (v instanceof Boolean) {
                    Boolean b = (Boolean) v;
                    if (b == vote) {
                        logger.debug("vote: already voted, with same vote (" + vote + "), not voting again");
                        shouldVote = false;
                    }
                }
            }
        } catch (ValueFormatException e) {
            logger.warn("vote: got a ValueFormatException: " + e, e);
        } catch (RepositoryException e) {
            logger.warn("vote: got a RepositoryException: " + e, e);
        }
        if (shouldVote) {
            logger.info("vote: slingId=" + slingId + " is voting vote=" + vote + " on " + getResource());
            memberMap.put("vote", vote);
            memberMap.put("votedAt", Calendar.getInstance());
            String currentLeaderElectionId = memberMap.get("leaderElectionId", String.class);
            if (leaderElectionId != null && (currentLeaderElectionId == null || !currentLeaderElectionId.equals(leaderElectionId))) {
                // SLING-5030 : to ensure leader-step-down after being
                // isolated from the cluster, the leaderElectionId must
                // be explicitly set upon voting.
                // for 99% of the cases not be necessary,
                // for the rejoin-after-isolation case however it is
                logger.info("vote: changing leaderElectionId on vote to " + leaderElectionId);
                memberMap.put("leaderElectionId", leaderElectionId);
                memberMap.put("leaderElectionIdCreatedAt", new Date());
            }
        }
    }
    try {
        getResource().getResourceResolver().commit();
    } catch (PersistenceException e) {
        logger.error("vote: PersistenceException while voting: " + e, e);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ValueFormatException(javax.jcr.ValueFormatException) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date)

Example 58 with ValueFormatException

use of javax.jcr.ValueFormatException in project sling by apache.

the class DownloadBinaryProperty method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    String propertyName = request.getParameter("property");
    if ("".equals(propertyName) || propertyName == null)
        throw new ResourceNotFoundException("No property specified.");
    ServletOutputStream out = response.getOutputStream();
    Resource resource = request.getResource();
    Node currentNode = resource.adaptTo(Node.class);
    javax.jcr.Property property = null;
    try {
        property = currentNode.getProperty(propertyName);
    } catch (PathNotFoundException e) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found.");
        throw new ResourceNotFoundException("Not found.");
    } catch (RepositoryException e) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found.");
        throw new ResourceNotFoundException("Not found.");
    }
    InputStream stream = null;
    try {
        if (property == null || property.getType() != PropertyType.BINARY) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found.");
            throw new ResourceNotFoundException("Not found.");
        }
        long length = property.getLength();
        if (length > 0) {
            if (length < Integer.MAX_VALUE) {
                response.setContentLength((int) length);
            } else {
                response.setHeader("Content-Length", String.valueOf(length));
            }
        }
        stream = property.getStream();
        byte[] buf = new byte[2048];
        int rd;
        while ((rd = stream.read(buf)) >= 0) {
            out.write(buf, 0, rd);
        }
    } catch (ValueFormatException e) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error downloading the property content.");
        log.debug("error reading the property " + property + " of path " + resource.getPath(), e);
    } catch (RepositoryException e) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error downloading the property content.");
        log.debug("error reading the property " + property + " of path " + resource.getPath(), e);
    } finally {
        stream.close();
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) RepositoryException(javax.jcr.RepositoryException) ValueFormatException(javax.jcr.ValueFormatException) PathNotFoundException(javax.jcr.PathNotFoundException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Example 59 with ValueFormatException

use of javax.jcr.ValueFormatException in project sling by apache.

the class AuthorizableValueMap method convertToType.

// ---------- Implementation helper
@SuppressWarnings("unchecked")
private <T> T convertToType(String name, Class<T> type) {
    T result = null;
    try {
        if (authorizable.hasProperty(name)) {
            Value[] values = authorizable.getProperty(name);
            if (values == null) {
                return null;
            }
            boolean multiValue = values.length > 1;
            boolean array = type.isArray();
            if (multiValue) {
                if (array) {
                    result = (T) convertToArray(values, type.getComponentType());
                } else if (values.length > 0) {
                    result = convertToType(-1, values[0], type);
                }
            } else {
                Value value = values[0];
                if (array) {
                    result = (T) convertToArray(new Value[] { value }, type.getComponentType());
                } else {
                    result = convertToType(-1, value, type);
                }
            }
        }
    } catch (ValueFormatException vfe) {
        LOG.info("converToType: Cannot convert value of " + name + " to " + type, vfe);
    } catch (RepositoryException re) {
        LOG.info("converToType: Cannot get value of " + name, re);
    }
    // fall back to nothing
    return result;
}
Also used : Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) RepositoryException(javax.jcr.RepositoryException)

Example 60 with ValueFormatException

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

the class DecimalValue method getDate.

// ----------------------------------------------------------------< Value >
/**
 * {@inheritDoc}
 */
public Calendar getDate() throws ValueFormatException, IllegalStateException, RepositoryException {
    if (number != null) {
        // loosing timezone information...
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(number.longValue()));
        return cal;
    } else {
        throw new ValueFormatException("empty value");
    }
}
Also used : Calendar(java.util.Calendar) ValueFormatException(javax.jcr.ValueFormatException) Date(java.util.Date)

Aggregations

ValueFormatException (javax.jcr.ValueFormatException)106 Value (javax.jcr.Value)53 Property (javax.jcr.Property)34 RepositoryException (javax.jcr.RepositoryException)16 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)12 Calendar (java.util.Calendar)10 ItemNotFoundException (javax.jcr.ItemNotFoundException)10 Node (javax.jcr.Node)10 QValue (org.apache.jackrabbit.spi.QValue)10 PathNotFoundException (javax.jcr.PathNotFoundException)6 Name (org.apache.jackrabbit.spi.Name)6 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)5 InternalValue (org.apache.jackrabbit.core.value.InternalValue)5 InputStream (java.io.InputStream)4 BigDecimal (java.math.BigDecimal)4 Date (java.util.Date)4 Path (org.apache.jackrabbit.spi.Path)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3