Example usage for org.apache.http.message BasicHeaderValueParser parseElements

List of usage examples for org.apache.http.message BasicHeaderValueParser parseElements

Introduction

In this page you can find the example usage for org.apache.http.message BasicHeaderValueParser parseElements.

Prototype

public HeaderElement[] parseElements(CharArrayBuffer charArrayBuffer, ParserCursor parserCursor) 

Source Link

Usage

From source file:org.opendatakit.odktables.InstanceFileManager.java

public void postFiles(String tableId, String rowId, MultiPart multiPart, TablesUserPermissions userPermissions)
        throws IOException, ODKTaskLockException, ODKTablesException, ODKDatastoreException {

    try {//from   w w w. j  a  v  a  2  s.co  m
        if (tableId == null) {
            throw new IllegalArgumentException("tableId cannot be null!");
        }

        if (rowId == null) {
            throw new IllegalArgumentException("rowId cannot be null!");
        }

        userPermissions.checkPermission(appId, tableId, TablePermission.WRITE_ROW);

        OdkTablesLockTemplate propsLock = new OdkTablesLockTemplate(tableId, rowId,
                ODKTablesTaskLockType.TABLES_NON_PERMISSIONS_CHANGES, OdkTablesLockTemplate.DelayStrategy.LONG,
                cc);

        try {
            propsLock.acquire();

            // fetch these once and then continue to re-use them.
            DbTableInstanceFiles blobStore = new DbTableInstanceFiles(tableId, cc);
            BlobEntitySet instance = blobStore.newBlobEntitySet(rowId, cc);

            ODKTablesException e = null;
            // Parse the request
            for (BodyPart bodyPart : multiPart.getBodyParts()) {

                MultivaluedMap<String, String> headers = bodyPart.getHeaders();
                String disposition = (headers != null) ? headers.getFirst("Content-Disposition") : null;
                if (disposition == null) {
                    e = new ODKTablesException(InstanceFileService.ERROR_MSG_MULTIPART_FILES_ONLY_EXPECTED);
                    continue;
                }
                String partialPath = null;
                {
                    HeaderValueParser parser = new BasicHeaderValueParser();
                    HeaderElement[] values = BasicHeaderValueParser.parseElements(disposition, parser);
                    for (HeaderElement v : values) {
                        if (v.getName().equalsIgnoreCase("file")) {
                            partialPath = v.getParameterByName("filename").getValue();
                            break;
                        }
                    }
                }
                if (partialPath == null) {
                    e = new ODKTablesException(
                            InstanceFileService.ERROR_MSG_MULTIPART_CONTENT_FILENAME_EXPECTED);
                    continue;
                }

                String contentType = (headers != null) ? headers.getFirst("Content-Type") : null;

                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                InputStream bi = null;
                BodyPartEntity bodyPartEntity = (BodyPartEntity) bodyPart.getEntity();
                try {
                    bi = new BufferedInputStream(bodyPartEntity.getInputStream());
                    int length = 1024;
                    // Transfer bytes from in to out
                    byte[] data = new byte[length];
                    int len;
                    while ((len = bi.read(data, 0, length)) >= 0) {
                        if (len != 0) {
                            bo.write(data, 0, len);
                        }
                    }
                } finally {
                    bi.close();
                }
                byte[] content = bo.toByteArray();
                String md5Hash = PersistenceUtils.newMD5HashUri(content);

                // we are adding one or more files -- delete any cached ETag value for
                // this row's attachments manifest
                try {
                    DbTableInstanceManifestETagEntity entity = DbTableInstanceManifestETags
                            .getRowIdEntry(tableId, rowId, cc);
                    entity.delete(cc);
                } catch (ODKEntityNotFoundException ex) {
                    // ignore... it might already be deleted or have never existed
                }

                int count = instance.getAttachmentCount(cc);
                boolean found = false;
                for (int i = 1; i <= count; ++i) {
                    String path = instance.getUnrootedFilename(i, cc);
                    if (path != null && path.equals(partialPath)) {
                        // we already have this in our store -- check that it is
                        // identical.
                        // if not, we have a problem!!!
                        if (md5Hash.equals(instance.getContentHash(i, cc))) {
                            // no-op
                            found = true;
                        } else {
                            // this is an error case; indicated by setting exception
                            found = true;
                            e = new InstanceFileModificationException(
                                    ERROR_FILE_VERSION_DIFFERS + "\n" + partialPath);
                            break;
                        }
                    }
                }
                if (!found) {
                    BlobSubmissionOutcome outcome = instance.addBlob(content, contentType, partialPath, false,
                            cc);
                    if (outcome == BlobSubmissionOutcome.NEW_FILE_VERSION) {
                        e = new InstanceFileModificationException(
                                ERROR_FILE_VERSION_DIFFERS + "\n" + partialPath);
                    }
                }
            }
            if (e != null) {
                throw e;
            }
        } finally {
            propsLock.release();
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
        throw e;
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw e;
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
        throw e;
    } catch (ODKTaskLockException e) {
        e.printStackTrace();
        throw e;
    } catch (PermissionDeniedException e) {
        e.printStackTrace();
        throw e;
    } catch (ODKDatastoreException e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:org.opendatakit.aggregate.odktables.InstanceFileManager.java

public void postFiles(String tableId, String rowId, InMultiPart inMP, TablesUserPermissions userPermissions)
        throws IOException, ODKTaskLockException, ODKTablesException, ODKDatastoreException {

    try {// w  w  w  .j a  v a 2s  . co m
        if (tableId == null) {
            throw new IllegalArgumentException("tableId cannot be null!");
        }

        if (rowId == null) {
            throw new IllegalArgumentException("rowId cannot be null!");
        }

        userPermissions.checkPermission(appId, tableId, TablePermission.WRITE_ROW);

        OdkTablesLockTemplate propsLock = new OdkTablesLockTemplate(tableId, rowId,
                ODKTablesTaskLockType.TABLES_NON_PERMISSIONS_CHANGES, OdkTablesLockTemplate.DelayStrategy.LONG,
                cc);

        try {
            propsLock.acquire();

            // fetch these once and then continue to re-use them.
            DbTableInstanceFiles blobStore = new DbTableInstanceFiles(tableId, cc);
            BlobEntitySet instance = blobStore.newBlobEntitySet(rowId, cc);

            ODKTablesException e = null;
            // Parse the request
            while (inMP.hasNext()) {
                InPart part = inMP.next();
                MultivaluedMap<String, String> headers = part.getHeaders();
                String disposition = (headers != null) ? headers.getFirst("Content-Disposition") : null;
                if (disposition == null) {
                    e = new ODKTablesException(InstanceFileService.ERROR_MSG_MULTIPART_FILES_ONLY_EXPECTED);
                    continue;
                }
                String partialPath = null;
                {
                    HeaderValueParser parser = new BasicHeaderValueParser();
                    HeaderElement[] values = BasicHeaderValueParser.parseElements(disposition, parser);
                    for (HeaderElement v : values) {
                        if (v.getName().equalsIgnoreCase("file")) {
                            partialPath = v.getParameterByName("filename").getValue();
                            break;
                        }
                    }
                }
                if (partialPath == null) {
                    e = new ODKTablesException(
                            InstanceFileService.ERROR_MSG_MULTIPART_CONTENT_FILENAME_EXPECTED);
                    continue;
                }

                String contentType = (headers != null) ? headers.getFirst("Content-Type") : null;

                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                InputStream bi = null;
                try {
                    bi = new BufferedInputStream(part.getInputStream());
                    int length = 1024;
                    // Transfer bytes from in to out
                    byte[] data = new byte[length];
                    int len;
                    while ((len = bi.read(data, 0, length)) >= 0) {
                        if (len != 0) {
                            bo.write(data, 0, len);
                        }
                    }
                } finally {
                    bi.close();
                }
                byte[] content = bo.toByteArray();
                String md5Hash = PersistenceUtils.newMD5HashUri(content);

                // we are adding one or more files -- delete any cached ETag value for
                // this row's attachments manifest
                try {
                    DbTableInstanceManifestETagEntity entity = DbTableInstanceManifestETags
                            .getRowIdEntry(tableId, rowId, cc);
                    entity.delete(cc);
                } catch (ODKEntityNotFoundException ex) {
                    // ignore... it might already be deleted or have never existed
                }

                int count = instance.getAttachmentCount(cc);
                boolean found = false;
                for (int i = 1; i <= count; ++i) {
                    String path = instance.getUnrootedFilename(i, cc);
                    if (path != null && path.equals(partialPath)) {
                        // we already have this in our store -- check that it is
                        // identical.
                        // if not, we have a problem!!!
                        if (md5Hash.equals(instance.getContentHash(i, cc))) {
                            // no-op
                            found = true;
                        } else {
                            // this is an error case; indicated by setting exception
                            found = true;
                            e = new InstanceFileModificationException(
                                    ERROR_FILE_VERSION_DIFFERS + "\n" + partialPath);
                            break;
                        }
                    }
                }
                if (!found) {
                    BlobSubmissionOutcome outcome = instance.addBlob(content, contentType, partialPath, false,
                            cc);
                    if (outcome == BlobSubmissionOutcome.NEW_FILE_VERSION) {
                        e = new InstanceFileModificationException(
                                ERROR_FILE_VERSION_DIFFERS + "\n" + partialPath);
                    }
                }
            }
            if (e != null) {
                throw e;
            }
        } finally {
            propsLock.release();
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
        throw e;
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw e;
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
        throw e;
    } catch (ODKTaskLockException e) {
        e.printStackTrace();
        throw e;
    } catch (PermissionDeniedException e) {
        e.printStackTrace();
        throw e;
    } catch (ODKDatastoreException e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:org.eclipse.lyo.oslc.v3.sample.BugContainer.java

private void parsePrefer() {
    final List<String> preferValues = headers.getRequestHeader(PREFER);
    if (preferValues == null) {
        return;/*from w  w  w  .  j ava  2 s .  c o  m*/
    }

    for (String prefer : preferValues) {
        HeaderElement[] preferElements = BasicHeaderValueParser.parseElements(prefer, null);
        for (HeaderElement e : preferElements) {
            if ("return".equals(e.getName()) && "representation".equals(e.getValue())) {
                addValues(e.getParameterByName("include"), include);
                addValues(e.getParameterByName("omit"), omit);
            }
        }
    }
}

From source file:org.apereo.portal.portlet.container.PortletResourceResponseContextImpl.java

/**
 * Handles resource response specific headers. Returns true if the header was consumed by this method and requires no further processing
 * /*from ww w.ja  v  a2  s . c o  m*/
 * @return
 */
protected boolean handleResourceHeader(String key, String value) {
    if (ResourceResponse.HTTP_STATUS_CODE.equals(key)) {
        this.portletResourceOutputHandler.setStatus(Integer.parseInt(value));
        return true;
    }
    if ("Content-Type".equals(key)) {
        final ContentType contentType = ContentType.parse(value);
        final Charset charset = contentType.getCharset();
        if (charset != null) {
            this.portletResourceOutputHandler.setCharacterEncoding(charset.name());
        }
        this.portletResourceOutputHandler.setContentType(contentType.getMimeType());
        return true;
    }
    if ("Content-Length".equals(key)) {
        this.portletResourceOutputHandler.setContentLength(Integer.parseInt(value));
        return true;
    }
    if ("Content-Language".equals(key)) {
        final HeaderElement[] parts = BasicHeaderValueParser.parseElements(value, null);
        if (parts.length > 0) {
            final String localeStr = parts[0].getValue();
            final Locale locale = LocaleUtils.toLocale(localeStr);
            this.portletResourceOutputHandler.setLocale(locale);
            return true;
        }
    }

    return false;
}