Ejemplo n.º 1
0
  public static void wrapWithRequestElement(ContentHandlerHelper helper, DebugXML debugXML) {
    helper.startDocument();

    final ExternalContext externalContext = NetUtils.getExternalContext();
    final ExternalContext.Request request =
        (externalContext != null) ? externalContext.getRequest() : null;
    helper.startElement(
        "request",
        new String[] {
          "request-uri",
          (request != null) ? request.getRequestURI() : null,
          "query-string",
          (request != null) ? request.getQueryString() : null,
          "method",
          (request != null) ? request.getMethod() : null
        });

    debugXML.toXML(helper);

    helper.endElement();

    helper.endDocument();
  }
  /**
   * Constructor for top-level document.
   *
   * @param externalContext external context to obtain request path, properties, etc.
   * @param contentHandler resulting static state document
   */
  public XFormsExtractorContentHandler(
      ExternalContext externalContext, ContentHandler contentHandler) {
    super(contentHandler);
    this.externalContext = externalContext;
    this.ignoreRootElement = false;

    final ExternalContext.Request request = externalContext.getRequest();

    // Remember if filter provided separate deployment information
    isSeparateDeployment =
        "separate"
            .equals(
                request.getAttributesMap().get(OrbeonXFormsFilter.OPS_XFORMS_RENDERER_DEPLOYMENT));

    // Try to get request context path
    {
      // First try context path passed by the filter
      requestContextPath =
          (String)
              request
                  .getAttributesMap()
                  .get(OrbeonXFormsFilter.OPS_XFORMS_RENDERER_REQUEST_CONTEXT_PATH);

      // Otherwise just use the request's context path
      if (requestContextPath == null) requestContextPath = request.getContextPath();
    }

    // Create xml:base stack
    try {
      final String rootXMLBase;
      {
        // It is possible to override the base URI by setting a request attribute. This is used by
        // OPSXFormsFilter.
        final String rendererBaseURI =
            (String)
                request
                    .getAttributesMap()
                    .get(OrbeonXFormsFilter.OPS_XFORMS_RENDERER_BASE_URI_ATTRIBUTE_NAME);
        if (rendererBaseURI != null) rootXMLBase = rendererBaseURI;
        else rootXMLBase = request.getRequestPath();
      }
      xmlBaseStack.push(new URI(null, null, rootXMLBase, null));
    } catch (URISyntaxException e) {
      throw new ValidationException(e, new LocationData(locator));
    }
  }
 public ServletInputStreamRepeater(ExternalContext.Request request) throws IOException {
   this.contentType = request.getContentType();
   this.contentLength = request.getContentLength();
   this.characterEncoding = request.getCharacterEncoding();
   this.inputStream = request.getInputStream();
 }
Ejemplo n.º 4
0
  /**
   * Utility method to decode a multipart/fomr-data stream and return a Map of parameters of type
   * Object[], each of which can be a String or FileData.
   */
  public static Map<String, Object[]> getParameterMapMultipart(
      PipelineContext pipelineContext,
      final ExternalContext.Request request,
      String headerEncoding) {

    final Map<String, Object[]> uploadParameterMap = new HashMap<String, Object[]>();
    try {
      // Setup commons upload

      // Read properties
      // NOTE: We use properties scoped in the Request generator for historical reasons. Not too
      // good.
      int maxSize = RequestGenerator.getMaxSizeProperty();
      int maxMemorySize = RequestGenerator.getMaxMemorySizeProperty();

      final DiskFileItemFactory diskFileItemFactory =
          new DiskFileItemFactory(maxMemorySize, SystemUtils.getTemporaryDirectory());

      final ServletFileUpload upload =
          new ServletFileUpload(diskFileItemFactory) {
            protected FileItem createItem(Map headers, boolean isFormField)
                throws FileUploadException {
              if (isFormField) {
                // Handle externalized values
                final String externalizeFormValuesPrefix =
                    org.orbeon.oxf.properties.Properties.instance()
                        .getPropertySet()
                        .getString(ServletExternalContext.EXTERNALIZE_FORM_VALUES_PREFIX_PROPERTY);
                final String fieldName = getFieldName(headers);
                if (externalizeFormValuesPrefix != null
                    && fieldName.startsWith(externalizeFormValuesPrefix)) {
                  // In this case, we do as if the value content is an uploaded file so that it can
                  // be externalized
                  return super.createItem(headers, false);
                } else {
                  // Just create the FileItem using the default way
                  return super.createItem(headers, isFormField);
                }
              } else {
                // Just create the FileItem using the default way
                return super.createItem(headers, isFormField);
              }
            }
          };
      upload.setHeaderEncoding(headerEncoding);
      upload.setSizeMax(maxSize);

      // Add a listener to destroy file items when the pipeline context is destroyed
      pipelineContext.addContextListener(
          new PipelineContext.ContextListenerAdapter() {
            public void contextDestroyed(boolean success) {
              for (final String name : uploadParameterMap.keySet()) {
                final Object values[] = uploadParameterMap.get(name);
                for (final Object currentValue : values) {
                  if (currentValue instanceof FileItem) {
                    final FileItem fileItem = (FileItem) currentValue;
                    fileItem.delete();
                  }
                }
              }
            }
          });

      // Wrap and implement just the required methods for the upload code
      final InputStream inputStream;
      try {
        inputStream = request.getInputStream();
      } catch (IOException e) {
        throw new OXFException(e);
      }

      final RequestContext requestContext =
          new RequestContext() {

            public int getContentLength() {
              return request.getContentLength();
            }

            public InputStream getInputStream() {
              // NOTE: The upload code does not actually check that it doesn't read more than the
              // content-length
              // sent by the client! Maybe here would be a good place to put an interceptor and make
              // sure we
              // don't read too much.
              return new InputStream() {
                public int read() throws IOException {
                  return inputStream.read();
                }
              };
            }

            public String getContentType() {
              return request.getContentType();
            }

            public String getCharacterEncoding() {
              return request.getCharacterEncoding();
            }
          };

      // Parse the request and add file information
      try {
        for (Object o : upload.parseRequest(requestContext)) {
          final FileItem fileItem = (FileItem) o;
          // Add value to existing values if any
          if (fileItem.isFormField()) {
            // Simple form field
            // Assume that form fields are in UTF-8. Can they have another encoding? If so, how is
            // it specified?
            StringUtils.addValueToObjectArrayMap(
                uploadParameterMap,
                fileItem.getFieldName(),
                fileItem.getString(STANDARD_PARAMETER_ENCODING));
          } else {
            // File
            StringUtils.addValueToObjectArrayMap(
                uploadParameterMap, fileItem.getFieldName(), fileItem);
          }
        }
      } catch (FileUploadBase.SizeLimitExceededException e) {
        // Should we do something smart so we can use the Presentation
        // Server error page anyway? Right now, this is going to fail
        // miserably with an error.
        throw e;
      } catch (UnsupportedEncodingException e) {
        // Should not happen
        throw new OXFException(e);
      } finally {
        // Close the input stream; if we don't nobody does, and if this stream is
        // associated with a temporary file, that file may resist deletion
        if (inputStream != null) {
          try {
            inputStream.close();
          } catch (IOException e) {
            throw new OXFException(e);
          }
        }
      }

      return uploadParameterMap;
    } catch (FileUploadException e) {
      throw new OXFException(e);
    }
  }