コード例 #1
0
ファイル: ArchiveHttpHandler.cs プロジェクト: f3d3r1c0/web
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            DateTime start = DateTime.Now;

            try
            {
                DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(DocumentRequest));
                DocumentRequest form = (DocumentRequest)deserializer.ReadObject(request.InputStream);

                if (form.aic != null)
                {
                    Stream stream = response.OutputStream;
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DocumentResponse));

                    /*
                    string sql_ = String.Concat(
                                "SELECT ",
                                "  TR001.FDI_0001 ",    // 0 - aic banca dati
                                "  TDF.FDI_T227 ",      // 1 - nome file pdf
                                "  TDF.FDI_T483 ",      // 2 - lingua
                                "  FS_MOBI.FDI_T485 ",  // 3 - aic farmastampati (null => foglietto aggiornato)
                                "FROM TR001 " +
                                "  LEFT JOIN TDF ON (TR001.FDI_0001=TDF.FDI_T218 COLLATE Latin1_General_CI_AS) " +
                                "  LEFT JOIN FS_MOBI ON (TR001.FDI_0001=FS_MOBI.FDI_T485) " +
                                "WHERE TR001.FDI_0001='", form.aic, "'");
                    */

                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        using (SqlCommand command = new SqlCommand(
                            String.Concat(
                                "SELECT ",
                                "  TR001.FDI_0001, ",    // 0 - aic banca dati
                                "  TDF.FDI_T227, ",      // 1 - nome file pdf
                                "  TDF.FDI_T483, ",      // 2 - lingua
                                "  FS_MOBI.FDI_T485 ",  // 3 - aic farmastampati (null => foglietto aggiornato)
                                "FROM TR001 " +
                                "  LEFT JOIN TDF ON (TR001.FDI_0001=TDF.FDI_T218 COLLATE Latin1_General_CI_AS) " +
                                "  LEFT JOIN FS_MOBI ON (TR001.FDI_0001=FS_MOBI.FDI_T485) " +
                                "WHERE TR001.FDI_0001='", form.aic, "'")
                                    , connection))
                        {
                            command.CommandType = CommandType.Text;
                            connection.Open();

                            using (SqlDataReader sqlreader = command.ExecuteReader(CommandBehavior.CloseConnection))
                            {
                                if (sqlreader.HasRows)
                                {
                                    ArrayList lst = new ArrayList();
                                    response.ContentType = "application/json";

                                    while (sqlreader.Read())
                                    {
                                        DocumentResponse entry = new DocumentResponse();
                                        entry.language = sqlreader.IsDBNull(2) ? "" : sqlreader.GetString(2);
                                        entry.pagesCount = countPdfPages(documentRoot + sqlreader.GetString(1));
                                        entry.filename = sqlreader.IsDBNull(1) ? "" : sqlreader.GetString(1).ToUpper().Replace(".PDF", "");
                                        entry.aicFS = sqlreader.IsDBNull(3) ? "" : sqlreader.GetString(3);
                                        lst.Add(entry);
                                    }

                                    bool hasUserLanguage = false;

                                    foreach (string lang in request.UserLanguages)
                                    {
                                        for (int i = 0; !hasUserLanguage && i < lst.Count; i++)
                                        {
                                            DocumentResponse entry = (DocumentResponse)lst[i];
                                            if (lang.ToLower().StartsWith(entry.language.ToLower().Substring(0, 2)))
                                            {
                                                entry.isDefaultLanguage = true;
                                                hasUserLanguage = true;
                                                break;
                                            }
                                        }
                                    }

                                    if (!hasUserLanguage) ((DocumentResponse)lst[0]).isDefaultLanguage = true;

                                    serializer.WriteObject(stream, lst.ToArray());

                                }
                                else
                                {
                                    throw new Exception("aic not found");
                                }
                            }
                        }
                    }
                }

            }
            catch (Exception e)
            {
                response.StatusCode = 500;
                response.StatusDescription = e.Message;
            }
            finally
            {
                double elapsed = (double)(DateTime.Now - start).TotalMilliseconds / 1000;

                if (Logger.Enabled)
                    Logger.Write("HTTP /archive {0}\t{1:F2}\t{2}\t{3}\t{4}\t{5}",
                            response.StatusCode == 200 ? "OK" : "FAIL",
                            elapsed,
                            request.UserHostAddress,
                            request.UserHostName != null && request.UserHostName.Length > 0 ? "anonymous" : request.UserHostName,
                            response.StatusCode,
                            response.StatusDescription
                                .Replace('\r', ' ')
                                .Replace('\n', ' ')
                                .Replace('\t', ' ')
                                .Trim()
                            );

            }
        }
コード例 #2
0
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest  request  = context.Request;
            HttpResponse response = context.Response;

            DateTime start = DateTime.Now;

            try
            {
                DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(DocumentRequest));
                DocumentRequest            form         = (DocumentRequest)deserializer.ReadObject(request.InputStream);

                if (form.aic != null)
                {
                    Stream stream = response.OutputStream;
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DocumentResponse));

                    /*
                     * string sql_ = String.Concat(
                     *          "SELECT ",
                     *          "  TR001.FDI_0001 ",    // 0 - aic banca dati
                     *          "  TDF.FDI_T227 ",      // 1 - nome file pdf
                     *          "  TDF.FDI_T483 ",      // 2 - lingua
                     *          "  FS_MOBI.FDI_T485 ",  // 3 - aic farmastampati (null => foglietto aggiornato)
                     *          "FROM TR001 " +
                     *          "  LEFT JOIN TDF ON (TR001.FDI_0001=TDF.FDI_T218 COLLATE Latin1_General_CI_AS) " +
                     *          "  LEFT JOIN FS_MOBI ON (TR001.FDI_0001=FS_MOBI.FDI_T485) " +
                     *          "WHERE TR001.FDI_0001='", form.aic, "'");
                     */

                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        using (SqlCommand command = new SqlCommand(
                                   String.Concat(
                                       "SELECT ",
                                       "  TR001.FDI_0001, ",  // 0 - aic banca dati
                                       "  TDF.FDI_T227, ",    // 1 - nome file pdf
                                       "  TDF.FDI_T483, ",    // 2 - lingua
                                       "  FS_MOBI.FDI_T485 ", // 3 - aic farmastampati (null => foglietto aggiornato)
                                       "FROM TR001 " +
                                       "  LEFT JOIN TDF ON (TR001.FDI_0001=TDF.FDI_T218 COLLATE Latin1_General_CI_AS) " +
                                       "  LEFT JOIN FS_MOBI ON (TR001.FDI_0001=FS_MOBI.FDI_T485) " +
                                       "WHERE TR001.FDI_0001='", form.aic, "'")
                                   , connection))
                        {
                            command.CommandType = CommandType.Text;
                            connection.Open();

                            using (SqlDataReader sqlreader = command.ExecuteReader(CommandBehavior.CloseConnection))
                            {
                                if (sqlreader.HasRows)
                                {
                                    ArrayList lst = new ArrayList();
                                    response.ContentType = "application/json";

                                    while (sqlreader.Read())
                                    {
                                        DocumentResponse entry = new DocumentResponse();
                                        entry.language   = sqlreader.IsDBNull(2) ? "" : sqlreader.GetString(2);
                                        entry.pagesCount = countPdfPages(documentRoot + sqlreader.GetString(1));
                                        entry.filename   = sqlreader.IsDBNull(1) ? "" : sqlreader.GetString(1).ToUpper().Replace(".PDF", "");
                                        entry.aicFS      = sqlreader.IsDBNull(3) ? "" : sqlreader.GetString(3);
                                        lst.Add(entry);
                                    }

                                    bool hasUserLanguage = false;

                                    foreach (string lang in request.UserLanguages)
                                    {
                                        for (int i = 0; !hasUserLanguage && i < lst.Count; i++)
                                        {
                                            DocumentResponse entry = (DocumentResponse)lst[i];
                                            if (lang.ToLower().StartsWith(entry.language.ToLower().Substring(0, 2)))
                                            {
                                                entry.isDefaultLanguage = true;
                                                hasUserLanguage         = true;
                                                break;
                                            }
                                        }
                                    }

                                    if (!hasUserLanguage)
                                    {
                                        ((DocumentResponse)lst[0]).isDefaultLanguage = true;
                                    }

                                    serializer.WriteObject(stream, lst.ToArray());
                                }
                                else
                                {
                                    throw new Exception("aic not found");
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                response.StatusCode        = 500;
                response.StatusDescription = e.Message;
            }
            finally
            {
                double elapsed = (double)(DateTime.Now - start).TotalMilliseconds / 1000;

                if (Logger.Enabled)
                {
                    Logger.Write("HTTP /archive {0}\t{1:F2}\t{2}\t{3}\t{4}\t{5}",
                                 response.StatusCode == 200 ? "OK" : "FAIL",
                                 elapsed,
                                 request.UserHostAddress,
                                 request.UserHostName != null && request.UserHostName.Length > 0 ? "anonymous" : request.UserHostName,
                                 response.StatusCode,
                                 response.StatusDescription
                                 .Replace('\r', ' ')
                                 .Replace('\n', ' ')
                                 .Replace('\t', ' ')
                                 .Trim()
                                 );
                }
            }
        }