protected String getRallyXML(String apiUrl) throws Exception { String responseXML = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); Base64 base64 = new Base64(); String encodeString = new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes())); HttpGet httpGet = new HttpGet(apiUrl); httpGet.addHeader("Authorization", "Basic " + encodeString); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { InputStreamReader reader = new InputStreamReader(entity.getContent()); BufferedReader br = new BufferedReader(reader); StringBuilder sb = new StringBuilder(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } responseXML = sb.toString(); } log.debug("responseXML=" + responseXML); return responseXML; }
public void loadProperty() { try { File file = new File(confFileName); log.debug("file=" + file.getAbsolutePath()); confProperties = new Properties(); confProperties.loadFromXML(new FileInputStream(file)); bonitaApiHost = confProperties.getProperty("bonita.api.host"); bonitaApiUsername = confProperties.getProperty("bonita.api.username"); bonitaApiPassword = confProperties.getProperty("bonita.api.password"); bonitaApiHttpUsername = confProperties.getProperty("bonita.api.http.username"); bonitaApiHttpPassword = confProperties.getProperty("bonita.api.http.password"); bonitaApiPath = confProperties.getProperty("bonita.api.path"); rallyApiHost = confProperties.getProperty("rally.api.host"); rallyApiHttpUsername = confProperties.getProperty("rally.api.http.username"); rallyApiHttpPassword = confProperties.getProperty("rally.api.http.password"); rallyProjectIds = confProperties.getProperty("rally.project.ids"); // bonitaProcessId=properties.getProperty("bonita.process.xp_code.id"); workflowServiceUrl = confProperties.getProperty("service.url"); } catch (Exception e) { log.error("", e); } }
public void removeThread(WorkerThread thread) { threads.remove(thread); Logger.log(Level.DEBUG, Messages.getString("connection_closed", Settings.getLocale())); if (FancyFileServer.getGUI() != null) { FancyFileServer.getGUI().updateConnectionsCounter(); } }
public String loadStream(InputStream is) { StringBuilder sb = new StringBuilder(); try { InputStreamReader reader = new InputStreamReader(is); BufferedReader br = new BufferedReader(reader); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } } catch (Exception e) { log.error("", e); } return sb.toString(); }
public void run() { try { this.serverSocket = new ServerSocket(Settings.getPropertyInteger("server_port")); } catch (Exception e) { Logger.log( Level.WARNING, Messages.getString("can_not_open_port_", Settings.getLocale()) + Settings.getPropertyInteger("server_port")); if (FancyFileServer.getGUI() != null) { FancyFileServer.getGUI().updateServerStatus(); } return; } running = true; this.parameters = new BasicHttpParams(); this.parameters.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000); this.parameters.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024); this.parameters.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false); this.parameters.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true); this.parameters.setParameter( HttpProtocolParams.ORIGIN_SERVER, FancyFileServer.NAME.replaceAll("\\s", "-") + "/" + FancyFileServer.VERSION); Logger.log(Level.INFO, Messages.getString("server_started", Settings.getLocale())); try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface i = (NetworkInterface) interfaces.nextElement(); if (i.isLoopback() || i.isVirtual()) { continue; } Enumeration<InetAddress> addresses = i.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress a = (InetAddress) addresses.nextElement(); if (a instanceof Inet4Address) { Logger.log( Level.INFO, Messages.getString("server_address_", Settings.getLocale()) + "http://" + a.getHostAddress() + ":" + serverSocket.getLocalPort() + "/"); } } } } catch (Exception e) { Logger.log( Level.WARNING, Messages.getString("can_not_get_server_address_", Settings.getLocale()) + e.getMessage()); } if (FancyFileServer.getGUI() != null) { FancyFileServer.getGUI().updateServerStatus(); } while (running) { try { /** Set up HTTP connection */ Socket socket = this.serverSocket.accept(); DefaultHttpServerConnection connection = new DefaultHttpServerConnection(); Logger.log( Level.DEBUG, Messages.getString("incoming_connection_from_", Settings.getLocale()) + socket.getInetAddress().getHostAddress()); connection.bind(socket, this.parameters); /** Set up HTTP protocol processor */ BasicHttpProcessor processor = new BasicHttpProcessor(); processor.addInterceptor(new ResponseDate()); processor.addInterceptor(new ResponseServer()); processor.addInterceptor(new ResponseContent()); processor.addInterceptor(new ResponseConnControl()); /** Set up HTTP request handlers */ HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry(); registry.register("*", new FileHandler()); /** Set up HTTP service */ HttpService service = new HttpService( processor, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory(), registry, this.parameters); /** Start worker thread */ WorkerThread t = new WorkerThread(this, service, connection); t.setDaemon(true); t.start(); } catch (IOException e) { if (running) { running = false; Logger.log( Level.SEVERE, Messages.getString("i_o_error_", Settings.getLocale()) + e.getMessage()); break; } } } if (FancyFileServer.getGUI() != null) { FancyFileServer.getGUI().updateServerStatus(); } Logger.log(Level.INFO, Messages.getString("server_stopped", Settings.getLocale())); }
public class BaseService { protected Logger log = Logger.getLogger(this.getClass()); protected int resultCode; protected String resultMessage = ""; protected String bonitaApiHost; protected String bonitaApiUsername; protected String bonitaApiPassword; protected String bonitaApiHttpUsername; protected String bonitaApiHttpPassword; protected String bonitaApiPath; // protected String bonitaProcessId; protected String workflowServiceUrl; protected String rallyApiHost; protected String rallyApiHttpUsername; protected String rallyApiHttpPassword; protected String rallyProjectIds; protected Properties confProperties; protected String confFileName; protected AuthUser user; public BaseService() {} public BaseService(String confFileName) { this.confFileName = confFileName; loadProperty(); } public void setUser(AuthUser user) { this.user = user; } public void setConfigFile(String confFileName) { this.confFileName = confFileName; loadProperty(); } public void loadProperty() { try { File file = new File(confFileName); log.debug("file=" + file.getAbsolutePath()); confProperties = new Properties(); confProperties.loadFromXML(new FileInputStream(file)); bonitaApiHost = confProperties.getProperty("bonita.api.host"); bonitaApiUsername = confProperties.getProperty("bonita.api.username"); bonitaApiPassword = confProperties.getProperty("bonita.api.password"); bonitaApiHttpUsername = confProperties.getProperty("bonita.api.http.username"); bonitaApiHttpPassword = confProperties.getProperty("bonita.api.http.password"); bonitaApiPath = confProperties.getProperty("bonita.api.path"); rallyApiHost = confProperties.getProperty("rally.api.host"); rallyApiHttpUsername = confProperties.getProperty("rally.api.http.username"); rallyApiHttpPassword = confProperties.getProperty("rally.api.http.password"); rallyProjectIds = confProperties.getProperty("rally.project.ids"); // bonitaProcessId=properties.getProperty("bonita.process.xp_code.id"); workflowServiceUrl = confProperties.getProperty("service.url"); } catch (Exception e) { log.error("", e); } } protected String getRallyXML(String apiUrl) throws Exception { String responseXML = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); Base64 base64 = new Base64(); String encodeString = new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes())); HttpGet httpGet = new HttpGet(apiUrl); httpGet.addHeader("Authorization", "Basic " + encodeString); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { InputStreamReader reader = new InputStreamReader(entity.getContent()); BufferedReader br = new BufferedReader(reader); StringBuilder sb = new StringBuilder(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } responseXML = sb.toString(); } log.debug("responseXML=" + responseXML); return responseXML; } protected String postRallyXML(String apiUrl, String requestXML) throws Exception { String responseXML = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); Base64 base64 = new Base64(); String encodeString = new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes())); HttpPost httpPost = new HttpPost(apiUrl); httpPost.addHeader("Authorization", "Basic " + encodeString); httpPost.setEntity(new StringEntity(requestXML)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); responseXML = getEntityString(entity); return responseXML; } protected String getRallyAPIError(String responseXML) { String errorMsg = ""; try { org.jdom.input.SAXBuilder bSAX = new org.jdom.input.SAXBuilder(); org.jdom.Document doc = bSAX.build(new StringReader(responseXML)); Element root = doc.getRootElement(); XPath xpath = XPath.newInstance("//Errors"); List xlist = xpath.selectNodes(root); Iterator iter = xlist.iterator(); while (iter.hasNext()) { Element item = (Element) iter.next(); errorMsg = item.getChildText("OperationResultError"); } } catch (Exception e) { errorMsg = e.toString(); } return errorMsg; } public String getEntityString(HttpEntity entity) throws Exception { StringBuilder sb = new StringBuilder(); if (entity != null) { InputStreamReader reader = new InputStreamReader(entity.getContent()); BufferedReader br = new BufferedReader(reader); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } } return sb.toString(); } public String loadStream(InputStream is) { StringBuilder sb = new StringBuilder(); try { InputStreamReader reader = new InputStreamReader(is); BufferedReader br = new BufferedReader(reader); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } } catch (Exception e) { log.error("", e); } return sb.toString(); } public int getResultCode() { return resultCode; } public String getResultMessage() { return resultMessage; } public static void main(String[] argv) {} }