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
}
}
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);
}
}
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();
}
}
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;
}
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");
}
}
Aggregations