📄 syncadapter.java
字号:
if (log.isLoggable(Level.FINE)) { log.fine( "The synchronization is " + ((outMessage.isLastMessage()) ? "" : "not ") + "complete" ); } response = new Sync4jResponse(outMessage, resultMimeType); } catch (Sync4jException e) { String err = "Error processing return message: " + e.getMessage(); log.severe(err); throw new ServerException(err); } return (SyncResponse) response; } /** * Used to process a status information as needed by the client object (i.e. * errors or success). * * @param statusCode the status code * @param statusMessage additional descriptive message * * @see sync4j.framework.core.StatusCode for valid status codes. */ public SyncResponse processStatusCode(int statusCode, String info){ if (statusCode != StatusCode.OK) { sessionHandler.abort(statusCode); } return null; } // --------------------------------------------------------- private methods /** * Processes the given SyncML XML message. See the class description for * more information. * * @param message the message to be processed * * @return the response message * * @throws ProtocolException */ private SyncML processXMLMessage(String msg) throws ServerException { try { try { checkMessage(messageInput); return sessionHandler.processMessage(messageInput); } catch (InvalidCredentialsException e) { return sessionHandler.processError(messageInput, e); } catch (ServerException e) { return sessionHandler.processError(messageInput, e); } catch (ProtocolException e) { return sessionHandler.processError(messageInput, new BadRequestException(e.getMessage())); } } catch (Sync4jException e1) { // // This can be due only to processError // throw new ServerException(e1); } } /** * Checks if the given string is an accepted URL (starting with http://, * https:// or file://). If yes, a new URL object representing the given * url is returned; otherwise, the given string is considered a file name * and a new URL is obtained calling File.toURL(). * * @param s the string to check * * @return the corresponding URL if the string represents a URL or the * fixed URL if the string is a pathname/filename * * @throws MalformedURLException */ private URL fixURI(final String s) throws MalformedURLException { try { return new URL(s); } catch (MalformedURLException e) { // // This is not a URL, let's consider it just a file } try { return new File(new File(s).getCanonicalPath()).toURL(); } catch (IOException e) { throw new MalformedURLException("Unable to convert" + s + " to a URL"); } } /** * Checks if the given message can be processed by this server implementation.<br> * In detail, it checks: * <ul> * <li> if server_uri (as specified by the configuration parameter * CONFIG_SERVER_URI) is a URL, the taget uri hostname and the server * uri hostname must match. * <li> if the server uri is not a valid url, server uri and target uri * must match as strings. * </ul> * * @param msg the message to be checked * * @throws ServerException in case the message is wrong */ private void checkMessage(SyncML msg) throws ServerException { String serverURI = null; String targetURI = null; serverURI = config.getStringValue(CONFIG_SERVER_URI); targetURI = msg.getSyncHdr().getTarget().getLocURI(); try { URL serverURL = new URL(serverURI); URL targetURL = new URL(targetURI); serverURI = serverURL.getHost(); targetURI = targetURL.getHost(); } catch (MalformedURLException e) { // // The server uri is not a valid URL. Simple string comparison // will be performed // } if (log.isLoggable(Level.FINE)) { log.fine("serverURI: " + serverURI); log.fine("targetURI: " + targetURI); } if (!serverURI.equals(targetURI)) { throw new BadRequestException( "The message is addressed to " + targetURI + " not to " + serverURI ); } } /** * Returns the <i>Sync4jEngineFactory</i> object that a <i>SessionHandler</i> * should use to create a <i>SyncEngine</i>. The method dynamically loads the * <i>SyncEngineFactory</i> specified by the environment key * <i>syncengine/factory/bean</i>. * * @return the <i>Sync4jEngineFactory</i> object * * @throws Sync4jException */ protected void getSyncEngineFactory() throws Sync4jException { if (syncEngineFactory != null) { return; } String factoryName = null; try { InitialContext ctx = new InitialContext(); factoryName = (String)ctx.lookup(ENV_ENGINE_FACTORY_NAME); if (log.isLoggable(Level.FINE)) { log.fine("factory name: " + factoryName); log.fine("Getting a new syncEngineFactory..."); } if ((factoryName == null) || (factoryName.length() == 0)) { throw new Sync4jException( ENV_ENGINE_FACTORY_NAME + " must be specified" ); } syncEngineFactory = (SyncEngineFactory)BeanFactory.getBeanInstance(getClass().getClassLoader(), factoryName); } catch (Exception e) { log.throwing(getClass().getName(), "getSyncEngineFactory", e); throw new Sync4jException( "Error in loading " + factoryName , e ); } // // The engine factory is configured with the server configuration so // that the engine object has the opportunity to access to the server // configuration // syncEngineFactory.setConfiguration(config); } /** * Loads the configuration for the server. The URI from which the configuration * is must be set as the envirnoment property named as <i>ENV_SERVER_CONFIG_URI</i>. * * @throws ConfigurationException in case of errors. */ protected void loadConfiguration() throws ConfigurationException { URL configURI = null, configPath = null; config = new Configuration(); try { InitialContext ctx = new InitialContext(); configURI = fixURI((String)ctx.lookup(ENV_SERVER_CONFIG_URI )); configPath = fixURI((String)ctx.lookup(ENV_SERVER_CONFIG_PATH)); log.finest("configURI=" + configURI); log.finest("configPath=" + configPath); config.setClassLoader( new ConfigClassLoader( new URL[] { configPath }, getClass().getClassLoader() ) ); log.finest("config.load(" + configURI+")"); config.load(configURI.toString()); if (log.isLoggable(Level.FINEST)) { log.finest("Configuration: " + config); } } catch (ConfigurationException e) { throw e; } catch (Exception e) { throw new ConfigurationException("Error loading configuration from " + configURI, e); } } private static class Sync4jResponse implements SyncResponse { private SyncML msg; private String resultMimeType; private Sync4jResponse( final SyncML msg , final String resultMimeType) { this.msg = msg; this.resultMimeType = resultMimeType; } public SyncML getMessage() { return this.msg; } public String getMessageString() { return Util.toXML(msg); } public String getMimeType() { return resultMimeType; } /** * Is this message the last message allowed for the current session? * * @return true if yes, false otherwise * */ public boolean isCompleted() { return msg.isLastMessage(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -