As we transition to the new Hyland Community, some post links may be temporarily disrupted as we finalize redirects. We apologize for the inconvenience and are working to resolve this as quickly as possible. Details.
// Webscript URL String url = "http://localhost:8080/alfresco/wcservice/controlled-metadata/subjects?format=json"; // Build http request RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); // Prepare callback methods for error and success RequestCallback callback = new RequestCallback() { public void onError(Request request, Throwable exception) { Window.alert("Error: " + exception.getMessage()); } public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { // ANY CUSTOM method to parse JSON response parseResponse(response.getText()); } else { Window.alert("Error: Couldn't retrieve JSON (" + response.getStatusText() + ")"); } } }; // SEND Request to server and catch any errors. try { builder.sendRequest(null, callback); } catch (RequestException e) { Window.alert("Error: Couldn't retrieve JSON (" + e.getMessage() + ")"); }
private static final String GET = "GET"; private String webscriptUrl = "http://localhost:8080/alfresco/service/controlled-metadata/subjects?format=xml"; private String username; private String password; public void getStuffFromXML() throws Exception { // Build request url URL url = new URL(webscriptUrl); // Get response InputStream inputStream = submitGetRequest(url, username, password); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(inputStream); // Normalize text representation doc.getDocumentElement().normalize(); // Parse XML response doSomethingUseful(); } /** * Submit an HTTP GET request to the specified url */ private InputStream submitGetRequest(URL url, String username, String password) throws IOException { // openConnection() doesn't actually open the connection, // just gives you a URLConnection. connect() will open the connection. HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod(GET); // Write auth header BASE64Encoder encoder = new BASE64Encoder(); String encodedCredential = encoder.encode( (username + ":" + password).getBytes() ); connection.setRequestProperty("Authorization", "BASIC " + encodedCredential); // Do request connection.connect(); return connection.getInputStream(); }