Skip to content

Commit

Permalink
WW-5080 Defines a new result type plain to use directly with Java code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Jun 29, 2020
1 parent dbd2f28 commit f02e0d2
Show file tree
Hide file tree
Showing 10 changed files with 555 additions and 0 deletions.
64 changes: 64 additions & 0 deletions core/src/main/java/org/apache/struts2/result/PlainResult.java
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 core/src/main/java/org/apache/struts2/result/plain/BodyWriter.java
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();
}
}
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;
}
}
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 core/src/main/java/org/apache/struts2/result/plain/HttpHeader.java
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();

}
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);
}

}
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;
}

}
Loading

0 comments on commit f02e0d2

Please sign in to comment.