forked from apache/struts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WW-5080 Defines a new result type plain to use directly with Java code
- Loading branch information
1 parent
dbd2f28
commit f02e0d2
Showing
10 changed files
with
555 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
core/src/main/java/org/apache/struts2/result/PlainResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.struts2.result; | ||
|
||
import com.opensymphony.xwork2.ActionInvocation; | ||
import com.opensymphony.xwork2.Result; | ||
import org.apache.struts2.StrutsException; | ||
import org.apache.struts2.result.plain.HttpHeader; | ||
import org.apache.struts2.result.plain.ResponseBuilder; | ||
|
||
import javax.servlet.http.Cookie; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
public interface PlainResult extends Result { | ||
|
||
@Override | ||
default void execute(ActionInvocation invocation) throws Exception { | ||
ResponseBuilder builder = new ResponseBuilder(); | ||
write(builder); | ||
|
||
HttpServletResponse response = invocation.getInvocationContext().getServletResponse(); | ||
|
||
if (response.isCommitted()) { | ||
throw new StrutsException("Http response already committed, cannot modify it!"); | ||
} | ||
|
||
for (HttpHeader<String> header : builder.getStringHeaders()) { | ||
response.addHeader(header.getName(), header.getValue()); | ||
} | ||
for (HttpHeader<Long> header : builder.getDateHeaders()) { | ||
response.addDateHeader(header.getName(), header.getValue()); | ||
} | ||
for (HttpHeader<Integer> header : builder.getIntHeaders()) { | ||
response.addIntHeader(header.getName(), header.getValue()); | ||
} | ||
|
||
for (Cookie cookie : builder.getCookies()) { | ||
response.addCookie(cookie); | ||
} | ||
|
||
response.getWriter().write(builder.getBody()); | ||
response.flushBuffer(); | ||
} | ||
|
||
void write(ResponseBuilder response); | ||
|
||
} | ||
|
41 changes: 41 additions & 0 deletions
41
core/src/main/java/org/apache/struts2/result/plain/BodyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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.struts2.result.plain; | ||
|
||
import java.io.StringWriter; | ||
|
||
class BodyWriter { | ||
|
||
private final StringWriter body = new StringWriter(); | ||
|
||
public BodyWriter write(String out) { | ||
body.write(out); | ||
return this; | ||
} | ||
|
||
public BodyWriter writeLine(String out) { | ||
body.write(out); | ||
body.write("\n"); | ||
return this; | ||
} | ||
|
||
public String getBody() { | ||
return body.toString(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
core/src/main/java/org/apache/struts2/result/plain/DateHttpHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.struts2.result.plain; | ||
|
||
class DateHttpHeader implements HttpHeader<Long> { | ||
|
||
private final String name; | ||
private final Long value; | ||
|
||
public DateHttpHeader(String name, Long value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Long getValue() { | ||
return value; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
core/src/main/java/org/apache/struts2/result/plain/HttpCookies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.struts2.result.plain; | ||
|
||
import javax.servlet.http.Cookie; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
class HttpCookies { | ||
|
||
private final List<Cookie> cookies = new ArrayList<>(); | ||
|
||
public HttpCookies add(String name, String value) { | ||
cookies.add(new Cookie(name, value)); | ||
return this; | ||
} | ||
|
||
public List<Cookie> getCookies() { | ||
return Collections.unmodifiableList(cookies); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
core/src/main/java/org/apache/struts2/result/plain/HttpHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.struts2.result.plain; | ||
|
||
public interface HttpHeader<T> { | ||
|
||
String getName(); | ||
|
||
T getValue(); | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
core/src/main/java/org/apache/struts2/result/plain/HttpHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.struts2.result.plain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
class HttpHeaders { | ||
|
||
private final List<HttpHeader<String>> stringHeaders = new ArrayList<>(); | ||
private final List<HttpHeader<Long>> dateHeaders = new ArrayList<>(); | ||
private final List<HttpHeader<Integer>> intHeaders = new ArrayList<>(); | ||
|
||
public HttpHeaders add(String name, String value) { | ||
stringHeaders.add(new StringHttpHeader(name, value)); | ||
return this; | ||
} | ||
|
||
public HttpHeaders add(String name, Long value) { | ||
dateHeaders.add(new DateHttpHeader(name, value)); | ||
return this; | ||
} | ||
|
||
public HttpHeaders add(String name, Integer value) { | ||
intHeaders.add(new IntHttpHeader(name, value)); | ||
return this; | ||
} | ||
|
||
public List<HttpHeader<String>> getStringHeaders() { | ||
return Collections.unmodifiableList(stringHeaders); | ||
} | ||
|
||
public List<HttpHeader<Long>> getDateHeaders() { | ||
return Collections.unmodifiableList(dateHeaders); | ||
} | ||
|
||
public List<HttpHeader<Integer>> getIntHeaders() { | ||
return Collections.unmodifiableList(intHeaders); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
core/src/main/java/org/apache/struts2/result/plain/IntHttpHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.struts2.result.plain; | ||
|
||
class IntHttpHeader implements HttpHeader<Integer> { | ||
|
||
private final String name; | ||
private final Integer value; | ||
|
||
public IntHttpHeader(String name, Integer value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Integer getValue() { | ||
return value; | ||
} | ||
|
||
} |
Oops, something went wrong.