public static RuntimeException propagate(Throwable e) { if (e instanceof RuntimeException) { return (RuntimeException) e; } return new DbAccessException(e); }
private List<?> loadFromBackReferences(String thisId, DocumentReferences ann, CollectionType collectionType, String fieldName) throws JsonMappingException { try { Class<?> raw = collectionType.getRawClass(); if (Set.class.isAssignableFrom(raw)) { ViewQuery query = createBackReferenceQuery(thisId, ann, fieldName); Class<?> klass = collectionType.getContentType().getRawClass(); return loadSetResult(query, klass); } else { throw new DbAccessException("Unsupported back reference collection type: " + raw); } } catch (DbAccessException e) { if (e.getCause() instanceof JsonProcessingException) { JsonProcessingException jpe = (JsonProcessingException) e .getCause(); throw JsonMappingException.wrapWithPath(jpe, new JsonMappingException.Reference(collectionType .getContentType().getRawClass()), fieldName); } else { throw e; } } }
@Override public Boolean error(HttpResponse hr) { if (hr.getCode() == HttpStatus.NOT_FOUND) { return Boolean.FALSE; } throw new DbAccessException(hr.toString()); }
@Override public boolean apply(Field input) { DocumentReferences referenceMetaData = input.getAnnotation(DocumentReferences.class); if (referenceMetaData != null) { if (Set.class.isAssignableFrom(input.getType())) { try { input.setAccessible(true); Collection<?> d = findDocumentsToSave((Set<?>) input.get(value)); docsToSave.addAll(d); } catch (IllegalAccessException e) { throw Exceptions.propagate(e); } } else { throw new DbAccessException("@DocumentReferences can only be used on java.util.Set"); } } return false; } });
public void createDatabase(DbPath db) { if (!createDatabaseIfNotExists(db)) { throw new DbAccessException(format("A database with path %s already exists", db.getPath())); } }
public StreamingChangesResult(ObjectMapper objectMapper, HttpResponse response) { this.response = response; try{ jp = objectMapper.getFactory().createParser(response.getContent()); jp.nextValue(); jp.nextValue(); jp.nextToken(); }catch(Exception e) { throw new DbAccessException(e); } }
@edu.umd.cs.findbugs.annotations.SuppressWarnings({"DM_DEFAULT_ENCODING", "NP_DEREFERENCE_OF_READLINE_VALUE"}) public StreamingViewResult(ObjectMapper objectMapper, HttpResponse httpResponse, boolean ignoreNotFound) { this.objectMapper = objectMapper; this.httpResponse = httpResponse; this.ignoreNotFound = ignoreNotFound; reader = new BufferedReader(new InputStreamReader(httpResponse.getContent())); try{ String info = reader.readLine(); totalRows = getFieldValue(info, TOTAL_ROWS_FIELD_NAME); offset = getFieldValue(info, OFFSET_FIELD_NAME); }catch(IOException e) { throw new DbAccessException(e); } }
JsonParser jp = jsonFactory.createParser(hr.getContent()); if (jp.nextToken() != JsonToken.START_OBJECT) { throw new DbAccessException("Expected data to start with an Object"); if ("total_rows".equals(fieldName)) { if (result != null) { throw new DbAccessException("Two total_rows were provided."); } else if ("rows".equals(fieldName)) { if (result == null) { throw new DbAccessException("total_rows is required to be included in value before rows"); throw new DbAccessException("rows's value must be an array");
private void throwBulkUpdateError(List<DocumentOperationResult> res) { StringBuilder sb = new StringBuilder(); int maxErrors = 10; for (DocumentOperationResult docResult : res) { if (maxErrors == 0) { sb.append(".. " + res.size() + " more "); break; } sb.append(docResult.getId()); sb.append(" "); sb.append(docResult.getError()); sb.append(" "); sb.append(docResult.getReason()); sb.append(" "); maxErrors--; } throw new DbAccessException(sb.toString()); }
private void throwBulkUpdateError(List<DocumentOperationResult> res) { StringBuilder sb = new StringBuilder(); int maxErrors = 10; for (DocumentOperationResult docResult : res) { if (maxErrors == 0) { sb.append(".. " + res.size() + " more "); break; } sb.append(docResult.getId()); sb.append(" "); sb.append(docResult.getError()); sb.append(" "); sb.append(docResult.getReason()); sb.append(" "); maxErrors--; } throw new DbAccessException(sb.toString()); }
public boolean hasNext() { if (closed) { // The BufferedReader is closed. There can't be any more rows. return false; } if (row != null) { // We still already have an 'uncollected' row from last time. return true; } try { JsonNode node; do { String doc = reader.readLine(); if (doc == null || doc.equals("]}")) { reader.close(); closed = true; return false; } if (doc.endsWith(",")) { doc = doc.substring(0, doc.length() -1); } node = objectMapper.readTree(doc); }while(ignoreNotFound && node.has(Row.ERROR_FIELD_NAME)); row = new ViewResult.Row(node); return true; } catch (IOException e) { throw new DbAccessException(e); } }
private void parseResult(JsonParser jp) throws IOException { if (jp.nextToken() != JsonToken.START_OBJECT) { throw new DbAccessException("Expected data to start with an Object"); } Map<String, String> errorFields = new HashMap<String, String>(); // Issue #98: Can't assume order of JSON fields. while (jp.nextValue() != JsonToken.END_OBJECT) { String currentName = jp.getCurrentName(); if (OFFSET_FIELD_NAME.equals(currentName)) { offset = jp.getLongValue(); } else if (TOTAL_ROWS_FIELD_NAME.equals(currentName)) { totalRows = jp.getIntValue(); } else if (ROWS_FIELD_NAME.equals(currentName)) { rows = new ArrayList<T>(); parseRows(jp); } else if (UPDATE_SEQUENCE_NAME.equals(currentName)) { updateSequence = jp.getLongValue(); } else { // Handle cloudant errors. errorFields.put(jp.getCurrentName(), jp.getText()); } } if (!errorFields.isEmpty()) { JsonNode error = mapper.convertValue(errorFields, JsonNode.class); throw new DbAccessException(error.toString()); } }
protected void findNext() { if (hasNext == null) { try { JsonNode jsonNode = jp.readValueAs(JsonNode.class); if (jsonNode == null) { jsonNode = jp.readValueAs(JsonNode.class); lastSeq = jsonNode.get("last_seq").longValue(); close(); hasNext = false; } else { row = new StdDocumentChange(jsonNode); hasNext = true; } } catch (Exception e) { throw new DbAccessException(e); } } } public void remove() {
protected static <T> T checkResponseBodyOkAndReturnDefaultValue(HttpResponse hr, T defaultValue, ObjectMapper mapper) throws IOException { InputStream content = hr.getContent(); try { content = hr.getContent(); JsonNode body = responseBodyAsNode(content, MAPPER); JsonNode okNode = body.get("ok"); if (okNode != null) { if (okNode.isBoolean()) { if (okNode.booleanValue()) { return defaultValue; } } } throw new DbAccessException("Unexpected response body content, expected {\"ok\":true}, got " + body.toString()); } finally { IOUtils.closeQuietly(content); } }
throw new DbAccessException("@DocumentReferences can only be used on java.util.Set");
private void parseRows(JsonParser jp) throws IOException { if (jp.getCurrentToken() != JsonToken.START_ARRAY) { throw new DbAccessException("Expected rows to start with an Array");
body = "unavailable"; return new DbAccessException(hr.toString() + "\nURI: " + hr.getRequestURI() + "\nResponse Body: \n" + body);