I tried to get screenshot in my allure reports with Appium on IOS real device and I'm not getting it, what I did is :
in my mainRunner :
#Listeners({ utilities.AllureListeners.class })
in my Allurelisteners :
public class AllureListeners implements ITestListener {
IOSBaseTest iosBaseTest = new IOSBaseTest();
public void onTestStart(ITestContext iTestContext) {
System.out.println("I am on start method "+ iTestContext.getName());
iTestContext.setAttribute("Driver", iosBaseTest.driver);
}
#Override
public void onTestSuccess(ITestResult result) {
System.out.println("I am on start method "+ getTestMethodName(result));
}
#Override
public void onTestFailure(ITestResult result) {
System.out.println("I am on testFailure method " + getTestMethodName(result)+" failed");
//Object testClass = result.getInstance();
//ITestContext context = result.getTestContext();
//WebDriver driver = (WebDriver) context.getAttribute("driver");
IOSDriver<MobileElement> driver = iosBaseTest.getdriver();
if (driver instanceof WebDriver) {
System.out.println("Screenshot captured for test case: "+getTestMethodName(result));
saveFailureScreenShot(driver);
}
saveTextLog(getTestMethodName(result) + "failed and screenshot taken!");
}
#Override
public void onTestSkipped(ITestResult result) {
System.out.println("I am on test skipped method "+getTestMethodName(result)+" skipped");
}
#Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
}
#Override
public void onTestFailedWithTimeout(ITestResult result) {
}
#Override
public void onStart(ITestContext context) {
}
#Override
public void onFinish(ITestContext context) {
}
private static String getTestMethodName(ITestResult iTestResult) {
return iTestResult.getMethod().getConstructorOrMethod().getName();
}
// Screenshot attachments for allure
#Attachment (value ="page screenshot", type = "image/png")
public byte[] saveFailureScreenShot(WebDriver driver) {
return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
}
// Text attachments for allure
#Attachment(value= "{0}", type = "text/plain")
public static String saveTextLog(String message) {
return message;
}
//HTML attachments for allure
#Attachment(value= "{0}", type = "text/html")
public static String attachHtml(String html) {
return html;
}
}
in my Base test I have method:
public IOSDriver<MobileElement> getdriver(){
return driver;
}
my xml is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="utilities.AllureListeners" />
</listeners>
<test name="tester">
<groups>
<run>
<include name="add favorite contact" />
</run>
</groups>
<classes>
<class name="tests.IOSMainRunner">
</class>
</classes>
</test>
</suite>
and yet my report looking without a screenshot:
try this.
public void allureSaveDeviceScreenshot() {
Allure.addAttachment("screenshot", new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
}
Related
I am populating a firebase data with firebase recycler adapter through databinding and want to populate images with the data either from Picasso or Glide, but unable to do. So can any one help me what to write and where to write the code of either Picasso or Glide.
public class ShopByCategoryFragment extends Fragment {
FirebaseRecyclerAdapter adapter;
Firebase mFirebaseRef = new Firebase("https://abc.firebaseio.com/").child("subCategories");
public ShopByCategoryFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.recycler_view, container, false);
final RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
adapter = new FirebaseRecyclerAdapter<SubCategories, ViewHolder>(SubCategories.class, R.layout.fragment_shop_by_category,
ViewHolder.class, mFirebaseRef) {
#Override
protected void populateViewHolder(ViewHolder viewHolder, SubCategories subCategories, int i) {
FragmentShopByCategoryBinding binding = viewHolder.getBinding();
binding.setSubCategories(subCategories);
}
};
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
return rootView;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public FragmentShopByCategoryBinding binding;
public ViewHolder(View itemView) {
super(itemView);
binding = DataBindingUtil.bind(itemView);
}
public FragmentShopByCategoryBinding getBinding() {
return binding;
}
}
#Override
public void onDestroy() {
super.onDestroy();
adapter.cleanup();
}
}
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="SubCategories"
type="com.abc.www.shopping.model.SubCategories"/>
</data>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="#dimen/tile_height"
android:id="#+id/sub_category_image"
android:scaleType="centerCrop"
android:src="#{SubCategories.image}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="#dimen/tile_height"
android:text="#{SubCategories.title}"
android:id="#+id/sub_category_title"
android:padding="16dp"
android:textColor="#color/colorSubCategoryTitle"
android:layout_gravity="center"
android:textAppearance="#android:style/TextAppearance.Material.Title"
tools:targetApi="lollipop" />
</FrameLayout>
</android.support.v7.widget.CardView>
public class SubCategories extends BaseObservable {
private String title;
private String image;
public SubCategories() {
// empty constructor
}
public SubCategories(String title, String image) {
this.title = title;
this.image = image;
}
public String getTitle() {
return title;
}
public String getImage() {
return image;
}
}
I have made something similar to what you need. I'll share my code and do a little explanation.
I have a class that returns the FirebaseRecyclerAdapter like this:
public RecyclerView.Adapter postAdapter(String userKey, final Context context, MyClickListener clickListener) {
Query userPostRef = postRef.child(userKey);
myClickListener = clickListener;
return new FirebaseRecyclerAdapter<PostModel, DataObjectHolder>(PostModel.class, R.layout.stream_layout, DataObjectHolder.class, userPostRef) {
#Override
public void populateViewHolder(final DataObjectHolder dataviewHolder, final PostModel postModel, int position) {
StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();
mStorageRef.child("uploaded_captures/" + postModel.getImageFilename() + ".jpg").getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
dataviewHolder.setImage(uri.toString(), context);
dataviewHolder.setComment(postModel.getPostComment());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("FirebaseRecyclerAdapter", exception.getMessage());
}
});
}
};
}
and a ViewHolder that loads the image into the ImageView used by the layout:
private static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View mView;
public DataObjectHolder(View itemView) {
super(itemView);
mView = itemView;
}
void setImage(final String imageURL, final Context context) {
final ImageView postImage = (ImageView) mView.findViewById(R.id.imageView);
if (BuildConfig.DEBUG) {
Picasso.with(context).setIndicatorsEnabled(true);
}
Picasso.with(context)
.load(imageURL)
.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.drawable.ic_menu_camera)
.fit().centerCrop().into(postImage, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(context)
.load(imageURL)
.placeholder(R.drawable.ic_menu_camera)
.fit().centerCrop().into(postImage);
}
});
}
void setComment(String text) {
TextView comment = (TextView) mView.findViewById(R.id.comment);
comment.setText(text);
}
#Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
On Picasso I'm using the NerworkPolicy.OFFLINE to check if the image is on the disk cache before trying to get it from the network. It's not perfect and I'm still having some issues with the delay of the images once the ViewHolder it's been recycled but I think it's a start for you to go on.
In my ViewHolder I do this by:
Glide.with(context)
.load(chat.getImageUrl())
.into(imageView);
I haven't tried using data binding for this yet, but the code is simple enough.
I am trying to fake Enterprise Library LogWriter so it doesn't perform any logging when I run unit tests, so it looks like this in simplified version:
public class MockLogger : ILogger
{
public MockLogger()
{
}
public void Write(object message)
{
}
public void Write(object message, string category)
{
}
public void Write(object message, string category, int eventId)
{
}
public void Write(object message, string category, int eventId, int priority)
{
}
public void Write(object message, string category, int eventId, int priority, TraceEventType severity)
{
}
}
public class Logger : ILogger
{
LogWriter writer;
public Logger()
{
writer = (new LogWriterFactory()).Create();
}
public Logger(LogWriter logWriter)
{
writer = logWriter;
}
public void Write(LogEntry logEntry)
{
writer.Write(logEntry);
}
public void Write(object message)
{
writer.Write(message);
}
public void Write(object message, string category)
{
writer.Write(message, category);
}
public void Write(object message, string category, int eventId)
{
writer.Write(message, category, eventId);
}
public void Write(object message, string category, int eventId, int priority)
{
writer.Write(message, category, eventId, priority);
}
public void Write(object message, string category, int eventId, int priority, TraceEventType severity)
{
writer.Write(message, category, priority, eventId, severity);
}
public bool ShouldLog(LogEntry logEntry)
{
return writer.ShouldLog(logEntry);
}
}
Then i am trying to resolve it thru configuration:
<unity xmlns="http://schemas.microsoft.com/practices/2012/unity">
<alias alias="ILogger" type="ConsoleApplicationTest.ILogger, ConsoleApplicationTest" />
<namespace name="ConsoleApplicationTest" />
<assembly name="ConsoleApplicationTest" />
<container>
<register type="ConsoleApplicationTest.ILogger, ConsoleApplicationTest" name="logger" mapTo="ConsoleApplicationTest.Logger, ConsoleApplicationTest">
</register>
</container>
<container name="Mock">
<register type="ConsoleApplicationTest.ILogger, ConsoleApplicationTest" name="mockLogger" mapTo="ConsoleApplicationTest.MockLogger, ConsoleApplicationTest">
</register>
</container>
var container = new UnityContainer();
container.LoadConfiguration();
var logger = container.Resolve<ILogger>();
The error I am getting is the following:
Resolution of the dependency failed, type =
"ConsoleApplicationTest.ILogger", name = "(none)". Exception occurred
while: while resolving. Exception is: InvalidOperationException - The
type ILogger does not have an accessible constructor.
Both classes have a parameterless constructor. What should I do to resolve this error? Using .NET 4.5
Because you have registered your types with the name="logger" and name="mockLogger" you have created named registrations so you need to pass in the name when calling Resolve:
var container = new UnityContainer();
container.LoadConfiguration();
var logger = container.Resolve<ILogger>("logger"); // or "mockLogger"
I am trying to marshal/unmarshal a Color to XML. The JAXB project had example code for doing this exact thing via a XmlJavaTypeAdapter https://jaxb.java.net/guide/XML_layout_and_in_memory_data_layout.html.
The marshaling works fine and the output is what I expect:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beanWithColor>
<foreground>#ff0000ff</foreground>
<name>CleverName</name>
</beanWithColor>
However, when trying to go from XML to object the unmarshal method is never called. Can anyone provide insight as to why? After it is unmarshalled foreground color is null and I have confirmed with my debugger that unmarshal is never being called:
BeanWithColor{foreground=null, name='CleverName'}
SCCE:
#XmlRootElement
public class BeanWithColor {
private String name;
private Color foreground;
public BeanWithColor() {
}
public BeanWithColor(Color foreground, String name) {
this.foreground = foreground;
this.name = name;
}
#XmlJavaTypeAdapter(ColorAdapter.class)
public Color getForeground() {
return this.foreground;
}
public void setForeground(Color foreground) {
this.foreground = foreground;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("BeanWithColor{");
sb.append("foreground=").append(foreground);
sb.append(", name='").append(name).append('\'');
sb.append('}');
return sb.toString();
}
public static void main(String[] args) {
BeanWithColor bean = new BeanWithColor(Color.blue, "CleverName");
try {
StringWriter writer = new StringWriter(1000);
JAXBContext context = JAXBContext.newInstance(BeanWithColor.class);
final Marshaller marshaller = context.createMarshaller();
marshaller.marshal(bean, writer);
System.out.println("Marshaled XML: " + writer);
final Unmarshaller unmarshaller = context.createUnmarshaller();
BeanWithColor beanWithColor = (BeanWithColor) unmarshaller.unmarshal(new StringReader(writer.toString()));
System.out.println("beanWithColor = " + beanWithColor);
} catch (JAXBException e) {
e.printStackTrace();
}
}
static class ColorAdapter extends XmlAdapter<String, Color> {
public Color unmarshal(String s) {
return Color.decode(s);
}
public String marshal(Color color) {
return '#' + Integer.toHexString(color.getRGB());
}
}
}
I suspect that XmlAdapter is being called for unmarshal but that the Color.decode method is failing (this is what happens when I debugged your code).
Color.decode("#ff0000ff");
results in the following:
Exception in thread "main" java.lang.NumberFormatException: For input string: "ff0000ff"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
at java.lang.Integer.valueOf(Integer.java:528)
at java.lang.Integer.decode(Integer.java:958)
at java.awt.Color.decode(Color.java:707)
You can set a ValidationEventHandler on the Unmarshaller to get a hook on all failures. By default a JAXB impl wouldn't report that problem.
The Fix
The ColorAdapter.marshal method needs to be fixed to return the correct value.
String rgb = Integer.toHexString(color.getRGB());
return "#" + rgb.substring(2, rgb.length());
I have a pretty complex problem about struts2 chaining actions, thanks in advance for your patience reading my problem. I will try my best to describe it clearly.
Below is my struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.devMode" value="false" />
<package name="default" extends="struts-default" namespace="/">
<action name="test" class="com.bv.test.TestAction1" >
<result name="success" type="chain">y</result>
</action>
<action name="x">
<result name="success">/index.jsp</result>
</action>
<action name="y" class="com.bv.test.TestAction2">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
My logic is like this:
When accessing to /myapp/test, TestAction1 will handle the request;
In TestAction1, I "include" action x (my 2nd action in my config) like this:
ResponseImpl myResponse = new ResponseImpl(response);
RequestDispatcher rd = request.getRequestDispatcher("/x.action");
rd.include(request, myResponse);
And the important thing is I am using a customized ResponseIml when including "x.action".
After including, I return "success", so the result chains to action y (3rd action in my config);
And at last, TestAction2 continue to handle the request, it will go to success result, and the jsp should be rendered, but what I see is a blank page.
The jsp file is very simple:
index.jsp
<h1>Test!</h1>
My question/puzzle is:
In TestAction1, if I get the response from ServletActionContext, I
am getting different ones before and after including; before
including is the default response, but after including I got an
instance of my customized ResponseImpl; I expect to get the same
one: i.e.: the default response;
In TestAction2, I get response from ServletActionContext, what I got
is the instance of my customized ResponseIml. This is my most important thing, I think I should get a default response instance here, i.e.: org.apache.catalina.connector.Response, I am running on JBoss;
I am getting a different ActionContext in TestAction2 (compared with
the ActionContext I get in TestAction1).
This problem really drive me on the nuts, I have spent days on it.
Any advice will be appreciated!
Thanks a million!!
My Code:
TestAction1:
public class TestAction1 {
public String execute() {
ActionContext ac = ActionContext.getContext();
System.out.println("Before including: the action context is : " + ac);
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
System.out.println("Before including: the response is : " + response);
ResponseImpl myResponse = new ResponseImpl(response);
RequestDispatcher rd = request.getRequestDispatcher("/x.action");
try {
rd.include(request, myResponse);
String s = myResponse.getOutput();
System.out.println("get from response: " + s);
}
catch (Exception e) {
e.printStackTrace();
}
ac = ActionContext.getContext();
System.out.println("After including : the action context is : " + ac);
response = ServletActionContext.getResponse();
System.out.println("After including : the response is : " + response);
return "success";
}
}
ResponseImpl:
import java.util.Locale;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.Cookie;
import javax.servlet.jsp.JspWriter;
/**
*
*
*/
public class ResponseImpl extends HttpServletResponseWrapper {
//=========================================================
// Private fields.
//=========================================================
private ServletOutputStream outputStream = null;
private ByteArrayOutputStream byteArrayOutputStream = null;
private StringWriter stringWriter = null;
private PrintWriter printWriter = null;
private HttpServletResponse _response = null;
private String contentType= "text/html";
private String encoding = "UTF-8";
/**
*
*/
class ServletOutputStream extends javax.servlet.ServletOutputStream {
private OutputStream outputStream = null;
/**
*
*/
ServletOutputStream(ByteArrayOutputStream outputStream) {
super();
this.outputStream = outputStream;
}
/**
*
*/
public void write(int b) throws IOException {
this.outputStream.write(b);
}
}
//=========================================================
// Public constructors and methods.
//=========================================================
/**
*
*/
public ResponseImpl(HttpServletResponse response) {
super(response);
this._response = response;
}
/**
*
*/
public String getOutput() {
if (this.stringWriter != null) {
return this.stringWriter.toString();
}
if (this.byteArrayOutputStream != null) {
try {
return this.byteArrayOutputStream.toString(this.encoding);
}
catch (UnsupportedEncodingException e) {
}
return this.byteArrayOutputStream.toString();
}
return null;
}
//=========================================================
// Implements HttpServletResponse interface.
//=========================================================
public void addCookie(Cookie cookie) {
}
public void addDateHeader(String name, long date) {
}
public void addHeader(String name, String value) {
}
public void addIntHeader(String name, int value) {
}
public boolean containsHeader(String name) {
return false;
}
public String encodeRedirectURL(String url) {
if (null != this._response) {
url = this._response.encodeRedirectURL(url);
}
return url;
}
public String encodeURL(String url) {
if (null != this._response) {
url = this._response.encodeURL(url);
}
return url;
}
public void sendError(int sc) {
}
public void sendError(int sc, String msg) {
}
public void sendRedirect(String location) {
}
public void setDateHeader(String name, long date) {
}
public void setHeader(String name, String value) {
}
public void setIntHeader(String name, int value) {
}
public void setStatus(int sc) {
}
public void resetBuffer() {
}
//=========================================================
// Implements deprecated HttpServletResponse methods.
//=========================================================
public void setStatus(int sc, String sm) {
}
//=========================================================
// Implements deprecated HttpServletResponse methods.
//=========================================================
public String encodeRedirectUrl(String url) {
return encodeRedirectURL(url);
}
public String encodeUrl(String url) {
return encodeURL(url);
}
//=========================================================
// Implements ServletResponse interface.
//=========================================================
public void flushBuffer() {
}
public int getBufferSize() {
return 0;
}
public String getCharacterEncoding() {
return this.encoding;
}
public String getContentType() {
return this.contentType;
}
public Locale getLocale() {
return null;
}
public javax.servlet.ServletOutputStream getOutputStream() {
if (this.outputStream == null) {
this.byteArrayOutputStream = new ByteArrayOutputStream();
this.outputStream =
new ServletOutputStream(this.byteArrayOutputStream);
}
return this.outputStream;
}
public PrintWriter getWriter() {
if (this.printWriter == null) {
this.stringWriter = new StringWriter();
this.printWriter = new PrintWriter(this.stringWriter);
}
return this.printWriter;
}
public boolean isCommitted() {
return true;
}
public void reset() {
}
public void setBufferSize(int size) {
}
public void setCharacterEncoding(String charset) {
}
public void setContentLength(int len) {
}
public void setContentType(String type) {
int needle = type.indexOf(";");
if (-1 == needle) {
this.contentType = type;
}
else {
this.contentType = type.substring(0, needle);
String pattern = "charset=";
int index = type.indexOf(pattern, needle);
if (-1 != index) {
this.encoding = type.substring(index + pattern.length());
}
}
}
public void setLocale(Locale locale) {
}
}
TestAction2:
public class TestAction2 {
public String execute() {
ActionContext ac = ActionContext.getContext();
System.out.println("In TestAction 2 : the action context is : " + ac);
HttpServletResponse response = ServletActionContext.getResponse();
System.out.println("In TestAction 2 : the response is : " + response);
return "success";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2 Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
This is my debug info.
Before including: the action context is
:com.opensymphony.xwork2.ActionContext#c639ce
Before including: the response is :
org.apache.catalina.connector.ResponseFacade#8b677f
get from response: <h1>Test!</h1>
After including : the action context is :
com.opensymphony.xwork2.ActionContext#2445d7
After including : the response is : com.bv.test.ResponseImpl#165547d
In TestAction 2 : the action context is
:com.opensymphony.xwork2.ActionContext#19478c7
In TestAction 2 : the response is : com.bv.test.ResponseImpl#165547d
So, I have different ActionContext instances before and after the including!!
When you do rd.include another request is fired internally inside the web server. So from struts point of view it sees a completely new request and a new action context is created as a result. (that's why you need to include 'INCLUDE' thing on the struts2 filter.. so that it's seeing included requests as well). Probably since thread local variables are used to track action context and all that when you do ActionContext.getContext() the context related to the new request (related to the include) gets retrieved.
Did you try resetting the response to the initial one in a finally block like below
try {
rd.include(request, myResponse);
String s = myResponse.getOutput();
System.out.println("get from response: " + s);
}
catch (Exception e) {
e.printStackTrace();
} finally {
ServletActionContext.setResponse(response);
}
If this resolves the response issue.. you could probably store the string 's' as a variable in the action context and retrieve it inside your Action2
You could also try the following as well. Instead of using chaining.. inside your TestAction1 include the TestAction2 with the original response. return 'none' from the action as the return value.
public class TestAction1 {
public String execute() {
ActionContext ac = ActionContext.getContext();
System.out.println("Before including: the action context is : " + ac);
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
System.out.println("Before including: the response is : " + response);
ResponseImpl myResponse = new ResponseImpl(response);
RequestDispatcher rd = request.getRequestDispatcher("/x.action");
try {
rd.include(request, myResponse);
String s = myResponse.getOutput();
System.out.println("get from response: " + s);
}
catch (Exception e) {
e.printStackTrace();
}
RequestDispatcher rd = request.getRequestDispatcher("/y.action");
try {
rd.include(request, response);
}
catch (Exception e) {
e.printStackTrace();
} finally {
return "none";
}
}
}
I have a custom container component, that I want to use like this:
<p:a>A
<p:a>B</p:a>
</p:a>
That should generate this markup:
<div>A
<div>B</div>
</div>
Code for the component is below.
public class TagA extends TagHandler {
Logger logger = Logger.getLogger(getClass().getName());
public TagA(TagConfig config) {
super(config);
}
public void apply(FaceletContext ctx, UIComponent parent)
throws IOException {
UIComponentBase c = new UIComponentBase() {
public void encodeBegin(FacesContext ctx) throws IOException {
//getParent() always returns UIViewRot
logger.info("Parent is: " + getParent().getClass().getName());
ResponseWriter w = ctx.getResponseWriter();
w.write("<div>");
super.encodeBegin(ctx);
}
public void encodeEnd(FacesContext ctx) throws IOException {
ResponseWriter w = ctx.getResponseWriter();
w.write("</div>");
super.encodeEnd(ctx);
}
// abstract method in base, must override
public String getFamily() {
return "com.mobiarch.nf";
}
};
parent.getChildren().add(c);
nextHandler.apply(ctx, parent);
}
}
Unfortunately, this is rendering the following markup:
<div></div>A
<div></div>B
For others in a similar situation, just develop the component and not the tag.
#FacesComponent("my.ComponentA")
public class ComponentA extends UIComponentBase {
Logger logger = Logger.getLogger(getClass().getName());
public String getFamily() {
return "my.custom.component";
}
public void encodeBegin(FacesContext ctx) throws IOException {
super.encodeBegin(ctx);
logger.info("Component parent is: " + getParent().getClass().getName());
ResponseWriter w = ctx.getResponseWriter();
w.write("<div>");
}
public void encodeEnd(FacesContext ctx) throws IOException {
super.encodeEnd(ctx);
ResponseWriter w = ctx.getResponseWriter();
w.write("</div>");
}
}
Register it in your ??.taglib.xml
<tag>
<tag-name>a</tag-name>
<component>
<component-type>my.ComponentA</component-type>
</component>
</tag>