Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-85062 Replace language keys by their value in npm modules #64532

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,6 +14,7 @@ dependencies {
compileOnly project(":apps:static:osgi:osgi-util")
compileOnly project(":apps:static:portal-configuration:portal-configuration-metatype-api")
compileOnly project(":core:osgi-felix-util")
compileOnly project(":core:osgi-service-tracker-collections")
compileOnly project(":core:petra:petra-string")

testCompile group: "com.liferay.portal", name: "com.liferay.util.java", version: "default"
Expand Down
Expand Up @@ -14,21 +14,36 @@

package com.liferay.frontend.js.loader.modules.extender.internal.npm.builtin;

import com.liferay.frontend.js.loader.modules.extender.npm.JSBundle;
import com.liferay.frontend.js.loader.modules.extender.npm.JSPackage;
import com.liferay.osgi.service.tracker.collections.map.ServiceTrackerMap;
import com.liferay.osgi.service.tracker.collections.map.ServiceTrackerMapFactory;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.MimeTypes;
import com.liferay.portal.kernel.util.StreamUtil;
import com.liferay.portal.kernel.util.ResourceBundleLoader;
import com.liferay.portal.kernel.util.StringUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import java.net.URL;

import javax.servlet.ServletOutputStream;
import java.util.Locale;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

/**
* Provides a base abstract class to implement servlets that return JavaScript
* modules tracked by the {@link
Expand All @@ -38,16 +53,32 @@
*/
public abstract class BaseBuiltInJSModuleServlet extends HttpServlet {

@Override
public void destroy() {
_bundleSymbolicNameServiceTrackerMap.close();
}

@Override
public void init() {
Bundle bundle = FrameworkUtil.getBundle(getClass());

_bundleSymbolicNameServiceTrackerMap =
ServiceTrackerMapFactory.openSingleValueMap(
bundle.getBundleContext(), ResourceBundleLoader.class,
"bundle.symbolic.name");
}

protected abstract MimeTypes getMimeTypes();

/**
* Returns the requested resource. This is a template method that must be
* implemented by subclasses to lookup the requested resource.
* Returns the requested resource descriptor. This is a template method that
* must be implemented by subclasses to lookup the requested resource.
*
* @param pathInfo the request's pathInfo
* @return the {@link JSModule} object describing the module
* @return the {@link String} content of the resource or null
*/
protected abstract URL getURL(String pathInfo);
protected abstract ResourceDescriptor getResourceDescriptor(
String pathInfo);

@Override
protected void service(
Expand All @@ -56,38 +87,72 @@ protected void service(

String pathInfo = request.getPathInfo();

URL url = getURL(pathInfo);
ResourceDescriptor resourceDescriptor = getResourceDescriptor(pathInfo);

if (url == null) {
if (resourceDescriptor == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);

return;
}

_setContentType(response, url);
_setContentType(response, pathInfo);

String languageId = request.getParameter("languageId");

_sendResource(response, url);
Locale locale = LocaleUtil.fromLanguageId(languageId);

_sendResource(response, resourceDescriptor, locale);
}

private void _sendResource(HttpServletResponse response, URL url)
private void _sendResource(
HttpServletResponse response, ResourceDescriptor resourceDescriptor,
Locale locale)
throws IOException {

ServletOutputStream servletOutputStream = response.getOutputStream();
JSPackage jsPackage = resourceDescriptor.getJsPackage();

URL url = jsPackage.getResourceURL(resourceDescriptor.getPackagePath());

if (url == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);

return;
}

try (InputStream inputStream = url.openStream()) {
StreamUtil.transfer(inputStream, servletOutputStream, false);
String content = StringUtil.read(inputStream);

response.setCharacterEncoding(StringPool.UTF8);

PrintWriter printWriter = response.getWriter();

JSBundle jsBundle = jsPackage.getJSBundle();

ResourceBundleLoader resourceBundleLoader =
_bundleSymbolicNameServiceTrackerMap.getService(
jsBundle.getName());

if (resourceBundleLoader != null) {
content = LanguageUtil.process(
() -> resourceBundleLoader.loadResourceBundle(locale),
locale, content);
}

printWriter.print(content);
}
catch (Exception e) {
catch (IOException ioe) {
_log.error("Unable to read " + resourceDescriptor.toString(), ioe);

response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Unable to read file");
}
}

private void _setContentType(HttpServletResponse response, URL url) {
String file = url.getFile();
private void _setContentType(
HttpServletResponse response, String pathInfo) {

String extension = FileUtil.getExtension(file);
String extension = FileUtil.getExtension(pathInfo);

if (extension.equals(".js")) {
response.setContentType(ContentTypes.TEXT_JAVASCRIPT_UTF8);
Expand All @@ -98,8 +163,14 @@ else if (extension.equals(".map")) {
else {
MimeTypes mimeTypes = getMimeTypes();

response.setContentType(mimeTypes.getContentType(file));
response.setContentType(mimeTypes.getContentType(pathInfo));
}
}

private static final Log _log = LogFactoryUtil.getLog(
BaseBuiltInJSModuleServlet.class);

private ServiceTrackerMap<String, ResourceBundleLoader>
_bundleSymbolicNameServiceTrackerMap;

}
Expand Up @@ -20,8 +20,6 @@
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.util.MimeTypes;

import java.net.URL;

import javax.servlet.Servlet;

import org.osgi.service.component.annotations.Component;
Expand All @@ -47,7 +45,7 @@ protected MimeTypes getMimeTypes() {
}

@Override
protected URL getURL(String pathInfo) {
protected ResourceDescriptor getResourceDescriptor(String pathInfo) {
String identifier = pathInfo.substring(1);

int i = identifier.indexOf(StringPool.SLASH);
Expand All @@ -71,7 +69,7 @@ protected URL getURL(String pathInfo) {

String packagePath = ModuleNameUtil.getPackagePath(identifier);

return jsPackage.getResourceURL(packagePath);
return new ResourceDescriptor(jsPackage, packagePath);
}

private static final long serialVersionUID = -8753225208295935344L;
Expand Down
Expand Up @@ -19,8 +19,6 @@
import com.liferay.frontend.js.loader.modules.extender.npm.NPMRegistry;
import com.liferay.portal.kernel.util.MimeTypes;

import java.net.URL;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -50,7 +48,7 @@ protected MimeTypes getMimeTypes() {
}

@Override
protected URL getURL(String pathInfo) {
protected ResourceDescriptor getResourceDescriptor(String pathInfo) {
String identifier = pathInfo.substring(1);

String packageName = ModuleNameUtil.getPackageName(identifier);
Expand All @@ -63,7 +61,7 @@ protected URL getURL(String pathInfo) {

String packagePath = ModuleNameUtil.getPackagePath(identifier);

return jsPackage.getResourceURL(packagePath);
return new ResourceDescriptor(jsPackage, packagePath);
}

private JSPackage _getJSPackage(String packageName) {
Expand Down
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.frontend.js.loader.modules.extender.internal.npm.builtin;

import com.liferay.frontend.js.loader.modules.extender.npm.JSPackage;

/**
* @author Iván Zaera Avellón
*/
public class ResourceDescriptor {

public ResourceDescriptor(JSPackage jsPackage, String packagePath) {
_jsPackage = jsPackage;
_packagePath = packagePath;
}

public JSPackage getJsPackage() {
return _jsPackage;
}

public String getPackagePath() {
return _packagePath;
}

@Override
public String toString() {
return _jsPackage.getResolvedId() + _jsPackage;
}

private final JSPackage _jsPackage;
private final String _packagePath;

}