Ladies_Man

bck

Aug 4th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.54 KB | None | 0 0
  1. package ru.ruspost.docmangmntactions;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.InputStream;
  6. import java.io.StringReader;
  7. import java.io.StringWriter;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.List;
  13. import java.util.TimeZone;
  14.  
  15. import javax.xml.transform.OutputKeys;
  16. import javax.xml.transform.Source;
  17. import javax.xml.transform.Transformer;
  18. import javax.xml.transform.TransformerFactory;
  19. import javax.xml.transform.stream.StreamResult;
  20. import javax.xml.transform.stream.StreamSource;
  21.  
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.restlet.data.ChallengeResponse;
  25. import org.restlet.data.ChallengeScheme;
  26. import org.restlet.data.MediaType;
  27. import org.restlet.ext.xstream.XstreamRepresentation;
  28. import org.restlet.representation.InputRepresentation;
  29. import org.restlet.representation.Representation;
  30. import org.restlet.resource.ClientResource;
  31. import org.restlet.resource.ResourceException;
  32.  
  33. import pro.softrise.ruspost.outgoing.EnumDispatchMethod;
  34. import pro.softrise.ruspost.outgoing.EnumEntityType;
  35. import pro.softrise.ruspost.outgoing.TypeFile;
  36. import pro.softrise.ruspost.outgoing.TypeOutgoing;
  37. import pro.softrise.ruspost.outgoing.TypeOutgoingResponse;
  38. import pro.softrise.ruspost.outgoing.TypeOutgoingUpdate;
  39. import pro.softrise.ruspost.outgoing.TypeUserDb;
  40. import ru.lbpm.metasonic.ruspost.helper.JdbcUtils;
  41. import ru.lbpm.metasonic.ruspost.helper.JdbcUtils.ResulSetHandler;
  42. import ru.lbpm.metasonic.ruspost.helper.JdbcUtils.QueryHandler;
  43. import ru.lbpm.metasonic.ruspost.helper.JdbcUtils.ValueWrapper;
  44.  
  45. import com.jcom1.api.dto.interfaces.IUserBean;
  46. import com.jcom1.api.util.interfaces.IIdNamePair;
  47. import com.thoughtworks.xstream.XStream;
  48. import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
  49. import com.thoughtworks.xstream.converters.basic.DateConverter;
  50. import com.thoughtworks.xstream.io.xml.StaxDriver;
  51.  
  52. import de.metasonic.refinement.global.utils.PropertyUtils;
  53.  
  54. public class DocMangmntClient {
  55.    
  56.     private final static Log log = LogFactory.getFactory().getInstance(DocMangmntClient.class);
  57.    
  58.     public final static String DATE_FRMT = "yyyy-MM-dd'T'HH:mm:ss";
  59.     public final static String PROPERTY_FILE = "ru.lbpm.metasonic.ruspost.properties";
  60.     public final static String INTEGRATION_GROUP_NAME = "Integration";
  61.     public final static String CLAIM_ISO_URL_RETURN = INTEGRATION_GROUP_NAME +".claimIsoUrlReturn";
  62.     public final static String DOCMANGMNT_GROUP_NAME = INTEGRATION_GROUP_NAME + ".DocMangmnt";
  63.     public final static String SERVICE_URL = DOCMANGMNT_GROUP_NAME + ".Url";
  64.     public final static String LOGIN = DOCMANGMNT_GROUP_NAME + ".Login";
  65.     public final static String PSSWRD = DOCMANGMNT_GROUP_NAME + ".Password";
  66.    
  67.     public final static String ROUTING_BASE_URL = "http://10.7.124.114/DOTESTUZ01/hs/sr/userdb/";
  68.     //public final static String SERVICE_URL    = "http://10.7.124.114/DOTESTUZ01/hs/sr/outgoing/";
  69.     //public final static String LOGIN  = "claim";
  70.     //public final static String PSSWRD = "claim";
  71.    
  72.     private static final String ROUTING_PARAM_CONTROL = "?control";
  73.     private static final String USER_GUID_AD = "GUID_AD";
  74.    
  75.     private static String getTypeUserDb(IUserBean userBean) {
  76.         log.debug("Start getTypeUserDb");
  77.         String url = "";
  78.        
  79.         IIdNamePair[] userAttributes = userBean.getAttributes();
  80.         String guid = null;
  81.         for (IIdNamePair pair : userAttributes) {
  82.             if (USER_GUID_AD.equalsIgnoreCase(pair.getId())) {
  83.                 guid = pair.getName();
  84.                 break;
  85.             }
  86.         }
  87.         log.debug("Guid: " + guid);
  88.        
  89.         if (null != guid && !guid.isEmpty()) {
  90.  
  91.             ClientResource xstreamClient = new ClientResource(ROUTING_BASE_URL + guid);
  92.            
  93.             String login = PropertyUtils.getProperty(PROPERTY_FILE, LOGIN, "");
  94.             String psswrd = PropertyUtils.getProperty(PROPERTY_FILE, PSSWRD, "");
  95.             ChallengeResponse authentication = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, login, psswrd);
  96.            
  97.             xstreamClient.setChallengeResponse(authentication);
  98.            
  99.             try {
  100.                 Representation responseRepresentation = xstreamClient.get();
  101.                
  102.                 XStream xstream  = new XStream(new StaxDriver());
  103.                 xstream.alias("userdb", TypeUserDb.class);
  104.                 xstream.useAttributeFor(String.class);
  105.                 TypeUserDb userDb = (TypeUserDb) xstream.fromXML(responseRepresentation.getText());
  106.                 log.debug(userDb);
  107.                
  108.                 if (null != userDb) {
  109.                     log.debug("Composing URL");
  110.                    
  111.                     url = userDb.getSchemaWeb() + "://" + userDb.getServerWeb();
  112.                     url += null != userDb.getPortWeb() ? (":" + userDb.getPortWeb()) : "";
  113.                     url += null != userDb.getPortApp() ? (":" + userDb.getPortApp()) : "";
  114.                     url += "/" + userDb.getDbName();
  115.                     url += "/" + guid;
  116.                    
  117.                     //determine if control-param is needed
  118.                     url += isAnalyst(userBean.getId()) ? ROUTING_PARAM_CONTROL : "";
  119.                 }
  120.                
  121.             } catch (Exception e) {
  122.                 log.debug("Error has occured while composing URL: " + e.getMessage());
  123.                 e.printStackTrace();
  124.             }
  125.         }
  126.         log.debug("End getTypeUserDb. Returning '" + url + "'");
  127.         return url;
  128.     }
  129.    
  130.     public static boolean isAnalyst(String userId) throws Exception {
  131.         log.debug("Determining if user with id " + userId + " an analyst");
  132.        
  133.         final String isUserAnalyst = "SELECT IS_ANALYST FROM UM_USER  WHERE id = ?\n";
  134.         List<ValueWrapper> idAnalyst = new ArrayList<ValueWrapper>();
  135.         idAnalyst.add(new ValueWrapper(userId, String.class));
  136.                  
  137.         Connection conn = null;
  138.                  
  139.         try {
  140.             conn = JdbcUtils.getConnection(JdbcUtils.PROP_DB_INT);
  141.             Boolean isAnalyst =  (Boolean) JdbcUtils.executeQuery(conn, isUserAnalyst, idAnalyst,
  142.                     new ResulSetHandler() {    
  143.                         @Override
  144.                         public Object processResultSet(ResultSet rSet) throws Exception {
  145.                             if (rSet.next()) {
  146.                                 return rSet.getBoolean("is_analyst");
  147.                             }
  148.                             return null;
  149.                         }
  150.                     });
  151.             return isAnalyst;                
  152.  
  153.         } finally {
  154.             if (conn != null)
  155.                 conn.close();
  156.         }
  157.     }
  158.    
  159.  
  160.     public static void updateDoc(String docGUID, TypeOutgoingUpdate typeOutgoingUpdate, IUserBean userBean) throws Exception {
  161.         String serviceUrl = getTypeUserDb(userBean);
  162.         String login = PropertyUtils.getProperty(PROPERTY_FILE, LOGIN, "");
  163.         String psswrd = PropertyUtils.getProperty(PROPERTY_FILE, PSSWRD, "");
  164.         log.debug("serviceUrl : " + serviceUrl + ", login : "+ login + ", psswrd : "+ psswrd);
  165.        
  166.         ClientResource xstreamClient = new ClientResource(serviceUrl + docGUID);
  167.  
  168.         XstreamRepresentation<TypeOutgoingUpdate> fromClientXstreamRepresentation = new XstreamRepresentation<TypeOutgoingUpdate>(typeOutgoingUpdate);
  169.         XStream fromClientXstream  = fromClientXstreamRepresentation.getXstream();
  170.         fromClientXstream.alias("outgoingUpdate", TypeOutgoingUpdate.class);
  171.         DateConverter dateConverter = new DateConverter(DATE_FRMT, new String[]{DATE_FRMT+".S"}, TimeZone.getDefault());
  172.         fromClientXstream.registerConverter(dateConverter);
  173.         fromClientXstream.ignoreUnknownElements();
  174.         fromClientXstream.useAttributeFor(String.class);
  175.         fromClientXstream.useAttributeFor(Date.class);
  176.         fromClientXstream.useAttributeFor(Integer.class);
  177.         fromClientXstream.useAttributeFor(Boolean.class);
  178.         fromClientXstream.useAttributeFor(byte[].class);
  179.         fromClientXstream.useAttributeFor(EnumDispatchMethod.class);
  180.         fromClientXstream.useAttributeFor(EnumEntityType.class);
  181.         fromClientXstream.addImplicitCollection(
  182.                 TypeOutgoingUpdate.class, "file", "file", TypeFile.class);
  183.         fromClientXstream.registerConverter(DISPATCH_METHOD_VALUE_CONVERTER);
  184.         fromClientXstream.registerConverter(EVENT_TYPE_VALUE_CONVERTER);
  185.         if (log.isDebugEnabled()) {
  186.             String input = fromClientXstream.toXML(typeOutgoingUpdate);
  187.             String output = formatXML(input);
  188.             log.debug(output);
  189.         }
  190.         ChallengeResponse authentication = new ChallengeResponse(
  191.                 ChallengeScheme.HTTP_BASIC, login, psswrd);
  192.         xstreamClient.setChallengeResponse(authentication);
  193.         Representation responseRepresentation = null;
  194.         try {
  195.             responseRepresentation = xstreamClient.put(fromClientXstreamRepresentation);
  196.         } catch (ResourceException re) {
  197.             log.error(re.getStatus().toString());
  198.             responseRepresentation = xstreamClient.getResponseEntity();
  199.         }
  200.         log.debug(responseRepresentation.getText());
  201.     }
  202.    
  203.    
  204.     public static TypeOutgoingResponse createDoc(TypeOutgoing typeOutgoing) throws Exception {
  205.         String serviceUrl = PropertyUtils.getProperty(PROPERTY_FILE, SERVICE_URL, "");
  206.         String login = PropertyUtils.getProperty(PROPERTY_FILE, LOGIN, "");
  207.         String psswrd = PropertyUtils.getProperty(PROPERTY_FILE, PSSWRD, "");
  208.         log.debug("serviceUrl : " + serviceUrl + ", login : "+ login + ", psswrd : "+ psswrd);
  209.        
  210.         ClientResource xstreamClient = new ClientResource(serviceUrl);
  211.        
  212.         XstreamRepresentation<TypeOutgoing> fromClientXstreamRepresentation = new XstreamRepresentation<TypeOutgoing>(typeOutgoing );
  213.         XStream fromClientXstream  = fromClientXstreamRepresentation.getXstream();
  214.         fromClientXstream.alias("outgoing", TypeOutgoing.class);
  215.         DateConverter dateConverter = new DateConverter(DATE_FRMT, new String[]{DATE_FRMT+".S"}, TimeZone.getDefault());
  216.         fromClientXstream.registerConverter(dateConverter);
  217.         fromClientXstream.ignoreUnknownElements();
  218.         fromClientXstream.useAttributeFor(String.class);
  219.         fromClientXstream.useAttributeFor(Date.class);
  220.         fromClientXstream.useAttributeFor(Integer.class);
  221.         fromClientXstream.useAttributeFor(Boolean.class);
  222.         fromClientXstream.useAttributeFor(byte[].class);
  223.         fromClientXstream.useAttributeFor(EnumDispatchMethod.class);
  224.         fromClientXstream.useAttributeFor(EnumEntityType.class);
  225.         fromClientXstream.addImplicitCollection(
  226.                 TypeOutgoing.class, "file", "file", TypeFile.class);
  227.         fromClientXstream.registerConverter(DISPATCH_METHOD_VALUE_CONVERTER);
  228.         fromClientXstream.registerConverter(EVENT_TYPE_VALUE_CONVERTER);
  229.         if (log.isDebugEnabled()) {
  230.             String input = fromClientXstream.toXML(typeOutgoing);
  231.             String output = formatXML(input);
  232.             log.debug(output);
  233.         }
  234.         ChallengeResponse authentication = new ChallengeResponse(
  235.                 ChallengeScheme.HTTP_BASIC, login, psswrd);
  236.         xstreamClient.setChallengeResponse(authentication);
  237.         Representation responseRepresentation = null;
  238.         try {
  239.             responseRepresentation = xstreamClient.post(fromClientXstreamRepresentation);
  240.  
  241.             if (log.isDebugEnabled()) {
  242.                 InputStream is = responseRepresentation.getStream();
  243.                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
  244.                 int c;
  245.                 while(( c = is.read())!= -1) {
  246.                     baos.write(c);
  247.                 }
  248.                 byte[] buf = baos.toByteArray();
  249.                 ByteArrayInputStream bais = new ByteArrayInputStream(buf);
  250.                 responseRepresentation = new InputRepresentation(bais);
  251.                 responseRepresentation.setMediaType(MediaType.APPLICATION_XML);
  252.                 String input = new String(buf, "UTF-8");
  253.                 String output = formatXML(input);
  254.                 log.debug(output);
  255.             }
  256.            
  257.             XstreamRepresentation<TypeOutgoingResponse> xstreamRepresentation =
  258.                     new XstreamRepresentation<TypeOutgoingResponse>(responseRepresentation, TypeOutgoingResponse.class);
  259.             XStream xstream  = new XStream(new StaxDriver());
  260.             xstream.registerConverter(dateConverter);
  261.             xstream.ignoreUnknownElements();
  262.             ClassLoader classLoader = TypeOutgoingResponse.class.getClassLoader();
  263.             xstream.setClassLoader(classLoader);
  264.             xstream.ignoreUnknownElements();
  265.             xstream.alias("outgoingResponse", TypeOutgoingResponse.class);
  266.             xstream.useAttributeFor(String.class);
  267.             xstream.useAttributeFor(Date.class);
  268.             xstream.useAttributeFor(Integer.class);
  269.             xstream.useAttributeFor(Boolean.class);
  270.  
  271.             xstreamRepresentation.setXstream(xstream);
  272.             TypeOutgoingResponse outgoingResponse = xstreamRepresentation.getObject();
  273.             log.debug(outgoingResponse.toString());
  274.             return outgoingResponse;
  275.         } catch (ResourceException re) {
  276.             log.error(re.getStatus().toString());
  277.             responseRepresentation = xstreamClient.getResponseEntity();
  278.             log.error(responseRepresentation.getText());
  279.         }
  280.         return null;
  281.     }
  282.  
  283.    
  284.     public final static AbstractSingleValueConverter
  285.         DISPATCH_METHOD_VALUE_CONVERTER = new AbstractSingleValueConverter() {
  286.  
  287.     @Override
  288.     public Object fromString(String parsedText) {
  289.         return EnumDispatchMethod.fromValue(parsedText);
  290.     }
  291.  
  292.     @Override
  293.     public String toString(Object obj) {
  294.         return ((EnumDispatchMethod)obj).value();
  295.     }
  296.  
  297.     @Override
  298.     public boolean canConvert(Class clazz) {
  299.         return clazz.equals(EnumDispatchMethod.class);
  300.     }
  301. };
  302.    
  303.     public final static AbstractSingleValueConverter
  304.         EVENT_TYPE_VALUE_CONVERTER = new AbstractSingleValueConverter() {
  305.  
  306.     @Override
  307.     public Object fromString(String parsedText) {
  308.         return EnumEntityType.fromValue(parsedText);
  309.     }
  310.  
  311.     @Override
  312.     public String toString(Object obj) {
  313.         return ((EnumEntityType) obj).value();
  314.     }
  315.  
  316.     @Override
  317.     public boolean canConvert(Class clazz) {
  318.         return clazz.equals(EnumEntityType.class);
  319.     }
  320. };
  321.  
  322.     public static String formatXML (String input) throws Exception {
  323.         Integer indent = 4;
  324.         Source xmlInput = new StreamSource(new StringReader(input));
  325.         StringWriter stringWriter = new StringWriter();
  326.         StreamResult xmlOutput = new StreamResult(stringWriter);
  327.         TransformerFactory transformerFactory = TransformerFactory.newInstance();
  328.         transformerFactory.setAttribute("indent-number", indent);
  329.         Transformer transformer = transformerFactory.newTransformer();
  330.         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  331.         transformer.transform(xmlInput, xmlOutput);
  332.         String output = xmlOutput.getWriter().toString();
  333.         return output;
  334.     }  
  335.    
  336. }
Advertisement
Add Comment
Please, Sign In to add comment