1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.server.handlers.form;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import io.undertow.server.HttpServerExchange;
26 import io.undertow.util.AttachmentKey;
27
28 /**
29  * Factory class that can create a form data parser for a given request.
30  * <p>
31  * It does this by iterating the available parser definitions, and returning
32  * the first parser that is created.
33  *
34  * @author Stuart Douglas
35  */

36 public class FormParserFactory {
37
38     private static final AttachmentKey<FormDataParser> ATTACHMENT_KEY = AttachmentKey.create(FormDataParser.class);
39
40     private final ParserDefinition[] parserDefinitions;
41
42     FormParserFactory(final List<ParserDefinition> parserDefinitions) {
43         this.parserDefinitions = parserDefinitions.toArray(new ParserDefinition[parserDefinitions.size()]);
44     }
45
46     /**
47      * Creates a form data parser for this request. If a parser has already been created for the request the
48      * existing parser will be returned rather than creating a new one.
49      *
50      * @param exchange The exchange
51      * @return A form data parser, or null if there is no parser registered for the request content type
52      */

53     public FormDataParser createParser(final HttpServerExchange exchange) {
54         FormDataParser existing = exchange.getAttachment(ATTACHMENT_KEY);
55         if(existing != null) {
56             return existing;
57         }
58         for (int i = 0; i < parserDefinitions.length; ++i) {
59             FormDataParser parser = parserDefinitions[i].create(exchange);
60             if (parser != null) {
61                 exchange.putAttachment(ATTACHMENT_KEY, parser);
62                 return parser;
63             }
64         }
65         return null;
66     }
67
68     public interface ParserDefinition<T> {
69
70         FormDataParser create(final HttpServerExchange exchange);
71
72         T setDefaultEncoding(String charset);
73     }
74
75     public static Builder builder() {
76         return builder(true);
77     }
78
79     public static Builder builder(boolean includeDefault) {
80         Builder builder = new Builder();
81         if (includeDefault) {
82             builder.addParsers(new FormEncodedDataDefinition(), new MultiPartParserDefinition());
83         }
84         return builder;
85     }
86
87     public static class Builder {
88
89         private List<ParserDefinition> parsers = new ArrayList<>();
90
91         private String defaultCharset = null;
92
93         public Builder addParser(final ParserDefinition definition) {
94             parsers.add(definition);
95             return this;
96         }
97
98         public Builder addParsers(final ParserDefinition... definition) {
99             parsers.addAll(Arrays.asList(definition));
100             return this;
101         }
102
103         public Builder addParsers(final List<ParserDefinition> definition) {
104             parsers.addAll(definition);
105             return this;
106         }
107
108         public List<ParserDefinition> getParsers() {
109             return parsers;
110         }
111
112         public void setParsers(List<ParserDefinition> parsers) {
113             this.parsers = parsers;
114         }
115
116         /**
117          * A chainable version of {@link #setParsers}.
118          */

119         public Builder withParsers(List<ParserDefinition> parsers) {
120             setParsers(parsers);
121             return this;
122         }
123
124         public String getDefaultCharset() {
125             return defaultCharset;
126         }
127
128         public void setDefaultCharset(String defaultCharset) {
129             this.defaultCharset = defaultCharset;
130         }
131
132         /**
133          * A chainable version of {@link #setDefaultCharset}.
134          */

135         public Builder withDefaultCharset(String defaultCharset) {
136             setDefaultCharset(defaultCharset);
137             return this;
138         }
139
140         public FormParserFactory build() {
141             if(defaultCharset != null) {
142                 for (ParserDefinition parser : parsers) {
143                     parser.setDefaultEncoding(defaultCharset);
144                 }
145             }
146             return new FormParserFactory(parsers);
147         }
148
149     }
150
151 }
152