Java tutorial
/** * Copyright 2012 Society for Health Information Systems Programmes, India (HISP India) * * This file is part of DHIS2 Reporting module. * * DHIS2 Reporting module is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * DHIS2 Reporting module is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with DHIS2 Reporting module. If not, see <http://www.gnu.org/licenses/>. * **/ package org.openmrs.module.dhisreport.web.controller; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.text.ParseException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; import org.openmrs.GlobalProperty; import org.openmrs.api.context.Context; import org.openmrs.module.dhisreport.api.DHIS2ReportingService; import org.openmrs.module.dhisreport.api.dhis.HttpDhis2Server; import org.openmrs.module.dhisreport.api.model.ReportDefinition; import org.openmrs.web.WebConstants; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.context.request.WebRequest; import org.openmrs.module.dhisreport.api.dxf2.Metadata; import org.openmrs.module.dhisreport.api.dxf2.OrganizationUnit; import org.openmrs.Location; import org.openmrs.LocationAttribute; import org.openmrs.LocationAttributeType; import org.apache.http.client.ClientProtocolException; import javax.xml.bind.JAXBContext; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * The main controller. */ @Controller public class Dhis2ServerController { protected final Log log = LogFactory.getLog(getClass()); @RequestMapping(value = "/module/dhisreport/configureDhis2", method = RequestMethod.GET) public void showConfigForm(ModelMap model, WebRequest webRequest) { DHIS2ReportingService service = Context.getService(DHIS2ReportingService.class); HttpDhis2Server server = service.getDhis2Server(); String dhisurl = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2URL"); String dhisusername = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2UserName"); String dhispassword = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2Password"); URL url = null; try { url = new URL(dhisurl); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (server == null) { server = new HttpDhis2Server(); } server.setUrl(url); server.setUsername(dhisusername); server.setPassword(dhispassword); model.addAttribute("user", Context.getAuthenticatedUser()); model.addAttribute("dhis2Server", server); } @RequestMapping("/module/dhisreport/editReportCode") public void editReportCode(@RequestParam(required = false, value = "reportCode") String reportCode, @RequestParam(required = false, value = "reportId") Integer reportId) { DHIS2ReportingService service = Context.getService(DHIS2ReportingService.class); ReportDefinition rd = service.getReportDefinition(reportId); rd.setCode(reportCode); service.saveReportDefinition(rd); } @RequestMapping(value = "/module/dhisreport/testconnection", method = RequestMethod.POST) public String CheckConnectionWithDHIS2(ModelMap model, WebRequest webRequest) { DHIS2ReportingService service = Context.getService(DHIS2ReportingService.class); HttpDhis2Server server = service.getDhis2Server(); String dhisurl = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2URL"); String dhisusername = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2UserName"); String dhispassword = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2Password"); URL url = null; try { url = new URL(dhisurl); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean val = testConnection(url, dhisusername, dhispassword, server, webRequest, model); String referer = webRequest.getHeader("Referer"); if (val == false) { webRequest.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, Context.getMessageSourceService().getMessage("dhisreport.dhis2ConnectionFailure"), WebRequest.SCOPE_SESSION); return "redirect:" + referer; } webRequest.setAttribute(WebConstants.OPENMRS_MSG_ATTR, Context.getMessageSourceService().getMessage("dhisreport.dhis2ConnectionSuccess"), WebRequest.SCOPE_SESSION); return "redirect:" + referer; } @RequestMapping(value = "/module/dhisreport/configureDhis2", method = RequestMethod.POST) public void saveConfig(ModelMap model, @RequestParam(value = "url", required = true) String urlString, @RequestParam(value = "username", required = true) String username, @RequestParam(value = "password", required = true) String password, WebRequest webRequest) throws ParseException, MalformedURLException { DHIS2ReportingService service = Context.getService(DHIS2ReportingService.class); HttpDhis2Server server = service.getDhis2Server(); // System.out.println( "parameters received on post request" + urlString + username + password ); if (server == null) { server = new HttpDhis2Server(); } URL url = new URL(urlString); server.setUrl(url); server.setUsername(username); server.setPassword(password); service.setDhis2Server(server); boolean val = testConnection(url, username, password, server, webRequest, model); if (val == true) { model.addAttribute("dhis2Server", server); model.addAttribute("user", Context.getAuthenticatedUser()); webRequest.setAttribute(WebConstants.OPENMRS_MSG_ATTR, Context.getMessageSourceService().getMessage("dhisreport.saveConfigSuccess"), WebRequest.SCOPE_SESSION); GlobalProperty globalProperty = new GlobalProperty(); globalProperty.setProperty("dhisreport.dhis2URL"); globalProperty.setPropertyValue(urlString); Context.getAdministrationService().saveGlobalProperty(globalProperty); globalProperty.setProperty("dhisreport.dhis2UserName"); globalProperty.setPropertyValue(username); Context.getAdministrationService().saveGlobalProperty(globalProperty); globalProperty.setProperty("dhisreport.dhis2Password"); globalProperty.setPropertyValue(password); Context.getAdministrationService().saveGlobalProperty(globalProperty); } else { model.addAttribute("dhis2Server", server); model.addAttribute("user", Context.getAuthenticatedUser()); webRequest.setAttribute(WebConstants.OPENMRS_MSG_ATTR, Context.getMessageSourceService().getMessage("dhisreport.saveConfigFailure"), WebRequest.SCOPE_SESSION); } } /* * To test the http connection */ public boolean testConnection(URL url, String username, String password, HttpDhis2Server server, WebRequest webRequest, ModelMap model) { String host = url.getHost(); int port = url.getPort(); HttpHost targetHost = new HttpHost(host, port, url.getProtocol()); DefaultHttpClient httpclient = new DefaultHttpClient(); BasicHttpContext localcontext = new BasicHttpContext(); try { HttpGet httpGet = new HttpGet(url.getPath()); // + // DATAVALUESET_PATH // ); httpGet.addHeader( BasicScheme.authenticate(new UsernamePasswordCredentials(username, password), "UTF-8", false)); HttpResponse response = httpclient.execute(targetHost, httpGet, localcontext); //System.out.println( "Http Response :" + response + ":" + response.getStatusLine().getStatusCode() ); if (response.getStatusLine().getStatusCode() == 200) { log.debug("Dhis2 server configured: " + username + ":xxxxxx " + url.toExternalForm()); return true; } else { log.debug("Dhis2 server not configured"); return false; } } catch (IOException ex) { log.debug("Problem accessing DHIS2 server: " + ex.toString()); return false; } finally { httpclient.getConnectionManager().shutdown(); } } }