Skip to content

Commit

Permalink
The configuration of Extended Services is not working when a path is …
Browse files Browse the repository at this point in the history
…included in the URL.

Closes:
- apache#2759
- apache#2670
- apache/incubator-kie-issues#387
  • Loading branch information
jomarko committed Nov 28, 2024
1 parent 0ca727d commit af6be7c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ExtendedServicesClient } from "./ExtendedServicesClient";
import { DependentFeature, ExtendedServicesContext } from "./ExtendedServicesContext";
import { ExtendedServicesModal } from "./ExtendedServicesModal";
import { ExtendedServicesStatus } from "./ExtendedServicesStatus";
import { useEnv } from "../env/hooks/EnvContext";

interface Props {
children: React.ReactNode;
Expand All @@ -38,20 +39,17 @@ export function ExtendedServicesContextProvider(props: Props) {
const [isModalOpen, setModalOpen] = useState(false);
const [installTriggeredBy, setInstallTriggeredBy] = useState<DependentFeature | undefined>(undefined);
const [outdated, setOutdated] = useState(false);
const { env } = useEnv();

const config = useMemo(() => {
try {
const url = new URL(settings.extendedServices.host);
const port = settings.extendedServices.port;
if (port) {
url.port = port;
}
const url = new URL(settings.extendedServices.href);

return new ExtendedServicesConfig(url.href, url.port);
return new ExtendedServicesConfig(url.href);
} catch (error) {
return new ExtendedServicesConfig(settings.extendedServices.host ?? "", settings.extendedServices.port ?? "");
return new ExtendedServicesConfig(env.KIE_SANDBOX_EXTENDED_SERVICES_URL);
}
}, [settings.extendedServices.port, settings.extendedServices.host]);
}, [settings.extendedServices.href]);

const bridge = useMemo(() => new ExtendedServicesBridge(config.url.ping), [config]);
const version = useMemo(() => process.env.WEBPACK_REPLACE__extendedServicesCompatibleVersion ?? "0.0.0", []);
Expand Down Expand Up @@ -117,15 +115,12 @@ export function ExtendedServicesContextProvider(props: Props) {
);
}
export class ExtendedServicesConfig {
constructor(
public readonly href: string,
public readonly port: string
) {}
constructor(public readonly href: string) {}

public get url() {
return {
jitExecutor: `${this.href}`,
ping: `${this.href.endsWith("/") ? this.href : this.href + ":" + this.port + "/"}ping`,
ping: `${this.href.endsWith("/") ? this.href : this.href + "/"}ping`,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function ExtendedServicesModal() {
<TextContent>
<Text component={TextVariants.p} className={"kogito--code"}>
/Applications/KIE\ Tooling\ Extended\ Services.app/Contents/MacOs/kogito -p{" "}
{extendedServices.config.port}
{extendedServices.config.href}
</Text>
</TextContent>
<br />
Expand All @@ -276,7 +276,7 @@ export function ExtendedServicesModal() {
i18n,
extendedServices.outdated,
extendedServices.status,
extendedServices.config.port,
extendedServices.config.href,
downloadExtendedServicesUrl,
KIE_SANDBOX_EXTENDED_SERVICES_MACOS_DMG,
KIE_SANDBOX_EXTENDED_SERVICES_MACOS_APP,
Expand Down Expand Up @@ -421,7 +421,7 @@ export function ExtendedServicesModal() {
<TextContent>
<Text component={TextVariants.p} className={"kogito--code"}>
&quot;kie-sandbox-extended-services_windows_{extendedServices.version}.exe&quot; -p{" "}
{extendedServices.config.port}
{new URL(extendedServices.config.href).port}
</Text>
</TextContent>
<br />
Expand All @@ -437,7 +437,7 @@ export function ExtendedServicesModal() {
extendedServices.outdated,
extendedServices.status,
extendedServices.version,
extendedServices.config.port,
extendedServices.config.href,
downloadExtendedServicesUrl,
KIE_SANDBOX_EXTENDED_SERVICES_WINDOWS_EXE,
]
Expand Down Expand Up @@ -576,7 +576,7 @@ export function ExtendedServicesModal() {
<br />
<TextContent>
<Text component={TextVariants.p} className={"kogito--code"}>
./kie-sandbox-extended-services -p {extendedServices.config.port}
./kie-sandbox-extended-services -p {new URL(extendedServices.config.href).port}
</Text>
</TextContent>
<br />
Expand All @@ -590,7 +590,7 @@ export function ExtendedServicesModal() {
i18n,
extendedServices.outdated,
extendedServices.status,
extendedServices.config.port,
extendedServices.config.href,
downloadExtendedServicesUrl,
KIE_SANDBOX_EXTENDED_SERVICES_LINUX_TAG_GZ,
KIE_SANDBOX_EXTENDED_SERVICES_BINARIES,
Expand Down Expand Up @@ -856,12 +856,14 @@ function ExtendedServicesPortForm() {
const settingsDispatch = useSettingsDispatch();
const { i18n } = useOnlineI18n();

const configPort = new URL(config.href).port;

return (
<>
<Text component={TextVariants.p}>
<I18nWrapped
components={{
port: <Text className={"kogito--code"}>{config.port}</Text>,
port: <Text className={"kogito--code"}>{configPort}</Text>,
}}
>
{i18n.dmnRunner.modal.wizard.advancedSettings.title}
Expand All @@ -873,18 +875,18 @@ function ExtendedServicesPortForm() {
fieldId={"extended-services-port"}
label={i18n.dmnRunner.modal.wizard.advancedSettings.label}
validated={
config.port === "" || parseInt(config.port, 10) < 0 || parseInt(config.port, 10) > 65353
? "error"
: "success"
configPort === "" || parseInt(configPort, 10) < 0 || parseInt(configPort, 10) > 65353 ? "error" : "success"
}
helperTextInvalid={i18n.dmnRunner.modal.wizard.advancedSettings.helperTextInvalid}
>
<TextInput
value={config.port}
value={configPort}
type={"number"}
onChange={(value) =>
settingsDispatch.set((settings) => {
settings.extendedServices.port = `${value}`;
const newConfigUrl = new URL(config.href);
newConfigUrl.port = value;
settings.extendedServices.href = newConfigUrl.href;
})
}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/online-editor/src/i18n/locales/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ export const de: OnlineI18n = {
advancedSettings: {
title: [
`Der Standard-Port von ${de_common.names.extendedServices} ist `,
wrapped("port"),
"21345",
`. Wenn Sie diesen Port bereits für eine andere Anwendung verwenden, können Sie den Port, der für die Verbindung mit ${de_common.names.extendedServices} verwendet wird, ändern.`,
],
label: " Port",
Expand Down
2 changes: 1 addition & 1 deletion packages/online-editor/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ export const en: OnlineI18n = {
advancedSettings: {
title: [
`The default ${en_common.names.extendedServices} port is `,
wrapped("port"),
"21345",
`. If you're already using this port for another application, you can change the port used to connect with the ${en_common.names.extendedServices}.`,
],
label: "Port",
Expand Down
42 changes: 10 additions & 32 deletions packages/online-editor/src/settings/ExtendedServicesSettingsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ export function ExtendedServicesSettingsTab() {
const { settings } = useSettings();
const settingsDispatch = useSettingsDispatch();
const extendedServices = useExtendedServices();
const [host, setHost] = useState(settings.extendedServices.host);
const [port, setPort] = useState(settings.extendedServices.port);
const [href, setHref] = useState(settings.extendedServices.href);

const onSubmit = useCallback(
(e: any) => {
e.preventDefault();
settingsDispatch.set((settings) => {
settings.extendedServices.host = host;
settings.extendedServices.port = port;
settings.extendedServices.href = href;
});
},
[host, port, settingsDispatch]
[href, settingsDispatch]
);

return (
Expand Down Expand Up @@ -75,38 +73,18 @@ export function ExtendedServicesSettingsTab() {
isRequired={true}
helperTextInvalid={""}
validated={"default"}
label={"Host"}
fieldId={"host-input"}
label={"Href"}
fieldId={"href-input"}
>
<InputGroup>
<TextInput
id="host-input"
name="host"
aria-describedby="host-text-input-helper"
id="href-input"
name="href"
aria-describedby="href-text-input-helper"
placeholder={""}
validated={"default"}
value={host}
onChange={setHost}
autoFocus={true}
/>
</InputGroup>
</FormGroup>
<FormGroup
isRequired={false}
helperTextInvalid={""}
validated={"default"}
label={"Port"}
fieldId={"port-input"}
>
<InputGroup>
<TextInput
id="port-input"
name="port"
aria-describedby="port-text-input-helper"
placeholder={""}
validated={"default"}
value={port}
onChange={setPort}
value={href}
onChange={setHref}
autoFocus={true}
/>
</InputGroup>
Expand Down
17 changes: 5 additions & 12 deletions packages/online-editor/src/settings/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,16 @@ import { useEnv } from "../env/hooks/EnvContext";
import { QueryParams } from "../navigation/Routes";
import { useQueryParams } from "../queryParams/QueryParamsContext";
import { SettingsTabs } from "./SettingsModalBody";
import { getCookie } from "../cookies";
import { useEditorEnvelopeLocator } from "../envelopeLocator/hooks/EditorEnvelopeLocatorContext";

export const EXTENDED_SERVICES_HOST_COOKIE_NAME = "kie-tools-COOKIE__kie-sandbox-extended-services--host";
export const EXTENDED_SERVICES_PORT_COOKIE_NAME = "kie-tools-COOKIE__kie-sandbox-extended-services--port";

export type SettingsContextType = {
settings: {
version: number;
corsProxy: {
url: string;
};
extendedServices: {
host: string;
port: string;
href: string;
};
editors: {
useLegacyDmnEditor: boolean;
Expand Down Expand Up @@ -144,19 +139,17 @@ export function SettingsContextProvider(props: PropsWithChildren<{}>) {

const envExtendedServicesUrl = new URL(env.KIE_SANDBOX_EXTENDED_SERVICES_URL);
// 0.0.0.0 is "equivalent" to localhost, but browsers don't like having mixed http/https urls with the exception of localhost
const envExtendedServicesHost = `${envExtendedServicesUrl.protocol}//${
envExtendedServicesUrl.hostname === "0.0.0.0" ? "localhost" : envExtendedServicesUrl.hostname
}`;
const envExtendedServicesPort = envExtendedServicesUrl.port;
if (envExtendedServicesUrl.hostname === "0.0.0.0") {
envExtendedServicesUrl.hostname = "localhost";
}

await persistSettings({
version: SETTINGS_FILE_LATEST_VERSION,
corsProxy: {
url: env.KIE_SANDBOX_CORS_PROXY_URL,
},
extendedServices: {
host: getCookie(EXTENDED_SERVICES_HOST_COOKIE_NAME) ?? envExtendedServicesHost,
port: getCookie(EXTENDED_SERVICES_PORT_COOKIE_NAME) ?? envExtendedServicesPort,
href: envExtendedServicesUrl.href,
},
editors: {
useLegacyDmnEditor: false,
Expand Down

0 comments on commit af6be7c

Please sign in to comment.