Skip to content

Commit

Permalink
Pull up method to interface and initialze file item headers for
Browse files Browse the repository at this point in the history
propagation
  • Loading branch information
garydgregory committed Jun 14, 2023
1 parent 814bb5a commit a700589
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public FileItemInputIterator getItemIterator(final RequestContext requestContext
*/
public FileItemHeaders getParsedHeaders(final String headerPart) {
final int len = headerPart.length();
final FileItemHeadersImpl headers = newFileItemHeaders();
final FileItemHeaders headers = newFileItemHeaders();
int start = 0;
for (;;) {
int end = parseEndOfLine(headerPart, start);
Expand Down Expand Up @@ -365,7 +365,7 @@ public long getSizeMax() {
*
* @return The new instance.
*/
protected FileItemHeadersImpl newFileItemHeaders() {
protected FileItemHeaders newFileItemHeaders() {
return FileItemBuilder.newFileItemHeaders();
}

Expand Down Expand Up @@ -396,7 +396,7 @@ private int parseEndOfLine(final String headerPart, final int end) {
* @param headers String with all headers.
* @param header Map where to store the current header.
*/
private void parseHeaderLine(final FileItemHeadersImpl headers, final String header) {
private void parseHeaderLine(final FileItemHeaders headers, final String header) {
final int colonOffset = header.indexOf(':');
if (colonOffset == -1) {
// This header line is malformed, skip it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface FileItemFactory {
*/
abstract class FileItemBuilder<T extends FileItem, B extends FileItemBuilder<T, B>> extends AbstractStreamBuilder<T, B> {

static FileItemHeadersImpl newFileItemHeaders() {
public static FileItemHeaders newFileItemHeaders() {
return new FileItemHeadersImpl();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
*/
public interface FileItemHeaders {

/**
* Adds a header.
*
* @param name name
* @param value value.
*/
void addHeader(String name, String value);

/**
* Gets the value of the specified part header as a {@code String}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public interface FileItemHeadersProvider {
FileItemHeaders getHeaders();

/**
* Sets the headers read from within an item. Implementations of {@link FileItem} or {@link FileItemInput} should implement this interface to be able
* to get the raw headers found within the item header block.
* Sets the headers read from within an item. Implementations of {@link FileItem} or {@link FileItemInput} should implement this interface to be able to get
* the raw headers found within the item header block.
*
* @param headers the instance that holds onto the headers for this instance.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.fileupload2.disk;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import org.apache.commons.fileupload2.FileItemHeaders;
import org.apache.commons.fileupload2.disk.DiskFileItem.Builder;
import org.apache.commons.fileupload2.FileItemFactory.FileItemBuilder;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link DiskFileItem}.
*/
public class DiskFileItemFactoryTest {

@Test
void testHeaders() {
final DiskFileItemFactory factory = DiskFileItemFactory.builder().get();
final Builder fileItemBuilder = factory.fileItemBuilder();
assertNotNull(fileItemBuilder.getFileItemHeaders());
final DiskFileItem fileItem = fileItemBuilder.get();
assertNotNull(fileItem.getHeaders(), "Missing default headers (empty)");
assertFalse(fileItem.getHeaders().getHeaderNames().hasNext());
final FileItemHeaders fileItemHeaders = FileItemBuilder.newFileItemHeaders();
assertNotNull(fileItemHeaders);
fileItem.setHeaders(fileItemHeaders);
assertSame(fileItemHeaders, fileItem.getHeaders());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.fileupload2.disk;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import org.apache.commons.fileupload2.FileItemFactory.FileItemBuilder;
import org.apache.commons.fileupload2.FileItemHeaders;
import org.apache.commons.fileupload2.disk.DiskFileItem.Builder;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link DiskFileItem}.
*/
public class DiskFileItemTest {

@Test
void testBuilderHeaders() {
final Builder builder = DiskFileItem.builder();
assertNotNull(builder.getFileItemHeaders());
final DiskFileItem fileItem = builder.get();
assertNotNull(fileItem.getHeaders(), "Missing default headers (empty)");
assertFalse(fileItem.getHeaders().getHeaderNames().hasNext());
assertNotNull(fileItem.getHeaders());
final FileItemHeaders fileItemHeaders = FileItemBuilder.newFileItemHeaders();
assertNotNull(fileItemHeaders);
fileItem.setHeaders(fileItemHeaders);
assertSame(fileItemHeaders, fileItem.getHeaders());
}

}

0 comments on commit a700589

Please sign in to comment.