Apache SOLR and C# windows application - c#

Am newbie to Apache SOLR 7.4. Am trying to upload XML file to remote server where the SOLR 7.4 is hosted.I need to update the collection with new XML file uploaded to the server. How can I re-index the collection ?
Things I have tried like using Simple Post Tool, CURL command with update etc.

If you're writing a C# application, using SolrNet is the suggested way. To upload a file to Solr Cell (which is what the module that extracts content from most file types is called), you can follow the example given in the manual. To parse the file for content to see how it behaves with the file you've uploaded:
ISolrOperations<Something> solr = ...
using (var file = File.OpenRead(#"test.pdf")) {
var response = solr.Extract(new ExtractParameters(file, "some_document_id") {
ExtractOnly = true,
ExtractFormat = ExtractFormat.Text,
});
Console.WriteLine(response.Content);
}
Set ExtractOnly to false to index the content, and use Fields to set additional field values (i.e. an id field, etc.).

Related

How to make a complex file download functionality with Blazor server?

I have a Blazor server application. I want to allow the user to download files but the content of the files needs to be built dynamically.
Basically the application shows reports to the user based on filters and etc. and I want the user to have the option to download whatever he is currently seeing.
I know I can make a "link button" that points to a Razor page that returns some sort of FileContentResult in its OnGet method but I have no idea how to pass any data to that so that the correct report file can be built.
I know there is an alternative that uses JavaScript but, as far as I know, it's more cumbersome and I'm not sure if it is any better.
I thought about doing a request to some sort of REST/WebAPI (which would allow me to pass arguments and stuff) but I cannot seem to get a WebAPI and Blazor Server projects run at the same time. The only partial success I've had is adding a WebAPI project to my Blazor Server solution and starting both. But then, while debugging, for some reason, both processes stop when I download the file.
Also the application must be hosted on Azure Web app and am not sure how feasible it would be to run both projects at the same time.
So, how can I make my Blazor Server allow the user to download a file but generate the file dynamically based on what the user is seeing on his browser?
The JavaScript alternative is very straightforward.
export function saveAsFile(filename, bytesBase64) {
if (navigator.msSaveBlob) {
//Download document in Edge browser
var data = window.atob(bytesBase64);
var bytes = new Uint8Array(data.length);
for (var i = 0; i < data.length; i++) {
bytes[i] = data.charCodeAt(i);
}
var blob = new Blob([bytes.buffer], { type: "application/octet-stream" });
navigator.msSaveBlob(blob, filename);
}
else {
var link = document.createElement('a');
link.download = filename;
link.href = "data:application/octet-stream;base64," + bytesBase64;
document.body.appendChild(link); // Needed for Firefox
link.click();
document.body.removeChild(link);
}
}
create a bytestream of whatever content you want to create and call this function through IJSInterop. I have used it in blazor server and it works well.
Please note : I found this piece of code online but I don't remember from where. I will be happy to give credit to the original author if someone knows the source.

How to download a file from a shared folder in asp.net using <a href>?

Background:
There is an asp.Net MVC application, which I am currently re-creating without using MVC.
In the MVC application, the <a> elements that enables users to download a document is like:
download
and it works when I click download, because I could use a method like below in MVC:
public async Task<IActionResult> Download(string path)
{
var memory = new MemoryStream();
using (var stream = new FileStream(path, FileMode.Open))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
return File(memory, GetContentType(path), Path.GetFileName(path));
}
Note that there is an HTML table with 500+ rows in the page, some data has a file to be downloaded and for only those rows, the href link exists in a specific cell.
Here, when I try to visit http://myapplication/api/file/2187/filter.jpg, I could see the link exists.
Problem:
I couldn't use the same Download method in .net only Application because IActionResult looks for the namespace Microsoft.AspNet.MVC. Tried to set the <a> element in my asp.net application like below:
download
However, this does not work in Chrome giving me the error: Not allowed to load local resource
I couldn't find a way to create a link on http for the file like the way it is in the MVC application. How can I enable a link in the application like: http://myapplication/api/file/2187/filter.jpg to use in the href?
Note: The files to be downloaded are located in a Shared folder, can't move them into my local PC or can't import them inside the project etc..
Note 2: There should be no page-refresh after downloading the file.
Any help or advice should be appreciated! Thanks.
File Paths Should Not Be Accepted From URL or Query String:
I would strongly recommending not using filepath in the querystring or URL.
I see in the code that you are not validating the filePath and directly downloading a file. This can be very dangerous. People can try downloading web.config or any other file on your web server if the permissions are not set correctly or if there is no other mechanism to block such request.
I would strongly recommend using Indirection for file download.
What is Indirection ?
In indirection, you would create an identifier for the file. It may be a GUID as well.
Then you can accept this GUID through QueryString or through URL.
Then in your action you would map it to right file and then download it.
Code Example:
Below code example, uses file ID.
When the file id is passed, it tries to get the file details in CustomDocumentObj.
These details contains actual file path.
You can then perform validations to check if this is the application related file and if your application allows to download that file.
public class ServiceController : Controller
{
public ActionResult DownloadFile(string id)
{
CustomDocumentObj document = new CustomDocumentObj(Int32.Parse(id));
// OPTIONAL - validation to check if it is allowed to download this file.
string filetype = Helpers.GetMimeType(document.FilePath);
return File(document.FilePath, filetype, Path.GetFileName(document.FilePath));
}
}
Then in URL it can be something like below:
Download File
Refer this blog for further details.

Using the Azure API, how do I list and add virtual directories to a website?

I am trying to add a Virtual Directory to an Azure Web Site from a WinForms Application using the Azure API. I can enumerate the WebSites on in my webspace, but I cannot find a method that allows me access to the Virtual Directories in the WebSite.
Here is my code:
string certPath = Properties.Settings.Default.AzureCertificatePath;
string certPassword = Properties.Settings.Default.AzureCertificatePassword;
string subscriptionId = Properties.Settings.Default.AzureSubscriptionId;
var cert = new X509Certificate2(certPath, certPassword, X509KeyStorageFlags.MachineKeySet);
var cred = new CertificateCloudCredentials(subscriptionId, cert);
using (var client = new WebSiteManagementClient(cred))
{
var spaces = client.WebSpaces.List();
foreach (var space in spaces)
{
Console.WriteLine("Space: {0}", space.Name);
var sites = client.WebSpaces.ListWebSites(space.Name, new WebSiteListParameters {PropertiesToInclude = { "Name" } }); ***// Where do I find out what properties can be included in this array?***
foreach (var site in sites)
{
***// What goes here to show the virtual directories in this specific website??????***
}
}
}
I found that the Azure web services API does not offer access to the virtual directories/applications on a web app, although the underlying REST API's that it uses does.
Luckily, the management API is open source (I wanted to get the v3.0.0.0 of the website management API, matching what I had NuGet'd, which I found here: https://github.com/Azure/azure-sdk-for-net/commits/master?page=79) so with a little perseverance and messing about with .targets files and NuGet references, you can get the source code of the WebSiteManagement project into your solution instead of the referenced DLL that you likely downloaded from NuGet.
From there, if you go into your client.WebSites.GetConfiguration method, stick a breakpoint in and capture the HTTP response returned - you'll see that in the JSON the VirtualApplications on your website are indeed there.
From there, you can edit your copy of the source code to expose those object structures out, much the same way that other object structures are mapped out of the JSON (e.g. HandlerMappings).
Likewise for updating them, you need to add the VirtualApplications (in the ewact same format) into the Microsoft.WindowsAzure.Management.WebSites.Models.WebSiteUpdateConfigurationParameters, and pass that into client.WebSites.UpdateConfiguration (which you will also need to amend to map your VirtualApplications object structure back into JSON in the same format).
Works great for me doing it this way.
Note/Disclaimer: The GitHub site documentation does mention that much of the source code was autogenerated, and that you shouldn't really go editing that code, instead raise an issue on the project about getting it sorted. I didn't have the time to do this and needed an immediate way of getting it working with my local copy of the code only, so my solution does, as you'll note when you look a the source, involve adding to that auto-gen'd code. It works great, but I should in good conscience point you to the project owner's warnings about tinkering with the auto-gen'd code.

How to achieve webservice testing automation through programming/coding

I know these questions have been asked many times , I am still looking for an answer that satisfies my question. please have a look at below points what I am looking for:
First thing , How to achieve that through programming. for web automation I am using specflow C#. do i need to create unit tests for the web service testing ? what can be done through programming ? soapUI is ruled out as its already used by manual testers.
Are there any available frameworks for these ?
Please give me links and examples supporting them.
I know its a broad question. but I do need some help from everyone so that I start in the right direction on API testing.
Your absolute first port of call is to understand the web service you are testing. Do you have valid XML you can submit to the web service? If so, store it somewhere and create a step that obtains it from the folder. Use that XML as your test case and iterate through each file in the folder. Each XML file should be different based on what you want the tests to cover.
Generally, web service testing will include logging response times, sending differing flavors of XML as test cases (alter the content of the nodes to cover different triggers), and checking for particular values in your responses.
A very high level and implicit Scenario could look like this (based on something I did once):
Scenario Outline: Submit Requests to Web Service
Given I have XML file '<XML_Case>'
And I submit a 'POST' request
Then I should receive a response from the Web Service
And the response will include a 'ResponseId' in the 'Header' section
And the response will include a 'RequestId' in the 'Body' section
And the 'Complete' node in the 'Body' section will return the value of 'True'
Examples:
|XML_Case |
| C:\TestData\test1.xml |
| C:\TestData\test2.xml |
| etc... |
To drive this, there are many approaches in C#. To start you off, you could check for the XML file and convert it into your XML request via something like this as a step definition:
[Given(#"I have XML file '(.*)'")]
public void GivenIHaveXMLFile(string fileName)
{
//Checks if file exists
if (System.IO.File.Exists(fileName))
{
var requestXml = CreateXMLInstance(fileName);
}
else
{
throw new Exception("No XML file found in specified location");
}
}
The CreateXMLInstance method used in that code would load the XML from the file path and could be like this:
public XmlDocument CreateXmlInstance(string xmlPath)
{
//Loads XML from file path
XmlDocument request = new XmlDocument();
request.Load(xmlPath);
return request;
}
You've essentially created your first few steps and could store the requestXml variable as a field or in Specflow's ScenarioContext for use later when you do the web service calls.
There is obviously a lot more to consider but this might send you in the right direction.

Manipulate umbraco content using a console application

This is a noob question but I'm searching for some time and can't find any useful information.
I need to develop a rotine (console application) that will read and write content into a umbraco site. I've already read that you can do that with web forms and mvc application.
But I need to use umbraco like an external source. I need to do something like we do with Word documents. For example: open the file, read the file, write some things and save it.
I've already installed the API using
PM> Install-Package UmbracoCms -Pre
Some things I've already read:
http://nishantwork.wordpress.com/2012/09/27/umbraco-create-custom-content-node-in-umbraco-by-c/
https://github.com/sitereactor/umbraco-console-example
What is the best to achieve that? I don't know how to do it exactly...
You can create an Umbraco node (document), write to it and save it from a console application. Umbraco is basically a bunch of .Net libraries:
//Get the type you would like to use by its alias and the user who should be the creator of the document
DocumentType dt = DocumentType.GetByAlias("Textpage");
User author = User.GetUser(0);
//create a document with a name, a type, an umbraco user, and the ID of the document's parent page. To create a document at the root of umbraco, use the id -1
Document doc = Document.MakeNew("My new document", dt, author, 1018);
// Get the properties you wish to modify by it's alias and set their value
doc.getProperty("bodyText").Value = "<p>Your body text</p>";
doc.getProperty("articleDate").Value = DateTime.Now;
//after creating the document, prepare it for publishing
doc.Publish(author);
//Tell umbraco to publish the document
umbraco.library.UpdateDocumentCache(doc.Id);
See:
http://our.umbraco.org/wiki/reference/api-cheatsheet/creating-a-document
http://our.umbraco.org/wiki/reference/api-cheatsheet/modifying-document-properties
Just to help anyone with the same issue. I'm find out a web service in umbraco and i'm currently using that (until now for reading information only, but as far as I know we can write infomation also). Altought there's little documentation is easy to use.
But to use that you need to set <webservices enabled="False"> in umbracoSettings.config . This fie is in the folder Config inside umbraco.
We have to set user rights into the webservices node also to allow the user to use the web service
DocumentServiceReference.documentServiceSoapClient client = new DocumentServiceReference.documentServiceSoapClient();
client.WebservicesEnabled();
DocumentServiceReference.ArrayOfDocumentCarrier documents = client.readList(parentId, username, password);
foreach (DocumentServiceReference.documentCarrier doc in documents)
{
DocumentServiceReference.ArrayOfDocumentProperty properties = doc.DocumentProperties;
foreach (DocumentServiceReference.documentProperty property in properties)
{
string key = property.Key;
string value = property.PropertyValue.ToString();
}
}

Categories