OAuth1Signature { static String normalizeParameters(final OAuth1Request request, final OAuth1Parameters params) { final ArrayList<String[]> list = new ArrayList<String[]>(); for (final String key : params.keySet()) { if (key.equals(OAuth1Parameters.REALM) || key.equals(OAuth1Parameters.SIGNATURE)) { continue; } final String value = params.get(key); if (value != null) { addParam(key, value, list); } } for (final String key : request.getParameterNames()) { if (key.startsWith("oauth_") && params.containsKey(key)) { continue; } final List<String> values = request.getParameterValues(key); if (values != null) { for (final String value : values) { addParam(key, value, list); } } } Collections.sort(list, new Comparator<String[]>() { @Override public int compare(final String[] t, final String[] t1) { final int c = t[0].compareTo(t1[0]); return c == 0 ? t[1].compareTo(t1[1]) : c; } }); final StringBuilder buf = new StringBuilder(); for (final Iterator<String[]> i = list.iterator(); i.hasNext(); ) { final String[] param = i.next(); buf.append(param[0]).append("=").append(param[1]); if (i.hasNext()) { buf.append('&'); } } return buf.toString(); } @Inject  OAuth1Signature(final InjectionManager injectionManager); String generate(final OAuth1Request request,
                           final OAuth1Parameters params, final OAuth1Secrets secrets); void sign(final OAuth1Request request,
                     OAuth1Parameters params, final OAuth1Secrets secrets); boolean verify(final OAuth1Request request,
                          final OAuth1Parameters params, final OAuth1Secrets secrets);  }
@Test public void testNormalizeParameters() { final MultivaluedMap<String, String> params = new MultivaluedHashMap<String, String>(); params.add("org-country", "US"); params.add("org", "acme"); params.add("a", "b"); params.add("org", "dummy"); String normalizedParams = OAuth1Signature.normalizeParameters(new OAuth1Request() { @Override public String getRequestMethod() { throw new UnsupportedOperationException("Not supported yet."); } @Override public URL getRequestURL() { throw new UnsupportedOperationException("Not supported yet."); } @Override public Set<String> getParameterNames() { return params.keySet(); } @Override public List<String> getParameterValues(String name) { return params.get(name); } @Override public List<String> getHeaderValues(String name) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void addHeaderValue(String name, String value) throws IllegalStateException { throw new UnsupportedOperationException("Not supported yet."); } }, new OAuth1Parameters()); assertEquals("a=b&org=acme&org=dummy&org-country=US", normalizedParams); }
@Test public void testNullParamValue() { final MultivaluedMap<String, String> params = new MultivaluedHashMap<String, String>(); params.add("org-country", "US"); params.put("org", Arrays.asList(new String[]{null})); params.add("a", "b"); String normalizedParams = OAuth1Signature.normalizeParameters(new OAuth1Request() { @Override public String getRequestMethod() { throw new UnsupportedOperationException("Not supported yet."); } @Override public URL getRequestURL() { throw new UnsupportedOperationException("Not supported yet."); } @Override public Set<String> getParameterNames() { return params.keySet(); } @Override public List<String> getParameterValues(String name) { return params.get(name); } @Override public List<String> getHeaderValues(String name) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void addHeaderValue(String name, String value) throws IllegalStateException { throw new UnsupportedOperationException("Not supported yet."); } }, new OAuth1Parameters()); assertEquals("a=b&org=&org-country=US", normalizedParams); }