Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.ruspost.docmangmntactions;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.io.StringReader;
- import java.io.StringWriter;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.TimeZone;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Source;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.stream.StreamResult;
- import javax.xml.transform.stream.StreamSource;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.restlet.data.ChallengeResponse;
- import org.restlet.data.ChallengeScheme;
- import org.restlet.data.MediaType;
- import org.restlet.ext.xstream.XstreamRepresentation;
- import org.restlet.representation.InputRepresentation;
- import org.restlet.representation.Representation;
- import org.restlet.resource.ClientResource;
- import org.restlet.resource.ResourceException;
- import pro.softrise.ruspost.outgoing.EnumDispatchMethod;
- import pro.softrise.ruspost.outgoing.EnumEntityType;
- import pro.softrise.ruspost.outgoing.TypeFile;
- import pro.softrise.ruspost.outgoing.TypeOutgoing;
- import pro.softrise.ruspost.outgoing.TypeOutgoingResponse;
- import pro.softrise.ruspost.outgoing.TypeOutgoingUpdate;
- import pro.softrise.ruspost.outgoing.TypeUserDb;
- import ru.lbpm.metasonic.ruspost.helper.JdbcUtils;
- import ru.lbpm.metasonic.ruspost.helper.JdbcUtils.ResulSetHandler;
- import ru.lbpm.metasonic.ruspost.helper.JdbcUtils.QueryHandler;
- import ru.lbpm.metasonic.ruspost.helper.JdbcUtils.ValueWrapper;
- import com.jcom1.api.dto.interfaces.IUserBean;
- import com.jcom1.api.util.interfaces.IIdNamePair;
- import com.thoughtworks.xstream.XStream;
- import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
- import com.thoughtworks.xstream.converters.basic.DateConverter;
- import com.thoughtworks.xstream.io.xml.StaxDriver;
- import de.metasonic.refinement.global.utils.PropertyUtils;
- public class DocMangmntClient {
- private final static Log log = LogFactory.getFactory().getInstance(DocMangmntClient.class);
- public final static String DATE_FRMT = "yyyy-MM-dd'T'HH:mm:ss";
- public final static String PROPERTY_FILE = "ru.lbpm.metasonic.ruspost.properties";
- public final static String INTEGRATION_GROUP_NAME = "Integration";
- public final static String CLAIM_ISO_URL_RETURN = INTEGRATION_GROUP_NAME +".claimIsoUrlReturn";
- public final static String DOCMANGMNT_GROUP_NAME = INTEGRATION_GROUP_NAME + ".DocMangmnt";
- public final static String SERVICE_URL = DOCMANGMNT_GROUP_NAME + ".Url";
- public final static String LOGIN = DOCMANGMNT_GROUP_NAME + ".Login";
- public final static String PSSWRD = DOCMANGMNT_GROUP_NAME + ".Password";
- public final static String ROUTING_BASE_URL = "http://10.7.124.114/DOTESTUZ01/hs/sr/userdb/";
- //public final static String SERVICE_URL = "http://10.7.124.114/DOTESTUZ01/hs/sr/outgoing/";
- //public final static String LOGIN = "claim";
- //public final static String PSSWRD = "claim";
- private static final String ROUTING_PARAM_CONTROL = "?control";
- private static final String USER_GUID_AD = "GUID_AD";
- private static String getTypeUserDb(IUserBean userBean) {
- log.debug("Start getTypeUserDb");
- String url = "";
- IIdNamePair[] userAttributes = userBean.getAttributes();
- String guid = null;
- for (IIdNamePair pair : userAttributes) {
- if (USER_GUID_AD.equalsIgnoreCase(pair.getId())) {
- guid = pair.getName();
- break;
- }
- }
- log.debug("Guid: " + guid);
- if (null != guid && !guid.isEmpty()) {
- ClientResource xstreamClient = new ClientResource(ROUTING_BASE_URL + guid);
- String login = PropertyUtils.getProperty(PROPERTY_FILE, LOGIN, "");
- String psswrd = PropertyUtils.getProperty(PROPERTY_FILE, PSSWRD, "");
- ChallengeResponse authentication = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, login, psswrd);
- xstreamClient.setChallengeResponse(authentication);
- try {
- Representation responseRepresentation = xstreamClient.get();
- XStream xstream = new XStream(new StaxDriver());
- xstream.alias("userdb", TypeUserDb.class);
- xstream.useAttributeFor(String.class);
- TypeUserDb userDb = (TypeUserDb) xstream.fromXML(responseRepresentation.getText());
- log.debug(userDb);
- if (null != userDb) {
- log.debug("Composing URL");
- url = userDb.getSchemaWeb() + "://" + userDb.getServerWeb();
- url += null != userDb.getPortWeb() ? (":" + userDb.getPortWeb()) : "";
- url += null != userDb.getPortApp() ? (":" + userDb.getPortApp()) : "";
- url += "/" + userDb.getDbName();
- url += "/" + guid;
- //determine if control-param is needed
- url += isAnalyst(userBean.getId()) ? ROUTING_PARAM_CONTROL : "";
- }
- } catch (Exception e) {
- log.debug("Error has occured while composing URL: " + e.getMessage());
- e.printStackTrace();
- }
- }
- log.debug("End getTypeUserDb. Returning '" + url + "'");
- return url;
- }
- public static boolean isAnalyst(String userId) throws Exception {
- log.debug("Determining if user with id " + userId + " an analyst");
- final String isUserAnalyst = "SELECT IS_ANALYST FROM UM_USER WHERE id = ?\n";
- List<ValueWrapper> idAnalyst = new ArrayList<ValueWrapper>();
- idAnalyst.add(new ValueWrapper(userId, String.class));
- Connection conn = null;
- try {
- conn = JdbcUtils.getConnection(JdbcUtils.PROP_DB_INT);
- Boolean isAnalyst = (Boolean) JdbcUtils.executeQuery(conn, isUserAnalyst, idAnalyst,
- new ResulSetHandler() {
- @Override
- public Object processResultSet(ResultSet rSet) throws Exception {
- if (rSet.next()) {
- return rSet.getBoolean("is_analyst");
- }
- return null;
- }
- });
- return isAnalyst;
- } finally {
- if (conn != null)
- conn.close();
- }
- }
- public static void updateDoc(String docGUID, TypeOutgoingUpdate typeOutgoingUpdate, IUserBean userBean) throws Exception {
- String serviceUrl = getTypeUserDb(userBean);
- String login = PropertyUtils.getProperty(PROPERTY_FILE, LOGIN, "");
- String psswrd = PropertyUtils.getProperty(PROPERTY_FILE, PSSWRD, "");
- log.debug("serviceUrl : " + serviceUrl + ", login : "+ login + ", psswrd : "+ psswrd);
- ClientResource xstreamClient = new ClientResource(serviceUrl + docGUID);
- XstreamRepresentation<TypeOutgoingUpdate> fromClientXstreamRepresentation = new XstreamRepresentation<TypeOutgoingUpdate>(typeOutgoingUpdate);
- XStream fromClientXstream = fromClientXstreamRepresentation.getXstream();
- fromClientXstream.alias("outgoingUpdate", TypeOutgoingUpdate.class);
- DateConverter dateConverter = new DateConverter(DATE_FRMT, new String[]{DATE_FRMT+".S"}, TimeZone.getDefault());
- fromClientXstream.registerConverter(dateConverter);
- fromClientXstream.ignoreUnknownElements();
- fromClientXstream.useAttributeFor(String.class);
- fromClientXstream.useAttributeFor(Date.class);
- fromClientXstream.useAttributeFor(Integer.class);
- fromClientXstream.useAttributeFor(Boolean.class);
- fromClientXstream.useAttributeFor(byte[].class);
- fromClientXstream.useAttributeFor(EnumDispatchMethod.class);
- fromClientXstream.useAttributeFor(EnumEntityType.class);
- fromClientXstream.addImplicitCollection(
- TypeOutgoingUpdate.class, "file", "file", TypeFile.class);
- fromClientXstream.registerConverter(DISPATCH_METHOD_VALUE_CONVERTER);
- fromClientXstream.registerConverter(EVENT_TYPE_VALUE_CONVERTER);
- if (log.isDebugEnabled()) {
- String input = fromClientXstream.toXML(typeOutgoingUpdate);
- String output = formatXML(input);
- log.debug(output);
- }
- ChallengeResponse authentication = new ChallengeResponse(
- ChallengeScheme.HTTP_BASIC, login, psswrd);
- xstreamClient.setChallengeResponse(authentication);
- Representation responseRepresentation = null;
- try {
- responseRepresentation = xstreamClient.put(fromClientXstreamRepresentation);
- } catch (ResourceException re) {
- log.error(re.getStatus().toString());
- responseRepresentation = xstreamClient.getResponseEntity();
- }
- log.debug(responseRepresentation.getText());
- }
- public static TypeOutgoingResponse createDoc(TypeOutgoing typeOutgoing) throws Exception {
- String serviceUrl = PropertyUtils.getProperty(PROPERTY_FILE, SERVICE_URL, "");
- String login = PropertyUtils.getProperty(PROPERTY_FILE, LOGIN, "");
- String psswrd = PropertyUtils.getProperty(PROPERTY_FILE, PSSWRD, "");
- log.debug("serviceUrl : " + serviceUrl + ", login : "+ login + ", psswrd : "+ psswrd);
- ClientResource xstreamClient = new ClientResource(serviceUrl);
- XstreamRepresentation<TypeOutgoing> fromClientXstreamRepresentation = new XstreamRepresentation<TypeOutgoing>(typeOutgoing );
- XStream fromClientXstream = fromClientXstreamRepresentation.getXstream();
- fromClientXstream.alias("outgoing", TypeOutgoing.class);
- DateConverter dateConverter = new DateConverter(DATE_FRMT, new String[]{DATE_FRMT+".S"}, TimeZone.getDefault());
- fromClientXstream.registerConverter(dateConverter);
- fromClientXstream.ignoreUnknownElements();
- fromClientXstream.useAttributeFor(String.class);
- fromClientXstream.useAttributeFor(Date.class);
- fromClientXstream.useAttributeFor(Integer.class);
- fromClientXstream.useAttributeFor(Boolean.class);
- fromClientXstream.useAttributeFor(byte[].class);
- fromClientXstream.useAttributeFor(EnumDispatchMethod.class);
- fromClientXstream.useAttributeFor(EnumEntityType.class);
- fromClientXstream.addImplicitCollection(
- TypeOutgoing.class, "file", "file", TypeFile.class);
- fromClientXstream.registerConverter(DISPATCH_METHOD_VALUE_CONVERTER);
- fromClientXstream.registerConverter(EVENT_TYPE_VALUE_CONVERTER);
- if (log.isDebugEnabled()) {
- String input = fromClientXstream.toXML(typeOutgoing);
- String output = formatXML(input);
- log.debug(output);
- }
- ChallengeResponse authentication = new ChallengeResponse(
- ChallengeScheme.HTTP_BASIC, login, psswrd);
- xstreamClient.setChallengeResponse(authentication);
- Representation responseRepresentation = null;
- try {
- responseRepresentation = xstreamClient.post(fromClientXstreamRepresentation);
- if (log.isDebugEnabled()) {
- InputStream is = responseRepresentation.getStream();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int c;
- while(( c = is.read())!= -1) {
- baos.write(c);
- }
- byte[] buf = baos.toByteArray();
- ByteArrayInputStream bais = new ByteArrayInputStream(buf);
- responseRepresentation = new InputRepresentation(bais);
- responseRepresentation.setMediaType(MediaType.APPLICATION_XML);
- String input = new String(buf, "UTF-8");
- String output = formatXML(input);
- log.debug(output);
- }
- XstreamRepresentation<TypeOutgoingResponse> xstreamRepresentation =
- new XstreamRepresentation<TypeOutgoingResponse>(responseRepresentation, TypeOutgoingResponse.class);
- XStream xstream = new XStream(new StaxDriver());
- xstream.registerConverter(dateConverter);
- xstream.ignoreUnknownElements();
- ClassLoader classLoader = TypeOutgoingResponse.class.getClassLoader();
- xstream.setClassLoader(classLoader);
- xstream.ignoreUnknownElements();
- xstream.alias("outgoingResponse", TypeOutgoingResponse.class);
- xstream.useAttributeFor(String.class);
- xstream.useAttributeFor(Date.class);
- xstream.useAttributeFor(Integer.class);
- xstream.useAttributeFor(Boolean.class);
- xstreamRepresentation.setXstream(xstream);
- TypeOutgoingResponse outgoingResponse = xstreamRepresentation.getObject();
- log.debug(outgoingResponse.toString());
- return outgoingResponse;
- } catch (ResourceException re) {
- log.error(re.getStatus().toString());
- responseRepresentation = xstreamClient.getResponseEntity();
- log.error(responseRepresentation.getText());
- }
- return null;
- }
- public final static AbstractSingleValueConverter
- DISPATCH_METHOD_VALUE_CONVERTER = new AbstractSingleValueConverter() {
- @Override
- public Object fromString(String parsedText) {
- return EnumDispatchMethod.fromValue(parsedText);
- }
- @Override
- public String toString(Object obj) {
- return ((EnumDispatchMethod)obj).value();
- }
- @Override
- public boolean canConvert(Class clazz) {
- return clazz.equals(EnumDispatchMethod.class);
- }
- };
- public final static AbstractSingleValueConverter
- EVENT_TYPE_VALUE_CONVERTER = new AbstractSingleValueConverter() {
- @Override
- public Object fromString(String parsedText) {
- return EnumEntityType.fromValue(parsedText);
- }
- @Override
- public String toString(Object obj) {
- return ((EnumEntityType) obj).value();
- }
- @Override
- public boolean canConvert(Class clazz) {
- return clazz.equals(EnumEntityType.class);
- }
- };
- public static String formatXML (String input) throws Exception {
- Integer indent = 4;
- Source xmlInput = new StreamSource(new StringReader(input));
- StringWriter stringWriter = new StringWriter();
- StreamResult xmlOutput = new StreamResult(stringWriter);
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- transformerFactory.setAttribute("indent-number", indent);
- Transformer transformer = transformerFactory.newTransformer();
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.transform(xmlInput, xmlOutput);
- String output = xmlOutput.getWriter().toString();
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment