JSF 1.2 response capture including resources - jsf

I am using JSF 1.2 with RichFaces. Is it possible to mimic the "Save as" of a web browser client? To be more precise, I want to add a "Download zip" on a page. The purpose is to generate a zip archive containing the HTML generated page with all necessary files to open that page with a correct layout.
I succeeded in the past to capture the HTML page by means of a HttpResponseWrapper.
But as soon as the page contains RichFaces components, some JS and images are required for a correct display. How could I get those resources to add them in the zip?

Use a HTML parser like Jsoup. It can easily find CSS/JS/image elements by jQuery-like CSS selectors and give you the resource URLs back so that you can download them individually.
InputStream input = new URL(url).openStream();
// ... Save webpage itself.
Document document = Jsoup.parse(savedWebPage, "UTF-8", url);
for (Element stylesheet : document.select("link[rel=stylesheet]")) {
InputStream input = new URL(stylesheet.absUrl("href")).openStream();
// ... Save individual stylesheet file.
}
for (Element script : document.select("script[src]")) {
InputStream input = new URL(script.absUrl("src")).openStream();
// ... Save individual script file.
}
for (Element img : document.select("img[src]")) {
InputStream input = new URL(img.absUrl("src")).openStream();
// ... Save individual image file.
}
CSS background images are not taken into account. You could consider a CSS parser like SAC on every individual CSS file.

Related

Can't import a file into a page using the import toolkit

I'm working through an import using the Kentico Import Toolkit for Kentico 12.
My intention is to import content from a CSV file into pages in Kentico. It's fundamentally working, however, I can't get it to take a PDF on the file system and bring that in as a binary object into a Kentico page property.
In my CSV file I have a column called "FileName", and in Kentico I have a page type called "Asset" which has a property called "File" which is of type File.
In my column mapping I have the "File" property mapped as follows:
#<file>c:\temp\filesfortesting\{%FileName%}
The import runs and the pages are created however no files are actually imported and mapped to the File property on the page.
Any suggestions as to how I can fix this? Do I have the mapping right?
I guess you want to import is as a page attachment. In this case, you need to import the pages first and then, in a second run import the page attachments.It might be easier to create a simple tool and use API to do the migration.
At first I will clarify the types of attachments in Kentico:
Unsorted attachments: these are the attachments added through e.g.
WYSIWYG editor and are available on page's Properties -> Attachments
tab
Grouped attachments: these are the files attached to pages using the
Attachments form control. You can change the order of the attachments
and you manage them on the Form tab
CMS.File and direct file field attachments: these are the files
uploaded to a page on the Form tab using the Direct uploader form
control.
So, in the import toolkit, you will select the page attachments to be imported and then configure the source and import the attachments into the CMS_Attachment table.
The import toolkit supports importing of the unsorted attachments only. So, if you want to move the attachments you may need to do it in multiple runs + execute some API code.
When importing the attachments, you need to set the WHERE condition to match your pages, in my case I was using /news/% path:
Where condition:
AttachmentDocumentID IN (SELECT DocumentID FROM CMS_Document WHERE DocumentNamePath LIKE '/news/%')
Then, select some dummy page where to import all the attachments. remember? KIT imports attachments as unsorted. In this case I used the parent /news page with DocumentID = 1063 so all attachments are assigned to this page.
Then, execute code similar to this to move the attachments to respective pages:
// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// Gets the parent page
TreeNode page = tree.SelectNodes()
.Path("/news")
.OnCurrentSite()
.Culture("en-us")
.TopN(1)
.FirstOrDefault();
if (page != null)
{
foreach (DocumentAttachment attachment in page.AllAttachments)
{
// Perform any action with the attachment object (DocumentAttachment), NewsTeaser is the target page’s direct file upload field where I want to get the attachment
TreeNode targetPage = tree.SelectNodes()
.Type("CMS.News")
.Where("NewsTeaser = '" + attachment.AttachmentGUID + "'")
.TopN(1)
.Culture("en-us")
.OnCurrentSite()
.FirstOrDefault();
if (targetPage != null)
{
// Edits the attachment's metadata (name, title and description) and set the right AttachmentDocumentID and AttachmentIsUnsorted to false
attachment.AttachmentDocumentID = targetPage.DocumentID;
attachment.AttachmentIsUnsorted = false;
// Ensures that the attachment can be updated without supplying its binary data
attachment.AllowPartialUpdate = true;
// Saves the modified attachment into the database
attachment.Update();
}
}
}

Chrome extension: add HTML into web page

I've seen this answer:
Chrome extension: Insert fixed div as UI
which inserts a div into the current web page.
However, it gets pretty tiresome to be doing this all the time:
"div.id = 'myDivId';" +
"div.style.position = 'fixed';" +
"div.style.top = '50%';" +
"div.style.left = '50%';" +
Is it possible to insert a fully-formed HTML template instead (i.e. that you include as template.html in the extension)?
There are several methods.
Expose resources to content script via web_accessible_resources
Then the content script can use them almost directly (by using chrome.runtime.getURL) in images, videos, iframes, or read the contents of html files using fetch() and XMLHttpRequest.
If your UI is complex then go with the iframe approach - its contents will be a full-fledged extension environment like a browser_action popup with its own chrome://extension URL.
The downside is that your resources may be read by any site directly and the sites may easily detect the presence of your extension.
Use messaging and read the resources via the background script
content.js:
chrome.runtime.sendMessage({cmd: 'getTemplate'}, html => {
document.body.insertAdjacentHTML('beforeend', html);
});
background.js:
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.cmd === 'getTemplate') {
fetch('/templates/foo.html').then(r => r.text()).then(sendResponse);
return true;
}
});
Use you compiler/bundler
There are plugins for major bundlers/compilers that allow to inline the file contents in another one so you can include a HTML template in your compiled content script as a literal string.

Unable to parse webpage with frameset

I'm trying to parse Groovydoc, but Jsoup doesn't find the frameset in which everything is contained.
Connection connection=Jsoup.connect('http://groovy-lang.org/api.html')
Document document=connection.get()
Elements element= document.getElementsByTag('frameset')
element.each {println(it)}
If you check the result that is returned by connection.get() you can see that there is no frameset tag:
println document
Now, if you open the site in a browser and use development tools to look at it's html code you can see that the frameset you are looking for is a child of an iframe from source http://docs.groovy-lang.org/latest/html/gapi.
Just load the iframe url with Jsoup to get the frameset
Connection connection = Jsoup.connect('http://docs.groovy-lang.org/latest/html/gapi')
Document document = connection.get()
Elements element = document.getElementsByTag('frameset')
element.each { println it }
Or if you do not want to hardcode the iframe source url to parse, look at this SO answer on how to get the source url

j2me Lwuit form images from RSS feeds are not being loaded correctly

I have a LWUIT form in my j2me based application. I am getting my data through RSS Feeds(including image path). Now i want to show the images through RSS feeds image path in my LWUIT Form. I am using HTMLComponent for that purpose. The html based text is loading fine but the images are not being shown. Following is my code snippet.
HTMLComponent htmlComp = new HTMLComponent();
htmlComp.setPreferredSize(new Dimension(300,300));
String imagePath=myItem.getImagePath().trim();
htmlComp.setShowImages(true);
String myHtml="<b>Hello</b> <br/> <img src= \""+imagePath+"\" /> ";
System.out.println(myHtml);
htmlComp.setHTML(myHtml,null,null,false);
addComponent(htmlComp);
The image path is like "http://toffeetv.com/wp-content/uploads/2012/06/f_swan-150x150.jpg".
Why the images are not being shown? Can anybody help me with an appropriate code example?
The HTML Component accepts a base URL as one of its arguments to that method. Just set the URL to the appropriate path and it should work as expected.

chrome extension inject in frameset code

i develop some extension for google grome
i inject at document_end event my js (+jquery)
i set in manifest allFrames: true
my match url has frameset
my goal get element by id inside one of frame
i do
//wait for load my frame
$("frame[name='header']).load(function() {
//here I need get element by id inside this frame
});
how to do this properly?
PS: my frame is from the same domain
You dont need to do document load, I assume your doing this from a content script. Just place the "run_at" manifest to be document_end and within your content script you check if the current URL is that frame page, then you will be in that Domain.
Something like this:
if(location.href == 'http://someweb.com/path/to/page.html') {
var foo = document.getElementById('foo')
Something like that will get you started.

Resources