Can't access TrustedRequestFactory with ContainerManager
Asked by netsparker:

I am trying to make a rest call to "/rest/api/latest/info" bamboo rest endpoint within Bamboo plugin by using "TrustedRequestFactory". However, I am getting the following error.

```
com.atlassian.spring.container. : Failed to find component: No bean named 'trustedRequestFactory' available
```

I have imported "TrustedRequestFactory" interface in atlassian-plugin.xml.

```

<component-importname="Trusted Request Factory" key="trusted-request-factory" interface="com.atlassian.sal.api.net.TrustedRequestFactory"/>
```

My code:

```

try{
 final String baseUrl = getAdministrationConfiguration().getBaseUrl();
 final TrustedRequestFactory trustedRequestFactory = (TrustedRequestFactory) ContainerManager.getComponent("trustedRequestFactory");
 final TrustedRequest trustedRequest = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, baseUrl+"/rest/api/latest/info");
 final String result = trustedRequest.execute();
 bambooVersion=result;
} catch (Exception e) {
e.printStackTrace();
 bambooVersion=e.toString();
}
```

I have found following documents for this purpose.

<https://docs.atlassian.com/atlassian-bamboo/6.3.2/com/atlassian/sal/bamboo/trusted/BambooHttpClientTrustedRequestFactory.html>

<https://community.atlassian.com/t5/Answers-Developer-Questions/Call-Confluence-REST-from-within-Confluence/qaq-p/570337>

I have two question for this problem:

1. How can i make use of trustedRequestFactory?
2. Is there an alternative way to call bamboo rest endpoint within the plugin. sd


Answers:
author: netsparker
body: Injecting "TrustedRequestFactory" by Constructor Injection solved the problem. However, getting some of the dependencies through Container Manager and some of them via constructor is strange.

On the other, My actual problem is still continuing. I am now getting following error:

```
com.atlassian.sal.api.net.ResponseStatusException: Unexpected response received. Status code: 401
```

I have tried following code and the code with the previous post.

```
try {
   final String baseUrl = getAdministrationConfiguration().getBaseUrl();
   final TrustedRequest trustedRequest = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, baseUrl+"/rest/api/latest/info");
   trustedRequest.addTrustedTokenAuthentication(AppCommon.getDomainName(baseUrl));
   final String result = trustedRequest.execute();
   bambooVersion=result;
} catch (Exception e) {
   e.printStackTrace();
   bambooVersion=e.toString();
}
```

```
public static String getDomainName(String url) throws MalformedURLException {
   return new URL(url).getHost();
}
```


author: Krystian Brazulewicz
body: Can you please try explaining what are you trying to achieve?

I understand the attempt to access "/rest/api/latest/info" is just an example - otherwise I don't see a point in checking Bamboo server version from a Bamboo plugin by calling REST endpoint.

comments: author: netsparker
body: We want to access server version info which will be sent to Netsparker Cloud and used by our support team. (Assume that there is a problem reported which only occurs in particular version of the Bamboo server. This can point out a compatibility issue.)

Then, It turns out, we can retrieve it from API which needs authentication. [See: How can I obtain bamboo server version in Scan Task plugin.](https://community.atlassian.com/t5/Bamboo-questions/How-can-I-obtain-bamboo-server-version-in-Scan-Task-plugin/qaq-p/759157) What we want is accessing server version info. It can be obtained through API or something else. As long as we are obtaining it, we are fine.


author: Krystian Brazulewicz
body: [@netsparker](/t5/user/viewprofilepage/user-id/1231702) see BuildUtils class in bamboo-api package - it will contain Bamboo version. This package is also available on remote agent JVM and you can be assured that agent will always have its binaries synced with the server.


Tags: app