Install a plugin to Liferay's editor - liferay

I have a Liferay DXP installation and I would like to install a plugin to the editor. The plugin is base64image.
I was following this official guide so I created a class generally like this:
#Component(immediate = true, service = DynamicInclude.class)
public class CKEditorBase64ImageDynamicInclude implements DynamicInclude {
private BundleContext bundleContext;
#Override
public void include(HttpServletRequest request, HttpServletResponse response, String key) throws IOException {
Bundle bundle = bundleContext.getBundle();
URL entryURL = bundle.getEntry("/META-INF/resources/html/editors/ckeditor/extension/base64_images.js");
StreamUtil.transfer(entryURL.openStream(), response.getOutputStream());
}
#Override
public void register(DynamicIncludeRegistry dynamicIncludeRegistry) {
dynamicIncludeRegistry.register("com.liferay.frontend.editors.web#ckeditor#onEditorCreate");
}
#Activate
protected void activate(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
}
It should include the base64_images.js file where it initializes the editor. But it never works, regardless what the content of the file is. What is wrong with that?
I would like to add that the plugin files (JavaScript code) are part of my Liferay theme. I wanted base64_images.js to call its API but it also might not be the correct way how to do it.

Related

How to insert code in the static files like .html, .htm, .asp on IIS for non-asp project

I want to add a script on my IIS Server.
So that it will be applied on all the websites that are upload will have that script in their request response.
Anyone who knows how to do it?
I had implemented the IHttpModule and IHttpHandler, it works fine for the asp.net projects.
but if the website contains only html, css, and js files in the folder, this solution doesn't work.
Here the HttpModule and HttpHandler
public class MyCustomHttpModuleClass : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += OnPostRequestHandlerExecute;
}
public void OnPostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
context.Response.Write("<h1>alert('HELLO')</h1>");
}
}
public class MyHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<h1>alert('HELLO')</h1>");
}
}
I'm not sure if you have learnt how to add Custom Module and Handler in IIS. After tested your module and handler with static website, it works fine.
I will just give you a sample of adding them to IIS.
1.Create a project "class library .netframework". I name the project"ClassLibrary1"
2.Add class "MyCustomHttpModuleClass" and "MyHandler" to the project
3.Build this solution and find "ClassLibrary1.dll" in the "project/bin/debug" folder.
4.Copy "ClassLibrary1.dll" to the website root "BIN" folder.
5.Add managed module and handler by choose your dll.(should in the list after you copied)Just mention that your custom handler only work on the file extension you set up.
Now they work.

Register ModelListener<JournalArticle> in Liferay 7

I'm trying to implement a ModelListener in Liferay 7. When I'm updating a JournalArticle in the UI, I would like to add a small behaviour and some logging.
#Component(immediate = true, service = ModelListener.class)
public class DownloadsListener extends BaseModelListener<JournalArticle> {
#Reference
private StructureService structureService;
#Reference
private DLFileEntryLocalServiceUtil fileEntryLocalService;
private static final Log LOG = LogFactoryUtil.getLog(DownloadsListener.class);
#Override
public void onAfterUpdate(JournalArticle model) throws ModelListenerException {
LOG.error("UPDATES!");
LOG.error("UPDATES!");
super.onAfterUpdate(model);
if(structureService.isNieuwsArticle(model)) {
}
getFileFromURL("/documents/82177/0/file.pdf/0d10338c-8ca1-c5b7-cc4b-011bef1ee759");
}
private DLFileEntry getFileFromURL(String url) {
String[] splittedURL = StringUtil.split(url, '/');
return null;
}
}
But this code is never triggered when I'm updating an article. I'm not hitting my debug point in the onAfterUpdate method, and I'm not seeing any logging. The OSGI Module is deployed correctly. What am I missing?
The issue was that my ModelListener was not being correctly deployed in the OSGi container. (Although Eclipse / Apache Gogo told everything was fine).
OSGi could not resolve:
#Reference
private DLFileEntryLocalServiceUtil fileEntryLocalService;
Changed it to
#Reference
private DLFileEntryLocalService fileEntryLocalService;
Now the behaviour is correctly. It's a shame that there is ZERO feedback by Apache GoGo.

Orchard CMS: Create OnPublished event on existing content type

I have a content type called Article. We created the part inside the CMS admin console, so I do not have a corresponding ArticlePart and ArticlePartRecord in the module. Now I need to run an operation when an article is published. I'm having a hard time finding out where to intercept the publishing of an item. I would normally do this in a Handler, but I don't know how to create a handler in this scenario (not having the part and part record objects).
I think you can just override the Published method, like this:
protected override void Published(PublishContentContext context) {
if (context.ContentType == "Article") {
// do something
}
}
Just for those who are new, create a class and add below code.
Make sure to inherit from ContentHandlerBase class.
also add services.AddScoped<IContentHandler, MyCustomPublishHandler>(); to startup class.
And Done!
public class MyCustomPublishHandler : ContentHandlerBase
{
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly ILogger _logger;
public MyCustomPublishHandler(
IContentDefinitionManager contentDefinitionManager,
ILogger<ContentPartHandlerCoordinator> logger)
{
_contentDefinitionManager = contentDefinitionManager;
_logger = logger;
}
public override async Task PublishedAsync(PublishContentContext context)
{
// do something
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentItem.ContentType);
_logger.LogInformation($"published {contentTypeDefinition.DisplayName}");
}
}

Using RazorEngine with TextWriter

I want to use RazorEngine to generate some html files. It's easy to generate strings first, then write them to files. But if the generated strings are too large, that will cause memory issues.
So I wonder is there a non-cached way to use RazorEngine, like using StreamWriter as its output rather than a string.
I google this for a while, but with no luck.
I think use a custom base template should be the right way, but the documents are so few(even out of date) on the offcial homepage of RazorEngine.
Any hint will be helpful!
OK. I figured it out.
Create a class that inherits TemplateBase<T>, and take a TextWrite parameter in the constructor.
public class TextWriterTemplate<T> : TemplateBase<T>
{
private readonly TextWriter _tw;
public TextWriterTemplate(TextWriter tw)
{
_tw = tw;
}
// override Write and WriteLiteral methods, write text using the TextWriter.
public override void Write(object value)
{
_tw.Write(value);
}
public override void WriteLiteral(string literal)
{
_tw.Write(literal);
}
}
Then use the template as this:
private static void Main(string[] args)
{
using (var sw = new StreamWriter(#"output.txt"))
{
var config = new FluentTemplateServiceConfiguration(c =>
c.WithBaseTemplateType(typeof(TextWriterTemplate<>))
.ActivateUsing(context => (ITemplate)Activator.CreateInstance(context.TemplateType, sw))
);
using (var service = new TemplateService(config))
{
service.Parse("Hello #Model.Name", new {Name = "Waku"}, null, null);
}
}
}
The content of output.txt should be Hello WAKU.

Displaying an html page in form (error connecting to stream)

I putted an .html page in a src folder of project in order to display this page on runtime.
But I get an error on runtime that say:- Error connecting to stream.
import javax.microedition.midlet.*;
public class HtmlMidlet extends MIDlet {
public void startApp()
{
com.sun.lwuit.Display.init(this);
final com.sun.lwuit.Form form = new com.sun.lwuit.Form("");
final com.sun.lwuit.html.HTMLComponent htmlC = new com.sun.lwuit.html.HTMLComponent( );
htmlC.setRTL(true);
htmlC.setPage("jar://src/ahlam.html");
form.addComponent(htmlC);
form.setScrollable(true);
form.show( );
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
}
I think that the way that you are using to open your file is not the proper way. Is this html page in your folder? Where is your page? the root directory is /src from your project, why are you using jar:... try /ahlam.html only
Take a look on this page for more info and examples HTML Component

Resources