Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support BigInteger as return type for method Invocation #334
  • Loading branch information
nevenr committed Jul 5, 2018
1 parent 8f0704b commit 24123bf
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
@@ -0,0 +1,42 @@
package org.jolokia.converter.json.simplifier;

import java.math.BigInteger;

/*
* Copyright 2009-2018 Roland Huss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/**
* Simplifier for BigIntegers which result in a map with a single key <code>bigint</code>
*
* @author Neven Radovanović
* @since June 27, 2018
*/
public class BigIntegerSimplifier extends SimplifierExtractor<BigInteger> {

/**
* No arg constructor as required for simplifiers
*/
public BigIntegerSimplifier() {
super(BigInteger.class);
addExtractor("bigint", new BigIntegerAttributeExtractor());
}

private static class BigIntegerAttributeExtractor implements AttributeExtractor<BigInteger> {
/** {@inheritDoc} */
public Object extract(BigInteger pBigInt) { return pBigInt.toString(); }
}
}
1 change: 1 addition & 0 deletions agent/core/src/main/resources/META-INF/simplifiers-default
Expand Up @@ -3,3 +3,4 @@ org.jolokia.converter.json.simplifier.FileSimplifier
org.jolokia.converter.json.simplifier.DomElementSimplifier
org.jolokia.converter.json.simplifier.UrlSimplifier
org.jolokia.converter.json.simplifier.ObjectNameSimplifier
org.jolokia.converter.json.simplifier.BigIntegerSimplifier
@@ -0,0 +1,90 @@
package org.jolokia.converter.json;

import mockit.Mock;
import mockit.MockUp;
import org.jolokia.converter.json.simplifier.BigIntegerSimplifier;
import org.jolokia.converter.json.simplifier.UrlSimplifier;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.management.AttributeNotFoundException;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Stack;

import static org.testng.Assert.assertEquals;

/**
* @author Neven Radovanović
* @since June 27, 2018
*/
@Test()
public class SimplifiersTest {

private BigIntegerSimplifier bigIntegerSimplifier;
private UrlSimplifier urlSimplifier;
private ObjectToJsonConverter converter;

@BeforeMethod
public void setup() {
bigIntegerSimplifier = new BigIntegerSimplifier();
urlSimplifier = new UrlSimplifier();

new MockUp<ObjectToJsonConverter>() {
@Mock
public ValueFaultHandler getValueFaultHandler() {
return new PathAttributeFilterValueFaultHandler(ValueFaultHandler.THROWING_VALUE_FAULT_HANDLER);
}
};
// Needed for subclassing final object
converter = new ObjectToJsonConverter(null, null);
converter.setupContext();
}

@Test
public void bigIntegerSimplifier() throws AttributeNotFoundException {
BigInteger bigInt = new BigInteger("12345678901234567890");
Object result = bigIntegerSimplifier.extractObject(converter, bigInt, new Stack<String>(), false);
assertEquals(result, bigInt);
}

@Test
public void bigIntegerSimplifierJson() throws AttributeNotFoundException {
BigInteger bigInt = new BigInteger("12345678901234567890");
Object result = bigIntegerSimplifier.extractObject(converter, bigInt, new Stack<String>(), true);
assertEquals(result.toString(), "{\"bigint\":\"12345678901234567890\"}");
}

@Test
public void urlSimplifier() throws AttributeNotFoundException, MalformedURLException {
URL url = new URL("https://www.jolokia.org");
Object result = urlSimplifier.extractObject(converter, url, new Stack<String>(), false);
assertEquals(result, url);
}

@Test
public void urlSimplifierJson() throws AttributeNotFoundException, MalformedURLException {
URL url = new URL("https://www.jolokia.org");
Object result = urlSimplifier.extractObject(converter, url, new Stack<String>(), true);
assertEquals(result.toString(), "{\"url\":\"https:\\/\\/www.jolokia.org\"}");
}


private class PathAttributeFilterValueFaultHandler implements ValueFaultHandler {
private final ValueFaultHandler origHandler;

PathAttributeFilterValueFaultHandler(ValueFaultHandler pOrigHandler) {
origHandler = pOrigHandler;
}

public <T extends Throwable> Object handleException(T exception) throws T {
if (exception instanceof AttributeNotFoundException) {
throw new AttributeFilteredException(exception.getMessage());
} else {
return origHandler.handleException(exception);
}
}
}

}
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -462,7 +462,7 @@
<url>https://github.com/mjr5749</url>
</contributor>
<contributor>
<name>Neven Radovanović</name>
<name>Neven Radovanović</name>
<url>https://github.com/nevenr</url>
</contributor>
<contributor>
Expand Down
5 changes: 5 additions & 0 deletions src/changes/changes.xml
Expand Up @@ -23,6 +23,11 @@
<author email="roland@jolokia.org">Roland Huß</author>
</properties>
<body>
<release version="1.6.1-SNAPSHOT" description="Release 1.6.1-SNAPSHOT" date="yyyy-mm-dd">
<action dev="nevenr" type="add" issue="334">
Add BigInteger simplifier. BigInteger becomes a JSON object with the key "bigint" containing the big integer value as String.
</action>
</release>
<release version="1.6.0" description="Release 1.6.0" date="2018-06-25">
<action dev="rhuss" type="update">
Enable authentication with role "jolokia" for the war agent by default.
Expand Down
5 changes: 5 additions & 0 deletions src/docbkx/protocol.xml
Expand Up @@ -712,6 +712,11 @@
<literal>name</literal>, <literal>value</literal> and
<literal>hasChildNodes</literal>.
</listitem>
<listitem>
<classname>java.math.BigInteger</classname> becomes a JSON
object with the key <literal>bigint</literal> containing
the big integer value as String.
</listitem>
</itemizedlist>
</para>
<para>
Expand Down

0 comments on commit 24123bf

Please sign in to comment.