Get parameter value from method in another class
<p>What I'm trying to achieve is, using Raygun (a crash reporting service), send the user details of the user who experienced the issue.</p>

<p>I originally tried using a method similar to this to get the username:</p>

<pre><code>Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
username = authentication.getName();
</code></pre>

<p>But for some reason this returned the value, <code>ipaduser</code>. I'm not sure where that value comes from but this obviously isn't the answer. </p>

<p>I then tried:</p>

<pre><code>System.getProperty("user.name");
</code></pre>

<p>But this just gets the name of the logged in user for our API server, not the username of the user using the application.</p>

<p>After some digging around, I discovered that the username and name of the database for a user is being stored in a custom object called ApiAccessToken. The problem is, that this ApiAccessToken object is only being set in methods in our Controller classes.</p>

<p>For instance:</p>

<pre><code>@RequestMapping(path = "/api/foo", method = RequestMethod.GET)
public List&lt;Bar&gt; getBarForDatabase(@RequestParam(value = "api_key") String apiKey{
    ApiAccessToken accessToken = validateApiKey(apiKeyService, apiKey);
    // Etc
}
</code></pre>

<p>The code for ApiAccessToken and validateApiKey() are as follows:</p>

<pre><code>public final class ApiAccessToken {

    private String databaseName;
    private String username;

    public String getDatabaseName() { return databaseName; }

    public void setDatabaseName(String databaseName) { this.databaseName = databaseName; }

    public String getUsername() { return username; }

    public void setUsername(String username) { this.username = username; }
}
</code></pre>

<p>And</p>

<pre><code>protected ApiAccessToken validateApiKey(ApiKeyService apiKeyService, String apiKey){
    UserAccountJdbcRepository userAccountJdbcRepository = new UserAccountJdbcRepository(createDataSourceForDatabase(USERS_DATABASE_NAME));
    ApiAccessToken accessToken = apiKeyService.validateApiKey(userAccountJdbcRepository, apiKey);
    if (accessToken == null) {
        throw new InvalidApiKeyException();
    }
    return  accessToken;
}
</code></pre>

<p>My other idea was having a local variable in the Controller class that would store the value of <code>apiKey</code> and then I would pass that value to the other method using a getter. For example, in the Controller class:</p>

<pre><code>private static String apiKey;

public static String getApiKey() { return apiKey; }

@RequestMapping(path = "/api/foo", method = RequestMethod.GET)
public List&lt;Bar&gt; getBarForDatabase(@RequestParam(value = "api_key") String apiKey{
    ApiAccessToken accessToken = validateApiKey(apiKeyService, apiKey);
    apiKey = this.apiKey; 
    //Etc
}
</code></pre>

<p>And then in the class where I'm sending exception details to Raygun:</p>

<pre><code> String apiKey = Controller.getApiKey();
 ApiAccessToken accessToken = validateApiKey(apiKeyService, apiKey);
 username = accessToken.getUsername();
 database = accessToken.getDatabaseName();
</code></pre>

<p>But that just returns a NullPointerException and doesn't send anything to Raygun.</p>

<p>Any help would be greatly appreciated as I'm not entirely sure what else to try. Let me know if there's anything I've left out that you think I should include. Once again, I'm still pretty new to Spring MVC and the developer who wrote most of this is no longer employed with us.</p>