Search in sources :

Example 46 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class WMultiFileWidget method doHandleUploadRequest.

/**
 * The request is a targeted file upload request. Upload the file and respond with the file information.
 *
 * @param request the request being processed.
 */
protected void doHandleUploadRequest(final Request request) {
    // Protect against client-side tampering of disabled/read-only fields.
    if (isDisabled() || isReadOnly()) {
        throw new SystemException("File widget cannot be updated.");
    }
    // Only process on a POST
    if (!"POST".equals(request.getMethod())) {
        throw new SystemException("File widget cannot be updated by " + request.getMethod() + ".");
    }
    // Check only one file item in the request
    FileItem[] items = request.getFileItems(getId());
    if (items.length > 1) {
        throw new SystemException("More than one file item received on the request.");
    }
    // Check the client provided a fileID
    String fileId = request.getParameter(FILE_UPLOAD_MULTI_PART_ID_KEY);
    if (fileId == null) {
        throw new SystemException("No file id provided for file upload.");
    }
    // Wrap the file item
    FileItemWrap wrap = new FileItemWrap(items[0]);
    FileWidgetUpload file = new FileWidgetUpload(fileId, wrap);
    addFile(file);
    // Set the file id to be used ion the renderer
    setFileUploadRequestId(fileId);
    setNewUpload(true);
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) SystemException(com.github.bordertech.wcomponents.util.SystemException) FileItemWrap(com.github.bordertech.wcomponents.file.FileItemWrap)

Example 47 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class WRepeater method preparePaintComponent.

/**
 * Override preparePaintComponent to prepare each row for painting.
 *
 * @param request the request being responded to.
 */
@Override
protected void preparePaintComponent(final Request request) {
    assertConfigured();
    List beanList = getBeanList();
    List<Integer> used = new ArrayList<>();
    for (int i = 0; i < beanList.size(); i++) {
        Object rowData = beanList.get(i);
        // Each row has its own context. This is why we can reuse the same
        // WComponent instance for each row.
        UIContext rowContext = getRowContext(rowData, i);
        // Check the context has not been used for another row
        Integer subId = ((SubUIContext) rowContext).getContextId();
        if (used.contains(subId)) {
            Object rowId = ((SubUIContext) rowContext).getRowId();
            String msg = "The row context for row id [" + rowId + "] has already been used for another row. " + "Either the row ID is not unique or the row bean has not implemented equals/hashcode " + "or no rowIdBeanProperty set on the repeater that uniquely identifies the row.";
            throw new SystemException(msg);
        }
        used.add(subId);
        UIContextHolder.pushContext(rowContext);
        try {
            prepareRow(request, i);
        } finally {
            UIContextHolder.popContext();
        }
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 48 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class AbstractWComponent method registerId.

/**
 * Helper method to be used by a {@link NamingContextable} to register a component.
 *
 * @param component the component to register
 */
void registerId(final WComponent component) {
    if (!WebUtilities.isActiveNamingContext(this)) {
        throw new SystemException("Can only register a component on an active NamingContext");
    }
    // Get registered id names
    ComponentModel model = getOrCreateComponentModel();
    Map<String, WComponent> ids = model.getContextIds();
    if (ids == null) {
        ids = new HashMap<>();
        model.setContextIds(ids);
    }
    // Get id name
    String idName = component.getIdName();
    // Check if already used
    WComponent mapped = ids.get(idName);
    // Not in map
    if (mapped == null) {
        // Save ID / Component
        ids.put(idName, component);
    } else if (mapped != component) {
        // Check is same component
        String contextName = getId();
        throw new SystemException("Duplicate ID. ID \"" + idName + "\" for " + component.getClass().getName() + " is already in use by " + mapped.getClass().getName() + " in context \"" + contextName + "\".");
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException)

Example 49 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class ContentEscape method escape.

/**
 * Writes the content to the response.
 *
 * @throws IOException if there is an error writing the content.
 */
@Override
public void escape() throws IOException {
    LOG.debug("...ContentEscape escape()");
    if (contentAccess == null) {
        LOG.warn("No content to output");
    } else {
        String mimeType = contentAccess.getMimeType();
        Response response = getResponse();
        response.setContentType(mimeType);
        if (isCacheable()) {
            getResponse().setHeader("Cache-Control", CacheType.CONTENT_CACHE.getSettings());
        } else {
            getResponse().setHeader("Cache-Control", CacheType.CONTENT_NO_CACHE.getSettings());
        }
        if (contentAccess.getDescription() != null) {
            String fileName = WebUtilities.encodeForContentDispositionHeader(contentAccess.getDescription());
            if (displayInline) {
                response.setHeader("Content-Disposition", "inline; filename=" + fileName);
            } else {
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            }
        }
        if (contentAccess instanceof ContentStreamAccess) {
            InputStream stream = null;
            try {
                stream = ((ContentStreamAccess) contentAccess).getStream();
                if (stream == null) {
                    throw new SystemException("ContentAccess returned null stream, access=" + contentAccess);
                }
                StreamUtil.copy(stream, response.getOutputStream());
            } finally {
                StreamUtil.safeClose(stream);
            }
        } else {
            byte[] bytes = contentAccess.getBytes();
            if (bytes == null) {
                throw new SystemException("ContentAccess returned null data, access=" + contentAccess);
            }
            response.getOutputStream().write(bytes);
        }
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) InputStream(java.io.InputStream)

Example 50 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class TestServlet method addIndirectJar.

/**
 * @param jarname the jar name to add
 */
public void addIndirectJar(final String jarname) {
    try (JarFile jarfile = new JarFile(jarname)) {
        Manifest man = jarfile.getManifest();
        Attributes atts = man.getMainAttributes();
        String jarlist = atts.getValue("Class-Path");
        StringTokenizer tokenizer = new StringTokenizer(jarlist, " ", false);
        while (tokenizer.hasMoreTokens()) {
            String tokenUrl = tokenizer.nextToken();
            union.add(Resource.newResource("jar:" + tokenUrl + "!/"));
            LOG.info("Added webdocs at " + tokenUrl);
        }
    } catch (IOException ex) {
        throw new SystemException("Could handle indirect jar " + jarname, ex);
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SystemException(com.github.bordertech.wcomponents.util.SystemException) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest)

Aggregations

SystemException (com.github.bordertech.wcomponents.util.SystemException)91 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)17 WComponent (com.github.bordertech.wcomponents.WComponent)15 UIContext (com.github.bordertech.wcomponents.UIContext)14 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 Test (org.junit.Test)9 WebElement (org.openqa.selenium.WebElement)8 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)6 IOException (java.io.IOException)5 Select (org.openqa.selenium.support.ui.Select)5 Environment (com.github.bordertech.wcomponents.Environment)4 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3 Date (java.util.Date)3 List (java.util.List)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)2 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)2 OptionGroup (com.github.bordertech.wcomponents.OptionGroup)2