Skip to content

Commit

Permalink
fix(jmx): provide full URL including origin for Jolokia URL in Attrib…
Browse files Browse the repository at this point in the history
…ute modal
  • Loading branch information
tadayosi committed Oct 24, 2023
1 parent 2982ac9 commit 13373ed
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class MockJolokiaService implements IJolokiaService {
return 0
}

async getFullJolokiaUrl(): Promise<string> {
return ''
}

async list(options: ListRequestOptions): Promise<unknown> {
return jmxCamelResponse
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AttributeService {
}

async buildUrl(mbean: string, attribute: string): Promise<string> {
const jolokiaUrl = await jolokiaService.getJolokiaUrl()
const jolokiaUrl = await jolokiaService.getFullJolokiaUrl()
return `${jolokiaUrl}/read/${escapeMBean(mbean)}/${attribute}`
}
}
Expand Down
20 changes: 20 additions & 0 deletions packages/hawtio/src/plugins/shared/jolokia-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { userService } from '@hawtiosrc/auth'
import Jolokia, { ListRequestOptions } from 'jolokia.js'
import { DEFAULT_MAX_COLLECTION_SIZE, DEFAULT_MAX_DEPTH, JolokiaListMethod, jolokiaService } from './jolokia-service'
import { hawtio } from '@hawtiosrc/core'

describe('JolokiaService', () => {
beforeEach(() => {
Expand Down Expand Up @@ -64,6 +65,25 @@ describe('JolokiaService', () => {
await expect(jolokiaService.getListMethod()).resolves.toEqual(JolokiaListMethod.DEFAULT)
})

test('getFullJolokiaUrl', async () => {
hawtio.getBasePath = jest.fn(() => '/hawtio')

jolokiaService.getJolokiaUrl = jest.fn(async () => '/hawtio/jolokia')
await expect(jolokiaService.getFullJolokiaUrl()).resolves.toEqual('http://localhost/hawtio/jolokia')

jolokiaService.getJolokiaUrl = jest.fn(async () => 'jolokia')
await expect(jolokiaService.getFullJolokiaUrl()).resolves.toEqual('http://localhost/hawtio/jolokia')

jolokiaService.getJolokiaUrl = jest.fn(async () => 'http://test:12345/test/jolokia')
await expect(jolokiaService.getFullJolokiaUrl()).resolves.toEqual('http://test:12345/test/jolokia')

jolokiaService.getJolokiaUrl = jest.fn(async () => 'https://test:12345/test/jolokia')
await expect(jolokiaService.getFullJolokiaUrl()).resolves.toEqual('https://test:12345/test/jolokia')

jolokiaService.getJolokiaUrl = jest.fn(async () => 'http/jolokia')
await expect(jolokiaService.getFullJolokiaUrl()).resolves.toEqual('http://localhost/hawtio/http/jolokia')
})

test('load and save Jolokia options', () => {
let options = jolokiaService.loadJolokiaStoredOptions()
expect(options.maxDepth).toEqual(DEFAULT_MAX_DEPTH)
Expand Down
37 changes: 36 additions & 1 deletion packages/hawtio/src/plugins/shared/jolokia-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { userService } from '@hawtiosrc/auth'
import { eventService } from '@hawtiosrc/core'
import { eventService, hawtio } from '@hawtiosrc/core'
import { basicAuthHeaderValue, getCookie } from '@hawtiosrc/util/https'
import {
escapeMBeanPath,
Expand Down Expand Up @@ -85,6 +85,7 @@ export interface IJolokiaService {
getJolokiaUrl(): Promise<string | null>
getJolokia(): Promise<Jolokia>
getListMethod(): Promise<JolokiaListMethod>
getFullJolokiaUrl(): Promise<string>
list(options: ListRequestOptions): Promise<unknown>
readAttributes(mbean: string): Promise<AttributeValues>
readAttribute(mbean: string, attribute: string): Promise<unknown>
Expand Down Expand Up @@ -118,6 +119,15 @@ class JolokiaService implements IJolokiaService {
}
}

/**
* Get the Jolokia URL that the service is connected to.
*
* The URL may not be a full URL including origin (`http(s)://host:port`).
* It can be a path relative to the root (`/hawtio/jolokia`) or to the current
* path (`jolokia`).
*
* @see Use {@link getFullJolokiaUrl} for getting the full URL.
*/
getJolokiaUrl(): Promise<string | null> {
if (this.jolokiaUrl) {
return this.jolokiaUrl
Expand Down Expand Up @@ -365,6 +375,31 @@ class JolokiaService implements IJolokiaService {
return opts
}

/**
* Get the full Jolokia URL that the service is connected to.
*
* The origin part (`http(s)://host:port`) is resolved based on `window.location`.
*
* @see {@link getJolokiaUrl}
*/
async getFullJolokiaUrl(): Promise<string> {
const jolokiaUrl = (await this.getJolokiaUrl()) ?? ''
if (jolokiaUrl.match(/^https?:\/\//)) {
return jolokiaUrl
}

const { origin } = window.location
if (jolokiaUrl.startsWith('/')) {
return `${origin}${jolokiaUrl}`
}

let basePath = hawtio.getBasePath() ?? ''
if (!basePath.endsWith('/')) {
basePath += '/'
}
return `${origin}${basePath + jolokiaUrl}`
}

async getListMethod(): Promise<JolokiaListMethod> {
// Need to wait for Jolokia instance as it might update the list method
await this.getJolokia()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { hawtio } from '@hawtiosrc/core'
import { jolokiaService } from '@hawtiosrc/plugins/shared/jolokia-service'
import { escapeMBean } from '@hawtiosrc/util/jolokia'
import { log } from '../globals'
Expand All @@ -11,11 +10,8 @@ class OperationService {

async getJolokiaUrl(mbean: string, operation: string): Promise<string> {
const mbeanName = escapeMBean(mbean)
const basePath = hawtio.getBasePath() ?? ''
const origin = window.location.origin
const jolokiaUrl = (await jolokiaService.getJolokiaUrl()) ?? ''
const jolokiaPath = jolokiaUrl.startsWith('/') ? jolokiaUrl : basePath + jolokiaUrl
return `${origin}${jolokiaPath}/exec/${mbeanName}/${operation}`
const jolokiaUrl = await jolokiaService.getFullJolokiaUrl()
return `${jolokiaUrl}/exec/${mbeanName}/${operation}`
}
}

Expand Down

0 comments on commit 13373ed

Please sign in to comment.